我有以下代码.
public static void GuessTheType()
{
dynamic hasValue = true;
dynamic value = "true";
var whatami1 = hasValue ? (string)value : null;
var whatami2 = hasValue ? bool.Parse(value) : true;
var whatami3 = hasValue ? (bool)bool.Parse(value) : true;
}
Run Code Online (Sandbox Code Playgroud)
编译器推断的类型whatami1是string.
编译器推断的类型whatami2是dynamic.
编译器推断的类型whatami3是bool.
为什么第二种类型不是bool?
在 SonarQube 的分析过程中,我们看到了许多此警告的示例
更改此条件,使其不总是评估为“假”;一些后续代码永远不会执行。
所以发生这种情况的一个例子是 - 对于给定的代码......
if (components?.ContainsKey(machineType) ?? false)
{
return components[machineType];
}
throw new ArgumentException($"There is no available configuration for {machineType}!");
Run Code Online (Sandbox Code Playgroud)
……我们得到……
我们正在使用 C# null 合并来快捷地进行空检查,以便提供components是一个字典,不是null,并且components 确实包含由machineType我们指定的键,然后我们返回components字典中machineType键的值。
否则,如果 components为 null,或者如果 Dictionary不包含键,则表达式计算为false并且我们不进入块并返回值,而是抛出异常。
请有人解释为什么 SonarQube 抱怨这个。如果我们重写它以使用空检查和&&ing的“老式”冗长风格,那么 SonarQube 就是一个快乐的兔子。
仅仅是 SonarQube 不了解??操作符吗?
还是我们只是在这样的语句中滥用?.or??运算符?
是否可以在同一属性上使用公共 init 访问器和私有 setter?
目前我收到错误CS1007 “已定义属性访问器”。
public record Stuff
{
public int MyProperty { get; init; private set; } // Error
public void SetMyProperty(int value) => MyProperty = value;
}
var stuff = new Stuff
{
MyProperty = 3, // Using the init accessor
};
stuff.SetMyProperty(4); // Using the private setter (indirectly)
Run Code Online (Sandbox Code Playgroud)
我最好的猜测是使用私有成员变量,该变量的属性get和init访问器(不是自动实现的)和 setter 成员函数。可以更轻松地完成吗?
我有这个代码:
string json2 = vc.Request(model.uri + "?fields=uri,transcode.status", "GET");
var deserial = JsonConvert.DeserializeObject<Dictionary<string, object>>(json2);
var transcode = deserial["transcode"];
var serial = JsonConvert.SerializeObject(transcode);
var deserial2 = JsonConvert.DeserializeObject<Dictionary<string, object>>(serial);
var upstatus = deserial2["status"].ToString();
Run Code Online (Sandbox Code Playgroud)
我从服务器得到的json是:
{
"uri": "/videos/262240241",
"transcode": {
"status": "in_progress"
}
}
Run Code Online (Sandbox Code Playgroud)
在VS2017上运行,可以正常运行。
但在 VS2010 上出现以下错误:
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“System.Collections.Generic.Dictionary`2[System.String,System.Object]”,因为该类型需要 JSON 对象(例如 {"name ":"value"}) 以正确反序列化。要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"}),或者将反序列化类型更改为数组或实现集合接口(例如 ICollection、IList)的类型,例如 List,可以从 JSON 数组反序列化。还可以将 JsonArrayAttribute 添加到类型中,以强制其从 JSON 数组进行反序列化。路径 '',第 1 行,位置 1。
我正在使用 Newtonsoft.Json。
任何想法?
大家好我写了一个简单的程序,将参数中的字符串拆分为数字,字母和运算符,但是我遇到了23x + 3 = 8
foreach (char x in args[i]){
if (char.IsNumber(x)){
inter[i] = Convert.ToInt32(x);
Console.WriteLine("{0} is a number ", x);
}
else if (char.IsLetter(x)){
apha[i] = x;
Console.WriteLine("{0} is a Letter ", x);
}
else if (char.IsSymbol(x)){
symbol[i] = x;
Console.WriteLine("{0} is a Symbol ", x);
}
Run Code Online (Sandbox Code Playgroud)
我发现输出是分开的每个char 2和3我想要23作为一个整数.有没有办法将2号码组合在一起?
I have the asp.net core MVC project and separate WebApi project in one solution. I'm adding the swagger following the documentation on github. Here is my Startup.cs of mvc project:
public void ConfigureServices(IServiceCollection services)
{
//...
// Adding controllers from WebApi:
var controllerAssembly = Assembly.Load(new AssemblyName("WebApi"));
services.AddMvc(o =>
{
o.Filters.Add<GlobalExceptionFilter>();
o.Filters.Add<GlobalLoggingFilter>();
})
.AddApplicationPart(controllerAssembly)
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddSwaggerGen(c =>
{
//The generated Swagger JSON file will have these properties.
c.SwaggerDoc("v1", new Info
{
Title = "Swagger XML …Run Code Online (Sandbox Code Playgroud) 我有一个基类和几个派生类(例如Base和ChildA : Base).每次我创建一个ChildA类的实例时,我希望它被分配一个唯一的实例编号(类似于关系数据库中的自动增量ID,但对于我在内存中的类而不是在数据库中).
我的问题类似于这个问题,但有一个明显的区别:我希望基类自动处理这个问题.对于我的每个派生类(ChildA,ChildB,ChildC等),我希望基类保持单独的计数,并在创建该派生类的新实例时增加它.
所以,我Base班上的信息可能最终看起来像这样:
ChildA,5
ChildB,6
ChildC,9
Run Code Online (Sandbox Code Playgroud)
如果我然后实例化一个新的ChildB(var instance = new ChildB();),我希望ChildB被赋予id 7,因为它从6开始.
然后,如果我实例化一个新的ChildA,我希望ChildA被分配id 6.
-
我怎样才能在Base班级的构造函数中处理这个问题?
我有一个包含多个<p>标签的 XML 文件。一些<p>标签包含<br/>在其中。所以,我应该为标签中的XElement每个创建一个新<br/>的。我试图通过使用读取每一行foreach并将每一行替换<br/>为</p> + Environment.NewLine + <p>.
它的工作原理,但如果<p>包含这样的标签<b>或<i>,然后<和>成为<和>分别。这就是为什么我想要一种linq方法或一种foreach方法,以便我能够以 XML 格式进行更改。
请帮忙。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE repub SYSTEM "C:\repub\Repub_V1.dtd">
<?xml-stylesheet href="C:\repub\repub.xsl" type="text/xsl"?>
<repub>
<head>
<title>xxx</title>
</head>
<body>
<sec>
<title>First Title</title>
<break name="1-1"/>
<pps>This is Sparta</pps>
<h1><page num="1"/>First Heading</h1>
<bl>This is another text</bl>
<fig><img src="images/img_1-1.jpg" alt=""/><fc>This is a …Run Code Online (Sandbox Code Playgroud) 我想知道在 C# 中是否可以这样做:
public class Outer
{
public class Inner {}
public Inner CreateInner()
{
return new Inner(); // should only be allowed inside this method
}
}
Run Code Online (Sandbox Code Playgroud)
您只能在外部类方法内创建内部类的新实例。我想这样做是因为我需要更好地控制创建的内部类,它可以帮助我模拟 CreateInner 的返回值并验证它是否被调用。这可能吗?
我正在尝试将.Net框架应用程序迁移到.Net Core,在此过程中,我想将内存中的缓存从System.Runtime.Caching/MemoryCache迁移到Microsoft.Extensions.Caching.Memory/IMemoryCache。但是我有一个问题IMemoryCache,在删除/撤消缓存之前,我找不到刷新缓存的方法。
在的情况下System.Runtime.Caching/MemoryCache,可以UpdateCallback在CacheItemPolicy其中设置回调函数的委托的属性,并且在逐出缓存对象之前,将在单独的线程中调用此函数。即使回调函数需要很长时间来获取新数据,MemoryCache仍将在过期之前继续提供旧数据,这确保了我的代码在缓存刷新过程中无需等待数据。
但是我没有看到这样的功能,在Microsoft.Extensions.Caching.Memory/IMemoryCache里面有
RegisterPostEvictionCallback属性和PostEvictionCallbacks扩展方法MemoryCacheEntryOptions。但是这两个都将在从缓存中逐出缓存条目后触发。因此,如果此回调花费较长时间,则需要等待所有获取此数据的请求。
有什么解决办法吗?