1

What is the good practice on passing values(datatype and fieldname) on entity.GetAttributeValue<T>(fieldname). Currently I see something like : entity.GetAttributeValue<string>(address). Is there any better way of doing this without specifically pass those prameters(string,address) on .GetAttributeValue method?

Currently I have a code like this:

var prop = typeof(PersonAddress).GetProperties(); 
string address;
foreach(var p in prop)
{
  address = entity.GetAttributeValue<p.PropertyType>(p.name)
}

but the p.PropertyType is not accepted as a reference type in GetAttributeValue<T>

1 Answers1

0

You need to invoke GetAttributeValue generic method by using reflection and then construct it by supplying the type:

var getAttributeValueMethod = typeof(Microsoft.Xrm.Sdk.Entity).GetMethod("GetAttributeValue");
foreach (var property in typeof (PersonAddress).GetProperties())
{
    var generic = getAttributeValueMethod.MakeGenericMethod(property.PropertyType);
    var attributeValue = generic.Invoke(entity,
                                        new object[] {
                                        property.Name.ToLowerInvariant()});
}

Credits: Jon Skeet

Community
  • 1
  • 1
dynamicallyCRM
  • 2,980
  • 11
  • 16