none
c# reflection can't get values RRS feed

  • Question

  • I have a class :

    public class Test1    {
                   public string age { get; set; }
               public int? distance { get; set; }
           }

    I want to loop over the property and values, but I get only the names :

    Test1 t=new Test1();
    t.age="333";
    t.distance=4;
    
    PropertyInfo[] props = t.GetType().GetProperties();
    foreach (PropertyInfo obj in props)
    {
    Type type = obj.GetType();
                                                                                  PropertyInfo info = type.GetProperty(obj.Name);
                                            if (info == null) continue; 
    
                                            object obj2 = info.GetValue(obj, null);
    }

    I see the property name but not the value, what do I do wrong?

    Friday, September 28, 2018 1:53 PM

All replies

  • You're using the wrong object. You get the type of t which would be Test1. You then start enumerating its properties. But where things go wrong is that you then fetch the type of obj (which is the PropertyInfo instance) so type (inside the loop) is PropertyInfo. Then you are trying to find the property (on Test1) but using the PropertyInfo type.

    var t = new Test1() { age = "333", distance = 4 };
    
    var props = t.GetType().GetProperties();
    foreach (var prop in props)
    {
       //Get the value
       var propValue = prop.GetValue(t);
    };

    Using meaningful variable names here will make your code a lot more readable and make it obvious what is going wrong.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, September 28, 2018 2:06 PM
    Moderator
  • Hello,

    PropertyInfo info = type.GetProperty(obj.Name);


    GetProperty() work with name of the property:

    Type type = typeof(Test1);

    PropertyInfo info = type.GetProperty("distance");

    Object not needed as you already have a Type.


    Sincerely, Highly skilled coding monkey.


    Friday, September 28, 2018 2:23 PM
  • hello,

    exactly to get property info all you need is the type, but to get properties values you need the instance, 

    Type type = typeof(Test1);
    PropertyInfo info = type.GetProperty("distance");
    
    var instance = new Test1();
    
    // the SetValue and GetValue methods dependes on object 
    // instance since this instance is the properties values holder, 
    // memory pointer in Cpp speaking 
    
    info.SetValue(instance, 1000);
    var propertyValue = info.GetValue(instance);

    but here since the requirement is to loop through get all properties values through reflection then the more appropriate method to use is GetProperties which gives already the possibility to get properties using a predicate (lambda expression) or specifying BindingFlags etc ...

    then a recommendation is to follow C# coding conventions, which help to keep code more maintainable and more readable.

    Good Coding;


    Friday, September 28, 2018 6:57 PM
  • Hi,

    Is there any update? do you resolve the issue?

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 1, 2018 8:38 AM
    Moderator