Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the Value of a property from PropertyInfo?

I've got an object that has a collection of properties. When I get the specific entity I can see the field I'm looking for (opportunityid) and that it's Value attribute is the Guid for this opportunity. This is the value I want, but it won't always be for an opportunity, and therefore I can't always look at opportunityid, so I need to get the field based on input supplied by the user.

My code so far is:

Guid attrGuid = new Guid();  BusinessEntityCollection members = CrmWebService.RetrieveMultiple(query);  if (members.BusinessEntities.Length > 0) {     try     {         dynamic attr = members.BusinessEntities[0];         //Get collection of opportunity properties         System.Reflection.PropertyInfo[] Props = attr.GetType().GetProperties();         System.Reflection.PropertyInfo info = Props.FirstOrDefault(x => x.Name == GuidAttributeName);         attrGuid = info.PropertyType.GUID; //doesn't work.     }     catch (Exception ex)     {         throw new Exception("An error occurred when retrieving the value for " + attributeName + ". Error: " + ex.Message);     } } 

The dynamic attr contains the field I'm looking for (in this case opportunityid), which in turn contains a value field, which is the correct Guid. However, when I get the PropertyInfo info (opportunityid) it no longer has a Value attribute. I tried looking at the PropertyType.GUID but this doesn't return the correct Guid. How can I get the value for this property?

like image 885
sr28 Avatar asked Oct 15 '14 12:10

sr28


People also ask

What does GetValue return c#?

GetValue(Object) Returns the property value of a specified object.

How do you set property value?

To set the value of an indexed property, call the SetValue(Object, Object, Object[]) overload. If the property type of this PropertyInfo object is a value type and value is null , the property will be set to the default value for that type.

What is PropertyInfo?

< Previous Next > The PropertyInfo class discovers the attributes of a property and provides access to property metadata. The PropertyInfo class is very similar to the FieldInfo class and also contains the ability to set the value of the property on an instance.


2 Answers

Unless the property is static, it is not enough to get a PropertyInfo object to get a value of a property. When you write "plain" C# and you need to get a value of some property, say, MyProperty, you write this:

var val = obj.MyProperty; 

You supply two things - the property name (i.e. what to get) and the object (i.e. from where to get it).

PropertyInfo represents the "what". You need to specify "from where" separately. When you call

var val = info.GetValue(obj); 

you pass the "from where" to the PropertyInfo, letting it extract the value of the property from the object for you.

Note: prior to .NET 4.5 you need to pass null as a second argument:

var val = info.GetValue(obj, null); 
like image 80
Sergey Kalinichenko Avatar answered Oct 07 '22 17:10

Sergey Kalinichenko


If the name of the property is changing, you should use GetValue:

info.GetValue(attr, null); 

The last attribute of that method can be null, since it is the index value, and that is only needed when accessing arrays, like Value[1,2].

If you know the name of the attribute on beforehand, you could use the dynamic behavior of it: you can call the property without the need of doing reflection yourself:

var x = attr.Guid; 
like image 36
Patrick Hofman Avatar answered Oct 07 '22 17:10

Patrick Hofman