mercredi 25 novembre 2015

How can you access static properties dynamically from an object of type 'Type'?

In one of our assemblies, we have a namespace which is full of MarkupExtension subclasses, each which expose a static property called Geometry, like so...

public class SomeClass : MarkupExtension
{
    public static Geometry Geometry = Geometry.Parse("<bla>");

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Geometry;
    }
}

In code, we're using Assembly.GetTypes() to find them (they're all in a particular namespace) and I'm trying to retrieve that static variable. That's where I'm hitting a wall.

We can't get it from creating an instance (stored in a dynamic) since you can't access static's that way. You also can't simply cast the type to dynamic and try to call ((dynamic)type).Geometry.

What would be the most effective way to retrieve that static?

Note: It is assumed all such classes will have a static property called 'Geometry'. If it weren't in a static, we would have exposed it via a base property.





Aucun commentaire:

Enregistrer un commentaire