获取类的名称在C#中的字符串 [英] Get the name of a class as a string in C#

查看:108
本文介绍了获取类的名称在C#中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法把一个类名,并将其转换为C#中的字符串?

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

由于实体框架的一部分,在.include方法需要在字符串中的点分隔列表加入执行查询时。我有什么,我想加入的类模型,并为重构和未来的代码维护的原因,我希望能够有引用这个类时,编译时的安全性。

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.

因此,有没有办法,我能做到这一点:

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

class Foo
{
}

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



我不认为没有一个实例我可以做的GetType()。任何想法?

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

推荐答案

包含需要一个属性名,而不是一个类名。因此,这是你想要的属性的名称,而不是其类型的名称。 你可以得到与反思

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.

这篇关于获取类的名称在C#中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆