Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get type using reflection

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.

I have object MyObject actualData - it contains property - "name" as string and "Item" as my type 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:

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

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

like image 349
Martin Ch Avatar asked Mar 18 '12 11:03

Martin Ch


People also ask

Does GetType use reflection?

The most basic way to do reflection is to use the GetType() method, but we can also use reflection to get information about methods, constructors, properties, and more.

How do I get a property type from PropertyInfo?

You can do this by getting an array of all properties from the Type. GetProperties method and then iterating the elements in the array, or you can retrieve the PropertyInfo object that represents the property directly by calling the Type. GetProperty method and specifying the property name.

How do you get type object from assemblies that are already loaded?

Use Type. GetType to get the Type objects from an assembly that is already loaded.

What is type GetType in C#?

GetType(String, Boolean) Gets the Type with the specified name, performing a case-sensitive search and specifying whether to throw an exception if the type is not found. public: static Type ^ GetType(System::String ^ typeName, bool throwOnError); C# Copy.


2 Answers

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

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

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;
like image 199
C.Evenhuis Avatar answered Sep 21 '22 17:09

C.Evenhuis


The

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

retrieves something of type 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.

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 getType too far is a correct one. Finding PropertyType in PropertyInfo is then easy if the asking person knows what he is doing wrong.

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.

like image 40
Wiktor Zychla Avatar answered Sep 19 '22 17:09

Wiktor Zychla