如何根据其属性获取类名称
是否可以根据其属性名称获取类名称。如果是的话又如何呢?任何人都可以帮助我。
Is it possible to get the class name given the name of its property. If it is then how? Anyone can help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这不可能直接实现。您必须通过反射搜索所有类,并且必须在每个类中查找特定属性
I don't think it is directly possible. You will have to search all the classes via reflection and you will have to look for that particular property in each class
如果您有 PropertyInfo 那么您可以使用 < href="http://msdn.microsoft.com/en-us/library/system.reflection.memberinfo.declaringtype.aspx" rel="nofollow">DeclaringType 属性。如果你只有一些字符串,你就无法从中得到很多东西。您必须首先获取属性,但要获取属性,您首先需要获取声明类,因此您已经知道声明类。
If you have a PropertyInfo then you could use the DeclaringType property. If you only have some string you cannot get much from it. You will have to first get the property but to get the property you first need to get the declaring class, so you already know the declaring class.
反思是个好主意尝试阅读位于 MSDN.com 的这篇文章http://msdn.microsoft.com/en-us/library/ms173183(VS.80).aspx
Reflections are good idea Try to read this article located at MSDN.com http://msdn.microsoft.com/en-us/library/ms173183(VS.80).aspx
以下是 mscorlib.dll 的示例。它查找具有名为“Capacity”属性的所有类。
或者,使用 LINQ:
输出如下:
Here is an example for mscorlib.dll. It finds all classes which have property named "Capacity".
Or, using LINQ:
Here is the output: