在 C# 中以字符串形式获取类的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/179700/
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-03 16:49:32  来源:igfitidea点击:

Get the name of a class as a string in C#

c#entity-framework

提问by Jared

Is there a way to take a class name and convert it to a string in C#?

有没有办法在 C# 中获取类名并将其转换为字符串?

As part of the Entity Framework, the .Include method takes in a dot-delimited list of strings to join on when performing a query. I have the class model of what I want to join, and for reasons of refactoring and future code maintenance, I want to be able to have compile-time safety when referencing this class.

作为实体框架的一部分,.Include 方法接受以点分隔的字符串列表,以在执行查询时加入。我有我想要加入的类模型,并且出于重构和未来代码维护的原因,我希望在引用此类时能够具有编译时安全性。

Thus, is there a way that I could do this:

因此,有没有一种方法可以做到这一点:

class Foo
{
}

tblBar.Include ( Foo.GetType().ToString() );

I don't think I can do GetType() without an instance. Any ideas?

我认为没有实例我不能做 GetType() 。有任何想法吗?

采纳答案by Craig Stuntz

Include requires a property name, not a class name. Hence, it's the name of the property you want, not the name of its type. You can get that with reflection.

Include 需要一个属性名,而不是一个类名。因此,它是您想要的属性的名称,而不是其类型的名称。 你可以通过反射得到它

回答by Max Schmeling

You can't use .GetType()without an instance because GetTypeis a method.

.GetType()没有实例就不能使用,因为GetType是方法。

You can get the name from the type though like this:

您可以从类型中获取名称,如下所示:

typeof(Foo).Name

And as pointed out by Chris, if you need the assembly qualified name you can use

正如克里斯所指出的,如果您需要程序集限定名称,您可以使用

typeof(Foo).AssemblyQualifiedName

回答by Brian

typeof(Foo).ToString()

?

?

回答by Albino

You can use an DbSet<contact>instead of ObjectSet<contact>, so you can use lambda as a parameter, eg tblBar.Include(a => a.foo)

您可以使用DbSet<contact>代替ObjectSet<contact>,因此您可以使用 lambda 作为参数,例如tblBar.Include(a => a.foo)

回答by Glade Mellor

You could also do something like this:

你也可以做这样的事情:

Type CLASS = typeof(MyClass);

And then you can just access the name, namespace, etc.

然后你就可以访问名称、命名空间等。

 string CLASS_NAME = CLASS.Name;
 string NAMESPACE = CLASS.Namespace;

回答by Gustavo Mori

Another alternative using reflection, is to use the MethodBase class.

另一种使用反射的替代方法是使用 MethodBase 类。

In your example, you could add a static property (or method) that provides you with the info you want. Something like:

在您的示例中,您可以添加一个静态属性(或方法),为您提供所需的信息。就像是:

class Foo
{
    public static string ClassName
    {
        get
        {
            return MethodBase.GetCurrentMethod().DeclaringType.Name;
        }
    }
}

Which would allow you to use it without generating an instance of the type:

这将允许您在不生成该类型的实例的情况下使用它:

tblBar.Include(Foo.ClassName);

Which at runtime will give you:

这在运行时会给你:

tblBar.Include("Foo");