Dynamically set the property name of a C# anonymous type

后端 未结 6 1979
一向
一向 2020-12-19 02:28

Is there any way to dynamically set the property name of an anonymous type?

Normally we\'d do like this:

var anon = new { name = \"Kayes\" };
         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-19 02:43

    The compiler must know about the property name, otherwise it can't create the anonymous type for you.

    So, no, this isn't possible, unless the actual property name is known at compile-time (through some VS-generated magic with the database or something).

提交回复
热议问题