[Mono-dev] embedding C, typeof operator

Paolo Molaro lupus at ximian.com
Wed Mar 22 13:35:43 EST 2006

On 03/22/06 vavra at software602.cz wrote:
> Hello,
>  I'd like in C to find out wheter some C# class has some type.
> For example:
>   class C is descendat of class B
>   class B is descendat of class A
>   I have instace ic of class C
>   I'd to test wheter ic is type of A.
> Is there other way than using  mono_class_get_parent?:
>        MonoClass *parent=ic_klass;
>        while (parent) {
>                if (strcmp(mono_type_full_name(mono_class_get_type(parent)), "A") {
>                    printf("ic is typeof A");
>                    break;
>                }
>                parent =  mono_class_get_parent(parent);
>        }

Using string compares to compare types is wrong, both in C and in C#
code. With the embedding interface, you can loop with
mono_clasS_get_parent() and compare the MonoClass pointers directly, or
you can use the provided API:
	gboolean mono_class_is_subclass_of (MonoClass* a, MonoClass *b, gboolean check_interfaces);

It will return TRUE if b is a supertype of a. You can just pass FALSE as
the last argument.


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

More information about the Mono-devel-list mailing list