mercredi 9 septembre 2020

Reflection to get value of class property in class

I have 2 type of class.

public class Info
{
 public string name {get; set;}
 public Address address {get; set;}
}

public class Address
{
 public string addressInfo {get;set;}
 public string country {get;set;}
}

**

Edit:

Info info = new Info();
string subClass="address"; 
Type subType = info .GetType().GetProperty(subClass).PropertyType;
string val = typeof(subType).GetType().GetProperty("addressInfo").GetValue(data, null)?.ToString() ?? "";

This is what I want to achieve but I cannot code in this way, cause it got error. **

Now I want to get the value of addressInfo in Address class that inside Info class by Reflection dynamically. How can I achieve this? Thanks!!!





Aucun commentaire:

Enregistrer un commentaire