nameof expression in .net framework 4

前端 未结 5 606
小鲜肉
小鲜肉 2020-12-03 07:05

\"nameof\" expression is introduced in Visual Studio 2015 and c# 6

nameof (C# and Visual Basic Reference)

How can u use it or write a similar method in older

5条回答
  •  天命终不由人
    2020-12-03 07:38

    @Rob, thank you so much, but the static member access could be for the non-static class members too.

    public static class TestExtension {
        public static String nameof(this Expression> accessor)
        {
            return nameof(accessor.Body);
        }
    }
    class MyClass {
        public Guid MyOtherField;
    }
    TestExtension.nameof(o=>o.MyOtherField);
    

    and the good news is the Class won't be instantiated. It is useful to access domain layer DTOs.

提交回复
热议问题