debugging - C# Debug Visualizer throug reflection: get value of property contained in complex object using Reflection -
suppose have list of 100 complex nested object, , want spool values of propery (identified pattern) of subobject.
ie:
lista_tipiclassifornitura[i].opzionetariffaria.codice
the immediate windows doesn't support loop, want create public static method this:
string spool(object c, string propertypath)
i'll call method follow:
spool(lista_tipiclassifornitura, "lista_tipiclassifornitura[#].opzionetariffaria.codice")
the procedure replace # 0, 1, 2, ecc , should access property "opzionetariffaria" , property of "codice" through memberinfo.
this example me? getting nested object property value using reflection
any suggestion?
yes! can done.
this how:
public class address { private string _addressline1; public string addressline1 { { return _addressline1; } set { _addressline1 = value; } } private string _addressline2; public string addressline2 { { return _addressline2; } set { _addressline2 = value; } } private string _city; public string city { { return _city; } set { _city = value; } } private string _state; public string state { { return _state; } set { _state = value; } } private string _zip; public string zip { { return _zip; } set { _zip = value; } } } public class employee { private string _firstname; public string firstname { { return _firstname; } set { _firstname = value; } } private string _middlename; public string middlename { { return _middlename; } set { _middlename = value; } } private string _lastname; public string lastname { { return _lastname; } set { _lastname = value; } } private address _employeeaddress; public address employeeaddress { { return _employeeaddress; } set { _employeeaddress = value; } } } class program { #region variabili globali static private contesto contesto; static private string file = configurationmanager.appsettings["pathfilelog"]; static eniloggermanager log; static private arraylist listamessaggi = null; #endregion public static object getpropertyvaluecomplete(object obj, string pattern) { pattern = "x.lista[#].opzione.codice"; list<string> s = new list<string>(pattern.split('.')); return getpropertyvaluecomplete(obj, s); return ""; } private static object getpropertyvaluecomplete(object obj, list<string> s) { s.removeat(0); if (s.count == 1) return getpropertyvalue(obj, s[0]); foreach (string s1 in s) { if(s1.contains("#")) { object propertyvalue = getpropertyvalue(obj, s1.substring(0, s1.indexof('['))); list<object> list = new list<object>(); for(int = 0; < 100; i++) { try { object value = getpropertyvalue(propertyvalue, "items", i); if (value != null) return getpropertyvaluecomplete(value, (string) s); } catch (exception) { break; } } } else return getpropertyvaluecomplete(obj, (string) s); } } public static object getpropertyvalue(object obj, string propertyname) { return getpropertyvalue(obj, propertyname, null); } public static object getpropertyvalue(object obj, string propertyname, int? index) { type objtype = obj.gettype(); propertyinfo prop = objtype.getproperty(propertyname); fieldinfo prop1 = objtype.getfield(propertyname); if (prop == null && prop1 == null) throw new exception(string.format("proprietà {0} non trovata nel tipo {1}", propertyname, objtype)); else if (prop != null) { object propertyvalue; if (index == null) propertyvalue = prop.getvalue(obj, null); else propertyvalue = prop.getvalue(obj, new object[] {index}); return propertyvalue; } else if(prop1 != null) return prop1.getvalue(obj); return null; }
Comments
Post a Comment