none
Access Base Class Property values through reflection RRS feed

  • Question

  • Hi,

    I am trying to access the property values assigned to base class property through reflection.
    Tried with all permutations and combinations with different values (check "Actual line of error"), but tired.
    Kindly suggest me some possible solution.

    I have put the necessery code and error messages/information.

    Error: Object does not match target type
    Trace:
     at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at System.Reflection.RuntimeMethodInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
     at System.Reflection.RuntimeMethodInfo.GetValue(Object obj, Object[] index)

    /* Main  class implementation	*/
    List<AcceableClass> accessableClassList = new List<AcceableClass>();
    System.Reflection.PropertyInfo[] properties = typeof(T). BaseType.GetProperties();
    foreach(System.Reflection.PropertyInfo bp in properties)
    {
    	if(bp.Name == "BClProperty")
    	{
    		object[] index = {0};
    		bp.GetValues(accessableClassList, index); // Actual line of error
    	}
    }
    public class AcceableClass
    {
    	public string Key {get; set;}
    	public int Expected {get; set;}
    	public int Actual {get; set;}
    }
    public class BaseClass
    {
    	public List<AcceableClass> BClProperty {get; set;}
    }
    public class BaseClass
    {
    	public int MyProperty {get; set;}
    }

    Monday, October 13, 2014 2:09 PM

Answers

  • Hello Jayanth,

    >>I am trying to access the property values assigned to base class property through reflection.

    From your current description, I am not quite sure if you want to access  “Key”,” Expected” and “Actual” in class AcceableClass  through reflecting the BClProperty property in BaseClass class. If it is, I copied your code and it thrown the exception you said, and modified it a little so that it could run without throwing any error:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                BaseClass baseClass = new BaseClass();
    
                baseClass.BClProperty.Add(new AcceableClass() { Key = "1", Actual = 1, Expected = 1 });
    
                System.Reflection.PropertyInfo[] properties = typeof(BaseClass).GetProperties();
    
                foreach (System.Reflection.PropertyInfo bp in properties)
    
                {
    
                    if (bp.Name == "BClProperty")
    
                    {
    
                        var result = bp.GetValue(baseClass); // Actual line of error
    
                    }
    
                }
    
            }
    
    }
    
    public class AcceableClass
    
        {
    
            public string Key { get; set; }
    
            public int Expected { get; set; }
    
            public int Actual { get; set; }
    
        }
    
        public class BaseClass
    
        {
    
            public BaseClass()
    
            {
    
                BClProperty = new List<AcceableClass>();
    
            }
    
            public List<AcceableClass> BClProperty { get; set; }
    
        }
    

    Please try it and if it does not work for you, please let me know.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 14, 2014 9:10 AM
    Moderator