小编Gor*_*yan的帖子

使用"is"关键字和"null"关键字c#7.0

最近我发现,以下代码在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中无效

c# null keyword c#-7.0

8
推荐指数
2
解决办法
3796
查看次数

Protobuf-net 动态类型数组

我不会使用 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)

serialization object protobuf-net dynamictype

5
推荐指数
1
解决办法
1614
查看次数

如何重写表达式x =>!x到x => x!= true和x => x到x => x == true

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# linq expression expressionvisitor

2
推荐指数
1
解决办法
298
查看次数