Grant Surridge
asked on
C# - Get value of property from an object dynamically
Hi
I have a class called "shipment" that contains a lot of properties, such as Address1, Address2, Address3, PostCode, OrderNumber etc etc etc. This object gets passed to a web service, it creates the orders, then it sends an email. However, I want to make the email dynamic. For instance: for customer A, I might want to show Address1 and Address2, Customer B might want to show OrderNumber and Post Code
What I want to do, is have a database table that is called, and then says which properties of the object need to go onto the email. I think I need to use reflection, but I don't really understand it
So, in the db table will be a value of shipment.Address1 and shipment.Address2 which will be pulled out as strings, that then needs converting to take the value of the actual shipment.Address1 and 2
Does this make sense ? Hopefully have explained myself ok !
Thanks in advance
I have a class called "shipment" that contains a lot of properties, such as Address1, Address2, Address3, PostCode, OrderNumber etc etc etc. This object gets passed to a web service, it creates the orders, then it sends an email. However, I want to make the email dynamic. For instance: for customer A, I might want to show Address1 and Address2, Customer B might want to show OrderNumber and Post Code
What I want to do, is have a database table that is called, and then says which properties of the object need to go onto the email. I think I need to use reflection, but I don't really understand it
So, in the db table will be a value of shipment.Address1 and shipment.Address2 which will be pulled out as strings, that then needs converting to take the value of the actual shipment.Address1 and 2
Does this make sense ? Hopefully have explained myself ok !
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
I need to develop this a little more (will open a new question if you wish)
I have used your code as follows:
I have the code inside a loop:
foreach (Order order1 in file.Orders)
and then using some code I used before, I tried
Type type = Type.GetType("order1.Heade r"); - but this resolves to null
If I tried and use the full path Integration.Types.Order then it works, but this won't relate specifically to the instance of ORder1 inside the loop
Does this make sense ?
Thanks
I need to develop this a little more (will open a new question if you wish)
I have used your code as follows:
string propName = field;
System.Reflection.PropertyInfo pi = order.Header.GetType().GetProperty(propName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
if (pi != null)
{
string value = pi.GetValue(order.Header, null).ToString();
//Console.WriteLine(propName + " = " + value);
strEmailLine += value + "~";
}
I have the code inside a loop:
foreach (Order order1 in file.Orders)
and then using some code I used before, I tried
Type type = Type.GetType("order1.Heade
If I tried and use the full path Integration.Types.Order then it works, but this won't relate specifically to the instance of ORder1 inside the loop
Does this make sense ?
Thanks
Are you trying to get the type of "order1", or "Header"?
At any rate, use GetType() against that specific member like this:
Type type = order1.GetType();
or this:
Type type = order1.Header.GetType();
At any rate, use GetType() against that specific member like this:
Type type = order1.GetType();
or this:
Type type = order1.Header.GetType();
ASKER
Hi
Am trying to get it for both. The problem is that I need to do it dynamically, so order1 or order1.Header will actually be a string value. If I do the below, type is null, which implies it can't find order1.Header when passed as a string
Am trying to get it for both. The problem is that I need to do it dynamically, so order1 or order1.Header will actually be a string value. If I do the below, type is null, which implies it can't find order1.Header when passed as a string
foreach (Order order1 in file.Orders)
{
string propName = "SHIPMENT_ID";
Type type = Type.GetType("Order1.Header");
System.Reflection.PropertyInfo pi = type.GetType().GetProperty(propName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
if (pi != null)
{
string value = pi.GetValue(order.Header, null).ToString();
Console.WriteLine(value);
}
}
ASKER
Hi
In the above example, where i have done Type.GetType("Order1.Heade r") - this is a mistake, it should be all lower case, and that still doesn't work
Scott
In the above example, where i have done Type.GetType("Order1.Heade
Scott
Is "Header" an actual TYPE (class/structure), or is it simply the name of a field/property?
I'm thinking the latter. If this is the case then you'd have to PARSE your string to separate into the Type portion and the FIELD portion. To get a type from a string, though, I think you'd need the entire FULLY QUALIFIED name like:
Type T = Type.GetType("WindowsForms Applicatio n1.Order") ;
Afterwards, you'd use GetField() or GetProperty(), and pass them "Header" as I demonstrated earlier.
I'm thinking the latter. If this is the case then you'd have to PARSE your string to separate into the Type portion and the FIELD portion. To get a type from a string, though, I think you'd need the entire FULLY QUALIFIED name like:
Type T = Type.GetType("WindowsForms
Afterwards, you'd use GetField() or GetProperty(), and pass them "Header" as I demonstrated earlier.
ASKER