使用反射型获得 [英] Get type using reflection
问题描述
我通过使用反射,但其回我只RuntimePropertyInfo想我的类属性的get类型 - 作为一个类型的名称
I am trying get type of property of my class by using of reflection but its returning my only RuntimePropertyInfo - as a name of a type.
我有对象为MyObject actualData - 它包含属性 - 名称作为字符串和项是我喜欢的类型DatumType
I have object MyObject actualData - it contains property - "name" as string and "Item" as my type DatumType
当我调试,我可以看到,actualData有2个属性,第一个是字符串类型,第二个是DatumType,但是当我使用这样的:
When I am debugging I can see, that actualData has 2 properties, first one is type of string and second one is DatumType, but when I use this:
字符串的typeName = actualData.getType()的getProperty(项目).getType()名称
- 它返回我RuntimePropertyInfo,不DatumType
string typeName = actualData.getType().getProperty("Item").getType().Name
- it returns me RuntimePropertyInfo, not DatumType
您可以看到我在做什么错了?我使用C# - .NET 4.0。
非常感谢!
Can you see what am I doing wrong? I am using C# - .Net 4.0. Thanks a lot!
推荐答案
你得到的PropertyInfo
对象<类型code>的getProperty()的回报。尝试
You're getting the type of the PropertyInfo
object getProperty()
returns. Try
string typeName = actualData.getType().getProperty("Item").PropertyType.Name;
如果您想现在通过分配给该对象的值的类型的PropertyInfo
的对象,你可以调用:
If you want the type of the value currently assigned to the object via the PropertyInfo
object, you could call:
string typeName = actualData.getType().getProperty("Item").GetValue(actualData, null).GetType().Name;
但在这种情况下,你也可以简单地调用:
But in that case you could also simply call:
string typeName = actualData.Item.GetType().Name;
这篇关于使用反射型获得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!