[Mono-list] String Field Value from Metadata

Hamza Karamali karamal@eecg.toronto.edu
Sat, 27 Sep 2003 14:57:31 -0400 (EDT)


Hi,

A while back, I asked about how to check the attributes of a method from
within mono, and got a very helpful response from Paolo (reproduced
below).  I had a follow up question: how can I use the MonoCustomAttrInfo
struct to figure out the values of certain public fields (e.g. string
fields) defined inside the attribute?

One way, I guess would be to read the constructor parameters from the
metadata (as done in create_custom_attr in reflection.c), but it would
probably be quicker to read the information directly from internal mono
structures since the attribute object has already been created for the
method.  How do I do this?

Thanks for your help,

Hamza.

-----------------------------------------------


[Mono-list] Method Attributes 
Paolo Molaro lupus@ximian.com 
Mon, 18 Aug 2003 18:24:33 +0200 

Previous message: [Mono-list] Method Attributes 
Next message: [Mono-list] Nant 0.8.2 question 
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] 

--------------------------------------------------------------------------------

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 {...}
and
	[MySpecialAttribute]
	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
			break;
		}
	}
	mono_custom_attrs_free (attributes);

lupus

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