Get type using reflection

后端 未结 3 1192
日久生厌
日久生厌 2021-01-04 23:39

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 actua

相关标签:
3条回答
  • 2021-01-04 23:56

    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;
    
    0 讨论(0)
  • 2021-01-05 00:18

    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

    0 讨论(0)
  • 2021-01-05 00:23

    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.

    0 讨论(0)
提交回复
热议问题