[Mono-list] Method Attributes

Paolo Molaro lupus@ximian.com
Mon, 18 Aug 2003 18:24:33 +0200

On 08/18/03 Hamza Karamali wrote:
> Thanks for the info.  However, I want to do this within the mono C code
> (I'm a Master's student trying to hack mono for research) -- not from C#.
> While I am compiling a method (in mini_method_compile, for example), I'd
> like to check the method attributes by referencing its metadata directly.
> Is there any easy way to do this in mono?  Or do I have to read the CLI
> specs and then use the functions in metadata.c in an appropriate manner?

You can call:
	MonoCustomAttrInfo* attributes = mono_custom_attrs_from_method (method);

It returns NULL if there are no attributes defined for the method.
MonoCustomAttrInfo is defined in reflection.h. You can run a loop
over the attrs array elements, checking whether the ctor belongs
to the attribute class that interests you. For example, given:

	class MySpecialAttribute: Attribute {...}
	public void method (...) {...}

When compiling method, you can do something like (untested):

	// my_special_attr should be cached...
	MonoClass *my_special_attr = mono_class_from_name (image, "Hack", "MySpecialAttribute");
	MonoCustomAttrInfo* attributes = mono_custom_attrs_from_method (method);
	if (!attributes)
		return; // no special processing requested
	for (i = 0; i < attributes->num_attrs; ++i) {
		if (attributes->attrs [i].ctor->klass == my_special_attr) {
			// do something special with method
	mono_custom_attrs_free (attributes);


lupus@debian.org                                     debian/rules
lupus@ximian.com                             Monkeys do it better