最近我发现,以下代码在VS2017中编译并按预期工作.但我找不到任何关于此的主题/文档.所以我很好奇使用这种语法是否合法:
class Program
{
static void Main(string[] args)
{
var o = new object();
Console.WriteLine(o is null);
o = null;
Console.WriteLine(o is null);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这在VS2015中无效
我不会使用 Protobuf-net 进行一些序列化,并会出现此代码片段的以下错误:
错误:
动态类型不是合同类型:TestType[]
片段:
using System.IO;
namespace QuickStart
{
class Program
{
static void Main()
{
//FileAccess.ShowFileAccess();
//Sockets.ShowSockets();
var dto = new DataTransferType
{
ProtoDynamicProperty = new TestType[]
{
new TestType {UselessProperty="AAA"},
new TestType{UselessProperty="BBB"},
new TestType{UselessProperty="CCC"}
}
};
using (MemoryStream testStream = new MemoryStream())
{
ProtoBuf.Serializer.SerializeWithLengthPrefix(testStream, dto, ProtoBuf.PrefixStyle.Base128);
}
}
}
[ProtoBuf.ProtoContract]
struct TestType
{
[ProtoBuf.ProtoMember(1)]
public string UselessProperty { get; set; }
}
[ProtoBuf.ProtoContract]
class DataTransferType
{
[ProtoBuf.ProtoMember(1, DynamicType = true)]
public object ProtoDynamicProperty { get; set; …Run Code Online (Sandbox Code Playgroud) Asume,我们这样的表达式:
someIQueryable.Where(x => x.SomeBoolProperty)
someIQueryable.Where(x => !x.SomeBoolProperty)
Run Code Online (Sandbox Code Playgroud)
我需要像上面这样的表达式转换(使用表达式访问器重写)表达式:
someIQueryable.Where(x => x.SomeBoolProperty == true)
someIQueryable.Where(x => x.SomeBoolProperty != true)
Run Code Online (Sandbox Code Playgroud)
注意:如果我们有更复杂的表达式,重写器也必须在更一般的情况下工作:
someIQueryable.Where((x => x.SomeBoolProperty && x.SomeIntProperty > 0) || !x.SomeOtherBoolProperty))
Run Code Online (Sandbox Code Playgroud) c# ×2
c#-7.0 ×1
dynamictype ×1
expression ×1
keyword ×1
linq ×1
null ×1
object ×1
protobuf-net ×1