Hi,

On Sat, Apr 23, 2011 at 9:30 PM, Hendry Luk <[email protected]> wrote:
> Hi,
> Is there any easy way in cecil to get a certain custom-attributes (of a
> specific type) from a method, and execute a virtual method on it?
> Just like it can easily be done using a basic System.Reflection.
>
> The only way i can think of is to manually load the assembly containing that
> attributes (that I've discovered using cecil), then instantiate the
> attribute using the constructor-arguments information (from cecil), and then
> copy across all property-values (from cecil). Once you got the attribute
> instance, I can then execute the method. It seems to be a lot of work for
> such a _very_ common thing.

.net attributes have been designed to be instantiated at runtime.
Cecil is not a runtime feature, it just gives you an object model
which maps to the serialized metadata. Cecil has obviously no way to
instantiate an object based on this metadata.

If you want to instantiate attributes, you're looking at the wrong tool.

> Oh while I'm here, is there also an easy way to do
> type.IsAssignableFrom(type) on cecil? At the moment i'm recursively checking
> all the base-classes within its ancestry line, as well as their interfaces,
> but it feels very inefficient, considering it's also a very common thing
> (checking for types assignability is probably far more common than checking
> for an exact equality). Is there a better way to do this?

Not really, I don't think I have such a method around.

Jb

-- 
--
mono-cecil

Reply via email to