[英]Object-To-Dictionary, compiler-generated function
我对C#编译器的最新功能有些不了解,因此我必须提出以下问题:
有没有办法获得编译器生成的成员函数,该成员函数对类的所有已定义和继承的属性进行迭代(在编译时,而不是在运行时通过反射),并返回名称-值字典?
class Foo
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
[Microsoft.CSharp._SomethingSomething_ToDictionaryConverter]
// Compiler-generated function:
// return new Dictionary<string, object>()
// {
// { nameof(Foo.A), this.A },
// { nameof(Foo.B), this.B },
// { nameof(Foo.C), this.C },
// { nameof(Foo.D), this.D },
// }
public Dictionary<string, object> ToDict();
}
// Then,
var foo = new Foo();
var d = foo.ToDict(); // yay!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.