Can I specify a meaningful name for an anonymous class in C#?

后端 未结 10 1740
小蘑菇
小蘑菇 2021-02-01 13:12

We all know that when we create an anonymous class like this:

var Employee = new { ID = 5, Name= \"Prashant\" };

...at run time it will be of t

10条回答
  •  孤城傲影
    2021-02-01 13:57

    It's an anonymous type, that defeats the purpose. Those objects are designed to be temporary. Hell, the properties are even read-only.

    Sorry, I'm being a smart-ass. The answer is no, there is no way to tell the compiler what name to use for an anonymous type.

    In fact, the names of the types generated by the compiler use illegal characters in their naming so that you cannot have a name collision in your application.

提交回复
热议问题