12

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!

Martin Ch
  • 1,337
  • 4
  • 21
  • 42
  • C# is case sensitive. What you've included in your question cannot be what you've actually tried. –  Mar 18 '12 at 12:03
  • are you kidding me? :D no thats for sure not the problem – Martin Ch Mar 18 '12 at 12:09
  • 1
    I didn't say that was the problem. Notice how I included this as a comment rather than an answer? But if you don't include the code you actually tried, we're left guessing whether what you did try merely had the capitalisation fixed, or had other changes as well. –  Mar 18 '12 at 12:24
  • 2
    if there was any problem with case, dont you think, that compiler would told me so? Or If it would be problem with property name, it returned me null exception – Martin Ch Mar 18 '12 at 12:27
  • 3
    How should I know what the compiler told you, if you don't include the code that you fed the compiler? –  Mar 18 '12 at 12:28

3 Answers3

16

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;
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • it returns me only - "Object" as a name of type – Martin Ch Mar 18 '12 at 12:03
  • 1
    The property may be defined as `object`, but have a `DatumType` value assigned to it. GetProperty only returns the type defined, not the type of the current value. Any type can be assigned to a property of type `object`. You should use the `PropertyInfo` and call `GetValue()` on it to get the type of the assigned value. – C.Evenhuis Mar 18 '12 at 12:05
  • so is there any way how to get this type? Because if I look in debugging on actualData - I can see that Item is DatumType, only reflection returns me something else – Martin Ch Mar 18 '12 at 12:07
  • thanks, it works great! :) I cant call actualData.Item.GetType().Name, because actualData can be instance of another class, that doesnt contain Item property, so I have to do it with reflection, but thanks again! – Martin Ch Mar 18 '12 at 12:14
2

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.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
1

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

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

Arnaud F.
  • 8,252
  • 11
  • 53
  • 102
  • Doesn't work. For example, if the property is a Nullable(Of Date), returns "Nullable`1". Only works for string properties. – Pachanka Mar 15 '16 at 10:43