C# Using Reflection to Get a Generic Object's (and its Nested Objects) Properties?

In the System.Web. UI namespace there's a method to do that: DataBinder. Eval(source, expression).

UBER Legend...! – Jimbo May 26 '10 at 10:04 Good catch. The only "problem" I have with this one is that it depends on System.Web. Dll – Philippe Leybaert May 26 '10 at 10:07 Yeah it should probably go to another assembly – Guillaume86 May 26 '10 at 10:08 2 I was concerned about that - WHY is it in System.Web.

UI? – Jimbo May 26 '10 at 10:15 I guess they use it in asp. Net templated controls with directives, I'm not very familiar with non-web .

Net so I don't know if there's an alternative. – Guillaume86 May 26 '10 at 10:28.

Here's a simple method that uses recursion to solve your problem. It allows you to traverse an object graph by passing a "dotted" property name. It works with properties as well as fields.

Static class PropertyInspector { public static object GetObjectProperty(object item,string property) { if (item == null) return null; int dotIdx = property. IndexOf('. '); if (dotIdx > 0) { object obj = GetObjectProperty(item,property.

Substring(0,dotIdx)); return GetObjectProperty(obj,property. Substring(dotIdx+1)); } PropertyInfo propInfo = null; Type objectType = item.GetType(); while (propInfo == null && objectType! = null) { propInfo = objectType.

GetProperty(property, BindingFlags. Public | BindingFlags. Instance | BindingFlags.

DeclaredOnly); objectType = objectType. BaseType; } if (propInfo! = null) return propInfo.

GetValue(item, null); FieldInfo fieldInfo = item.GetType(). GetField(property, BindingFlags. Public | BindingFlags.

Instance); if (fieldInfo! = null) return fieldInfo. GetValue(item); return null; } } Example: class Person { public string Name { get; set; } public City City { get; set; } } class City { public string Name { get; set; } public string ZipCode { get; set; } } Person person = GetPerson(id); Console.

WriteLine("Person name = {0}", PropertyInspector. GetObjectProperty(person,"Name")); Console. WriteLine("Person city = {0}", PropertyInspector.

GetObjectProperty(person,"City. Name")).

Legend......... – Jimbo May 26 '10 at 10:00 Thanks alot for this, a great piece to learn from, however I have accepted an answer that is build into . NET – Jimbo May 26 '10 at 10:06.

I'm guessing you just need to break this down into a couple of steps rather than trying to do it all in one, something like: // First get the customer group Property... CustomerGroup customerGroup = getValue(customer, "Group"); // Then get the name of the group... if(customerGroup! = null) { string customerGroupName = getValue(customerGroup, "name"); }.

Since Group is the property of the customer, which itself hosts the property name, you have to go this way too. But since '. ' cant be part of the name of the property you can easyly use String.

Substring to remove the first property name from string and call your method recursively.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions