0

I m in middle of developing an application using third party api. They have provided a sample app to demonstrate the API. In the sample app there is a propertygrid which displays all the properties of the selected object.In the sample app I could see a propert lets say "X" but programmatically I am not able to access it by putting dot(.) after the object variable. I also used reflection to get the properties but no success. Someone suggested that it might be a dynamic property. I found no way to check if the given object or any of the memberof that object is dynamic?

Please suggest a way to access that property.

PropertGrid control is able to display the above mentioned property and also allows to change the value but this property is not accessible pro-grammatically.

himanshu
  • 417
  • 5
  • 18

1 Answers1

1

The way to solve this problem is to look directly at the generated IL of the type that you know has dynamic properties. There you will find that such a property is represented simply as an object type, but also all of its components are decorated with the DynamicAttribute attribute.

This attribute is put in place by the compiler itself, and can not be used by the developer. Thus, the only thing you need to do is to check if a property is decorated with the DynamicAttribute attribute.

To see this, take a look at the following IL code for the IamDynamic property's Get accessor method, which we will use later for testing purposes.

.method public hidebysig specialname instance object 
      get_IamDynamic() cil managed

.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) 
.param [0]
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 ) 

.maxstack  1
.locals init (object V_0)
IL_0000:  ldarg.0
IL_0001:  ldfld      object Testing.TestClass::'<IamDynamic>k__BackingField'
IL_0006:  stloc.0
IL_0007:  br.s       IL_0009
IL_0009:  ldloc.0
IL_000a:  ret

You can easily notice the line where the DynamicAttribute is used, indicating that this is a dynamic declaration. We can now discover all the dynamic properties by using the following extension method.

public static class DynamicExtension
{
    public static void GetDynamicProperties(this Type source)
    {
        source.GetProperties()
              .Where(x => x.GetCustomAttributes().OfType<DynamicAttribute>().Any())
              .ToList()
              .ForEach(x => Console.WriteLine(x.Name));
    }
}

This class, that was mentioned above will be tested for dynamic properties.

class TestClass
{
    public dynamic IamDynamic { get; set; }        
    public object IamNotDynamic { get; set; }
    public dynamic IamAlsoDynamic { get; set; }
}

Once you execute the following lines of code, you will see that only the two dynamic properties are displayed.

class Program
{
    static void Main()
    {
        typeof(TestClass).GetDynamicProperties();

        Console.Read();
    }
}
Mario Stopfer
  • 541
  • 3
  • 8