简体   繁体   English

我可以在C#中为匿名类指定有意义的名称吗?

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

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 type: ...在运行时它将是类型:

<>f__AnonymousType0<int,string>

Is there any way to specify a meaningful name to such classes? 有没有办法为这些类指定有意义的名称?

public class Employee {}
\n

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. 实际上,编译器生成的类型的名称在命名中使用非法字符,因此您的应用程序中不会发生名称冲突。

public class Employee {
    public int ID { get; set; }
    public string Name { get; set; }
}

Then use the following syntax 然后使用以下语法

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

Make it a regular class with a name? 把它变成一个有名字的普通班级?

public class Employee
{
    public int ID;
    public string Name;
}


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

Actually, if you're not afraid of getting extremely nitty gritty, you could use TypeBuilder to build your own runtime type based on your anonymous type, which will allow you to specify a name for the type. 实际上,如果你不害怕获得极端的细节,你可以使用TypeBuilder根据你的匿名类型构建自己的运行时类型,这将允许你指定类型的名称。 Of course, it is much easier to just declare a class as almost everyone else in this thread suggested, but the TypeBuilder way is far more exciting. 当然,在这个线程建议中几乎所有其他人都声明一个类要容易得多,但TypeBuilder方式更令人兴奋。 ;) ;)

TypeBuilder TypeBuilder

Java World中的答案是本地类 (在方法中定义),在C#中不存在。

是的,您正在创建一个匿名类 ,如果您希望您的类具有名称,即Not Anonymous ,则声明一个常规类或结构。

Since it's anonymous, you cannot name it. 由于它是匿名的,你无法命名。 If you need to know the name of a type, then you must really create a class. 如果您需要知道类型的名称,那么您必须真正创建一个类。

No, there is no way to give a meaningful type name to these classes as you've declared them. 不,当你声明它们时,没有办法给这些类提供有意义的类型名称。 Anonymous Types are just that, anonymous. 匿名类型只是匿名的。 There is no way to explicitly "name" the type in code without resorting to very ugly hacks. 没有办法明确地“命名”代码中的类型而不诉诸非常丑陋的黑客。

If you really need to give the type a name you will need to explicitly declare and use a new type with the properties you need. 如果您确实需要为类型指定名称,则需要显式声明并使用具有所需属性的新类型。

I think that, by definition, Anonymous Types can't have a name, just that which the compiler gives it. 我认为,根据定义,匿名类型不能有一个名称,只是编译器给出的名称。 If you're looking for more meaningful design-time information on Anonymous Types then you're expecting too much from the compiler. 如果您正在寻找有关匿名类型的更有意义的设计时信息,那么您对编译器的期望太高了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM