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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/793415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:21:12  来源:igfitidea点击:

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

c#.netc#-3.0anonymous-class

提问by Prashant Cholachagudda

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?

有没有办为这些类指定一个有意义的名称?

回答by Dead account

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" };

回答by Chad Grant

public class Employee {}

回答by Chris

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.

实际上,编译器生成的类型名称在命名中使用了非字符,因此您的应用程序中不会发生名称冲突。

回答by Nick Berardi

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" };

回答by Mark Carpenter

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.

我认为,根据定义,匿名类型不能有一个名字,只有编译器给它的名字。如果您正在寻找有关匿名类型的更有意义的设计时信息,那么您对编译器的期望过高。

回答by jcopenha

Yes, you are creating an Anonymous Class, if you want your class to have a name, i.e. Not Anonymous, then declare a regular class or struct.

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

回答by Luis Abreu

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.

因为它是匿名的,你不能命名它。如果您需要知道类型的名称,那么您必须真正创建一个类。

回答by JaredPar

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.

如果您确实需要为类型命名,则需要显式声明并使用具有所需属性的新类型。

回答by Mia Clarke

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. 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

类型生成器

回答by Damian

The answer in Java World would be a local class(defined in a method), which are absent in C#.

Java World 中的答案是本地类(在方中定义),而 C# 中没有。