C# 使用反射获取类型

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

Get type using reflection

c#reflectiontypes

提问by Martin Ch

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.

我正在尝试通过使用反射来获取我的类的属性类型,但它返回我唯一的 RuntimePropertyInfo - 作为类型的名称。

I have object MyObject actualData - it contains property - "name" as string and "Item" as my type DatumType

我有对象 MyObject actualData - 它包含属性 - “name”作为字符串和“Item”作为我的类型 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:

当我调试时,我可以看到,actualData 有 2 个属性,第一个是字符串类型,第二个是 DatumType,但是当我使用它时:

string typeName = actualData.getType().getProperty("Item").getType().Name- it returns me RuntimePropertyInfo, not DatumType

string typeName = actualData.getType().getProperty("Item").getType().Name- 它返回我 RuntimePropertyInfo,而不是 DatumType

Can you see what am I doing wrong? I am using C# - .Net 4.0. Thanks a lot!

你能看出我做错了什么吗?我正在使用 C# - .Net 4.0。非常感谢!

采纳答案by C.Evenhuis

You're getting the type of the PropertyInfoobject getProperty()returns. Try

您正在获取PropertyInfo对象getProperty()返回的类型。尝试

string typeName = actualData.getType().getProperty("Item").PropertyType.Name;

If you want the type of the value currently assigned to the object via the PropertyInfoobject, you could call:

如果您想要通过PropertyInfo对象当前分配给对象的值的类型,您可以调用:

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;

回答by Wiktor Zychla

The

actualData.getType().getProperty("Item")

retrieves something of type PropertyInfo.

检索类型的东西PropertyInfo

If you then ask for its type:

如果你然后询问它的类型:

actualData.getType().getProperty("Item").getType()

you get exactly what you observe you get.

你得到的正是你所观察到的。

I suspect this last getType()is not necessary then.

我怀疑这最后getType()是没有必要的。

Edit: someone has downvoted this answer which is unfair imho. The question is "Can you see what am I doing wrong?" and the answer of being one getTypetoo far is a correct one. Finding PropertyTypein PropertyInfois then easy if the asking person knows what he is doing wrong.

编辑:有人低估了这个答案,恕我直言不公平。问题是“你能看出我做错了什么吗?” getType太远的答案是正确的。发现PropertyTypePropertyInfo是那么容易,如果在询问的人都知道他在做什么错。

To the person who downvoted this answer: please at least leave a comment next time you downvote something. Stackoverflow makes sense only if we learn from each other, not just bash everyone around.

对于否决此答案的人:请至少在下次您否决某些内容时发表评论。Stackoverflow 只有在我们相互学习的情况下才有意义,而不仅仅是抨击周围的每个人。

回答by Arnaud F.

GetType()return always the type of the current object, not the pointed object.

GetType()始终返回当前对象的类型,而不是指向的对象。

In your case, consider using string typeName = actualData.getType().getProperty("Item").PropertyType.Name

在您的情况下,请考虑使用 string typeName = actualData.getType().getProperty("Item").PropertyType.Name