我有一个以下函数接受枚举值,并根据枚举值返回一个常量值(在不同的类中).现在我得到一个"Constant initializer missing"错误.
public const int Testfunction(TESTENUM TestEnum)
{
switch(TestEnum)
{
case TestEnum.testval1:
return testclass.constvalue;
case TestEnum.testVal2:
return testclass.constvalue1;
case TestEnum.testVal3:
return testclass.constvalue2;
}
}
Run Code Online (Sandbox Code Playgroud)
函数的返回类型究竟如何?(我使用对象返回类型,这不会抛出任何错误)
有没有其他选择来实现相同的目标?
我要做的就是检查列表B中的所有元素是否都在列表A中
if (listA.All(element => listB.Contains(element))
{
return;
}
Run Code Online (Sandbox Code Playgroud)
有人想出了另一个解决方案,说这会起作用并且有效!
if (listA.All(listB.Contains))
return;
Run Code Online (Sandbox Code Playgroud)
现在,(我知道它有效),
提前致谢.
今天,我想用文件执行操作,所以我想出了这段代码
class Test1
{
Test1()
{
using (var fileStream = new FileStream("c:\\test.txt", FileMode.Open))
{
//just use this filestream in a using Statement and release it after use.
}
}
}
Run Code Online (Sandbox Code Playgroud)
但在代码审查中,我被要求实现IDisposable接口和Finalizer方法
class Test : IDisposable
{
Test()
{
//using some un managed resources like files or database connections.
}
~Test()
{
//since .NET garbage collector, does not call Dispose method, i call in Finalize method since .net garbage collector calls this
}
public void Dispose()
{
//release my files or …Run Code Online (Sandbox Code Playgroud) 我有这个sealed班,
public sealed class A
{
public string AName {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
有人可以这样写一个扩展方法:
public static class Extensions
{
public static void ExtensionMethodForA (this A a)
{
Console.WriteLine("A's Extension method!");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,你如何防止这种情况?
如果我们有一个带有如下方法的API控制器,这似乎是完全有效的:
[HttpPost]
public IHttpActionResult DoStuff([FromBody]ModelA modelA, [FromBody]ModelB modelB)
Run Code Online (Sandbox Code Playgroud)
注意这两个[FromBody]属性.
问题是,如何调用这样的方法?
localhost/test/DoStuff/
Run Code Online (Sandbox Code Playgroud)
发布数据:
<?xml version="1.0"?>
<ModelA>
...
</ModelA>
<ModelB>
...
</ModelB>
Run Code Online (Sandbox Code Playgroud)
似乎没有得到承认.有什么想法吗?
编辑:错误数据如下:
<?xml version="1.0"?>
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Can't bind multiple parameters ('model1' and 'model2') to the request's content.
</ExceptionMessage>
</Error>
Run Code Online (Sandbox Code Playgroud) 我在WebApi中有这个方法
[IHttpActionResult]
[Get]
public void GetSome(P1, P2 ....... Pn)
{
...///
}
Run Code Online (Sandbox Code Playgroud)
只是好奇:
任何想法,WebApi为此方法允许的最大参数数量是多少?
我有一个ExProperty类似下面的课程:
class ExProperty
{
private int m_asimplevar;
private readonly int _s=2;
public ExProperty(int iTemp)
{
m_asimplevar = iTemp;
}
public void Asimplemethod()
{
System.Console.WriteLine(m_asimplevar);
}
public int Property
{
get {return m_asimplevar ;}
//since there is no set, this property is just made readonly.
}
}
class Program
{
static void Main(string[] args)
{
var ap = new ExProperty(2);
Console.WriteLine(ap.Property);
}
}
Run Code Online (Sandbox Code Playgroud)
将属性设置为只读或只写的唯一目的是什么?我看,通过下面的程序可以readonly达到相同的目的!
当我将该属性设置为只读时,我认为它应该不可写。当我使用
public void Asimplemethod()
{
_s=3; //Compiler reports as "Read only field cannot …Run Code Online (Sandbox Code Playgroud)以下模型serializes类
[XmlRoot]
public class A
{
[XmlAttribute]
public string Period { get; set; }
public List<C> B { get; set; }
}
<?xml version=1.0>
<A Period="Today">
<B>
<C>
</C>
<C>
</C>
</B>
</A>
Run Code Online (Sandbox Code Playgroud)
现在,我不希望这样<B>(对象列表应该直接列在下面XmlRoot)
生成的 XML 应该如下所示:
<A Period="Today">
<C>
</C>
<C>
</C>
</A>
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
有这个控制器文件 SecretAuthController.cs
public class SecretAuthController : Controller
{
public ActionResult Auth()
{
//library method whose signature recently changed to async
return ProcessTelemetry();
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我的同事将此代码更改为
public class SecretAuthController : MyBaseController
{
public Async Task<ActionResult> Auth()
{
//library method whose signature recently changed to async
await MyLib.GetTaskDetails(Id);
return ProcessTelemetry();
}
}
Run Code Online (Sandbox Code Playgroud)
在此过程中,他们还将所有其他控制器方法签名也更改为使用 1 async。
所以,有些方法看起来像
public Async Task<ActionResult> ExFooTest()
{
//There is no awaitable call here
//normally returns.
}
Run Code Online (Sandbox Code Playgroud)
对此有几点疑问:
async是否明智?async,是否需要更改所有操作方法签名,这是一个通用规则吗?(假设他们不等待)阅读有关SameSite为防止跨站点伪造而强制执行的更改。来源:https : //blog.chromium.org/2019/10/developers-get-ready-for-new.html
我正在尝试将其值设置为“无”并Secure按照宣传的方式使用。
我目前的web.config设置如下:
<system.web>
<sessionState cookieless="UseCookies"
timeout="20"
cookieSameSite="None"
xdt:Transform="Replace"
xdt:Locator="Match(cookieless)"/>
</system.web>
Run Code Online (Sandbox Code Playgroud)
文档来源:https ://docs.microsoft.com/en-us/dotnet/api/system.web.configuration.sessionstatesection.cookiesamesite ? view = netframework-4.8#System_Web_Configuration_SessionStateSection_CookieSameSite
但我仍然收到以下错误:
A cookie associated with a resource at `mywebsite.net` was set with `SameSite=None` but without `Secure`. A future release of Chrome will only deliver cookies marked `SameSite=None` if they are also marked `Secure`.
Run Code Online (Sandbox Code Playgroud)
如何secure在上述 web.config 文件中指定属性?任何线索将不胜感激。
c# ×10
.net ×7
.net-4.5 ×3
asp.net-mvc ×2
.net-4.0 ×1
asp.net ×1
async-await ×1
asynchronous ×1
c#-4.0 ×1
cookies ×1
enums ×1
samesite ×1
web-config ×1