Tue Jul 27 15:47:17 CEST 2004 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / class.c
index 5a75563186feadfdc21ffdf0b4301c51523856a3..6283d3c1531763aced284694dcbdeeb7715e713b 100644 (file)
 #include <stdlib.h>
 #include <signal.h>
 #include <mono/metadata/image.h>
+#include <mono/metadata/assembly.h>
 #include <mono/metadata/cil-coff.h>
 #include <mono/metadata/metadata.h>
+#include <mono/metadata/metadata-internals.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/tokentype.h>
-#include <mono/metadata/class.h>
+#include <mono/metadata/class-internals.h>
 #include <mono/metadata/object.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/mono-endian.h>
 #include <mono/metadata/reflection.h>
 #include <mono/os/gc_wrapper.h>
 
-/*
- * Uncomment this to enable GC aware auto layout: in this mode, reference
- * fields are grouped together inside objects, increasing collector 
- * performance.
- * Requires that all classes whose layout is known to native code be annotated
- * with [StructLayout (LayoutKind.Sequential)]
- */
-//#define GC_AWARE_AUTO_LAYOUT
-
-#define CSIZE(x) (sizeof (x) / 4)
-
 MonoStats mono_stats;
 
 gboolean mono_print_vtable = FALSE;
 
 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
 
+void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
 
 MonoClass *
@@ -66,16 +58,16 @@ mono_class_from_typeref (MonoImage *image, guint32 type_token)
        name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
        nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
        
-       idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
-       switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
-       case RESOLTION_SCOPE_MODULE:
+       idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
+       switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
+       case MONO_RESOLTION_SCOPE_MODULE:
                if (!idx)
                        g_error ("null ResolutionScope not yet handled");
                /* a typedef in disguise */
                return mono_class_from_name (image, nspace, name);
-       case RESOLTION_SCOPE_MODULEREF:
+       case MONO_RESOLTION_SCOPE_MODULEREF:
                return mono_class_from_name (image->modules [idx - 1], nspace, name);
-       case RESOLTION_SCOPE_TYPEREF: {
+       case MONO_RESOLTION_SCOPE_TYPEREF: {
                MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
                GList *tmp;
                mono_class_init (enclosing);
@@ -87,7 +79,7 @@ mono_class_from_typeref (MonoImage *image, guint32 type_token)
                g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
                return NULL;
        }
-       case RESOLTION_SCOPE_ASSEMBLYREF:
+       case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
                break;
        }
 
@@ -96,6 +88,9 @@ mono_class_from_typeref (MonoImage *image, guint32 type_token)
                /* 
                 * detected a reference to mscorlib, we simply return a reference to a dummy 
                 * until we have a better solution.
+                * 
+                * once a better solution is in place, the System.MonoDummy
+                * class should be removed from CVS.
                 */
                fprintf(stderr, "Sending dummy where %s.%s expected\n", mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]), mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME])); 
                
@@ -114,15 +109,17 @@ mono_class_from_typeref (MonoImage *image, guint32 type_token)
 }
 
 static MonoType*
-dup_type (MonoType* t)
+dup_type (MonoType* t, const MonoType *original)
 {
        MonoType *r = g_new0 (MonoType, 1);
        *r = *t;
+       r->attrs = original->attrs;
+       mono_stats.generics_metadata_size += sizeof (MonoType);
        return r;
 }
 
 static void
-mono_type_get_name_recurse (MonoType *type, GString *str)
+mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed)
 {
        MonoClass *klass;
        
@@ -130,7 +127,7 @@ mono_type_get_name_recurse (MonoType *type, GString *str)
        case MONO_TYPE_ARRAY: {
                int i, rank = type->data.array->rank;
 
-               mono_type_get_name_recurse (&type->data.array->eklass->byval_arg, str);
+               mono_type_get_name_recurse (&type->data.array->eklass->byval_arg, str, FALSE);
                g_string_append_c (str, '[');
                for (i = 1; i < rank; i++)
                        g_string_append_c (str, ',');
@@ -138,17 +135,17 @@ mono_type_get_name_recurse (MonoType *type, GString *str)
                break;
        }
        case MONO_TYPE_SZARRAY:
-               mono_type_get_name_recurse (&type->data.klass->byval_arg, str);
+               mono_type_get_name_recurse (&type->data.klass->byval_arg, str, FALSE);
                g_string_append (str, "[]");
                break;
        case MONO_TYPE_PTR:
-               mono_type_get_name_recurse (type->data.type, str);
+               mono_type_get_name_recurse (type->data.type, str, FALSE);
                g_string_append_c (str, '*');
                break;
        default:
                klass = mono_class_from_mono_type (type);
                if (klass->nested_in) {
-                       mono_type_get_name_recurse (&klass->nested_in->byval_arg, str);
+                       mono_type_get_name_recurse (&klass->nested_in->byval_arg, str, TRUE);
                        g_string_append_c (str, '+');
                }
                if (*klass->name_space) {
@@ -156,6 +153,30 @@ mono_type_get_name_recurse (MonoType *type, GString *str)
                        g_string_append_c (str, '.');
                }
                g_string_append (str, klass->name);
+               if (is_recursed)
+                       break;
+               if (klass->generic_inst) {
+                       MonoGenericInst *ginst = klass->generic_inst;
+                       int i;
+
+                       g_string_append_c (str, '[');
+                       for (i = 0; i < ginst->type_argc; i++) {
+                               if (i)
+                                       g_string_append_c (str, ',');
+                               mono_type_get_name_recurse (ginst->type_argv [i], str, FALSE);
+                       }
+                       g_string_append_c (str, ']');
+               } else if (klass->gen_params) {
+                       int i;
+
+                       g_string_append_c (str, '[');
+                       for (i = 0; i < klass->num_gen_params; i++) {
+                               if (i)
+                                       g_string_append_c (str, ',');
+                               g_string_append (str, klass->gen_params [i].name);
+                       }
+                       g_string_append_c (str, ']');
+               }
                break;
        }
 }
@@ -171,7 +192,7 @@ char*
 mono_type_get_name (MonoType *type)
 {
        GString* result = g_string_new ("");
-       mono_type_get_name_recurse (type, result);
+       mono_type_get_name_recurse (type, result, FALSE);
 
        if (type->byref)
                g_string_append_c (result, '&');
@@ -179,57 +200,152 @@ mono_type_get_name (MonoType *type)
        return g_string_free (result, FALSE);
 }
 
+gboolean
+mono_class_is_open_constructed_type (MonoType *t)
+{
+       switch (t->type) {
+       case MONO_TYPE_VAR:
+       case MONO_TYPE_MVAR:
+               return TRUE;
+       case MONO_TYPE_SZARRAY:
+               return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
+       case MONO_TYPE_ARRAY:
+               return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
+       case MONO_TYPE_PTR:
+               return mono_class_is_open_constructed_type (t->data.type);
+       case MONO_TYPE_GENERICINST: {
+               MonoGenericInst *ginst = t->data.generic_inst;
+               int i;
+
+               if (mono_class_is_open_constructed_type (ginst->generic_type))
+                       return TRUE;
+               for (i = 0; i < ginst->type_argc; i++)
+                       if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
+                               return TRUE;
+               return FALSE;
+       }
+       default:
+               return FALSE;
+       }
+}
+
 static MonoType*
-inflate_generic_type (MonoType *type, MonoGenericInst *tgen, MonoGenericInst *mgen)
+inflate_generic_type (MonoType *type, MonoGenericContext *context)
 {
        switch (type->type) {
        case MONO_TYPE_MVAR:
-               if (mgen)
-                       return dup_type (mgen->type_argv [type->data.generic_param->num]);
+               if (context->gmethod && context->gmethod->mtype_argv)
+                       return dup_type (
+                               context->gmethod->mtype_argv [type->data.generic_param->num],
+                               type);
                else
-                       return type;
+                       return NULL;
        case MONO_TYPE_VAR:
-               /*g_print ("inflating var %d to %s\n", type->data.type_param, mono_type_get_name (tgen->type_argv [type->data.type_param]));*/
-               return dup_type (tgen->type_argv [type->data.generic_param->num]);
+               if (context->ginst)
+                       return dup_type (
+                               context->ginst->type_argv [type->data.generic_param->num],
+                               type);
+               else
+                       return NULL;
        case MONO_TYPE_SZARRAY: {
                MonoClass *eclass = type->data.klass;
-               MonoClass *nclass;
+               MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
+               if (!inflated)
+                       return NULL;
+               nt = dup_type (type, type);
+               nt->data.klass = mono_class_from_mono_type (inflated);
+               return nt;
+       }
+       case MONO_TYPE_GENERICINST: {
+               MonoGenericInst *oginst = type->data.generic_inst;
+               MonoGenericInst *nginst;
                MonoType *nt;
-               if (eclass->byval_arg.type == MONO_TYPE_MVAR) {
-                       nclass = mono_class_from_mono_type (mgen->type_argv [eclass->byval_arg.data.generic_param->num]);
-               } else if (eclass->byval_arg.type == MONO_TYPE_VAR) {
-                       nclass = mono_class_from_mono_type (tgen->type_argv [eclass->byval_arg.data.generic_param->num]);
-               } else {
-                       return type;
+               int i;
+
+               nginst = g_new0 (MonoGenericInst, 1);
+               *nginst = *oginst;
+
+               nginst->is_open = FALSE;
+
+               nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
+
+               for (i = 0; i < oginst->type_argc; i++) {
+                       MonoType *t = oginst->type_argv [i];
+                       nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
+
+                       if (!nginst->is_open)
+                               nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
+               };
+
+               nginst->klass = NULL;
+
+               nginst->context = g_new0 (MonoGenericContext, 1);
+               nginst->context->ginst = nginst;
+
+               mono_loader_lock ();
+               nt = g_hash_table_lookup (oginst->klass->image->generic_inst_cache, nginst);
+
+               if (nt) {
+                       g_free (nginst->type_argv);
+                       g_free (nginst);
+                       mono_loader_unlock ();
+                       return nt;
                }
-               nt = dup_type (type);
-               nt->data.klass = nclass;
+
+               nginst->dynamic_info = NULL;
+               nginst->initialized = FALSE;
+
+               mono_class_create_generic (nginst);
+
+               mono_stats.generic_instance_count++;
+               mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
+                       sizeof (MonoGenericContext) +
+                       nginst->type_argc * sizeof (MonoType);
+
+               nt = dup_type (type, type);
+               nt->data.generic_inst = nginst;
+               g_hash_table_insert (oginst->klass->image->generic_inst_cache, nginst, nt);
+               mono_loader_unlock ();
                return nt;
        }
        default:
-               return type;
+               return NULL;
        }
-       return type;
+       return NULL;
+}
+
+MonoType*
+mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
+{
+       MonoType *inflated = inflate_generic_type (type, context);
+
+       if (!inflated)
+               return type;
+
+       mono_stats.inflated_type_count++;
+       return inflated;
 }
 
 static MonoMethodSignature*
-inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericInst *tgen, MonoGenericInst *mgen)
+inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
+                          MonoGenericContext *context)
 {
        MonoMethodSignature *res;
        int i;
        res = mono_metadata_signature_alloc (image, sig->param_count);
-       res->ret = inflate_generic_type (sig->ret, tgen, mgen);
-       for (i = 0; i < sig->param_count; ++i) {
-               res->params [i] = inflate_generic_type (sig->params [i], tgen, mgen);
-       }
+       res->ret = mono_class_inflate_generic_type (sig->ret, context);
+       for (i = 0; i < sig->param_count; ++i)
+               res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
        res->hasthis = sig->hasthis;
        res->explicit_this = sig->explicit_this;
        res->call_convention = sig->call_convention;
+       res->generic_param_count = sig->generic_param_count;
+       res->is_inflated = 1;
        return res;
 }
 
 static MonoMethodHeader*
-inflate_generic_header (MonoMethodHeader *header, MonoGenericInst *tgen, MonoGenericInst *mgen)
+inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
 {
        MonoMethodHeader *res;
        int i;
@@ -241,28 +357,59 @@ inflate_generic_header (MonoMethodHeader *header, MonoGenericInst *tgen, MonoGen
        res->init_locals = header->init_locals;
        res->num_locals = header->num_locals;
        res->clauses = header->clauses;
-       for (i = 0; i < header->num_locals; ++i) {
-               res->locals [i] = inflate_generic_type (header->locals [i], tgen, mgen);
-       }
+       res->gen_params = header->gen_params;
+       for (i = 0; i < header->num_locals; ++i)
+               res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
        return res;
 }
 
 MonoMethod*
-mono_class_inflate_generic_method (MonoMethod *method, MonoGenericInst *tgen, MonoGenericInst *mgen)
+mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
+                                  MonoClass *klass)
 {
-       MonoMethod *result;
-       if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
-               MonoMethodPInvoke *nmethod = g_new0 (MonoMethodPInvoke, 1);
-               *nmethod = *(MonoMethodPInvoke*)method;
-               result = (MonoMethod*)nmethod;
-       } else {
-               MonoMethodNormal *nmethod = g_new0 (MonoMethodNormal, 1);
-               *nmethod = *(MonoMethodNormal*)method;
-               result = (MonoMethod*)nmethod;
-               nmethod->header = inflate_generic_header (((MonoMethodNormal*)method)->header, tgen, mgen);
+       MonoMethodInflated *result;
+
+       if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
+           (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
+               return method;
+
+       mono_stats.inflated_method_count++;
+       mono_stats.generics_metadata_size +=
+               sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
+
+       result = g_new0 (MonoMethodInflated, 1);
+       result->nmethod = *(MonoMethodNormal*)method;
+
+       if (result->nmethod.header)
+               result->nmethod.header = inflate_generic_header (
+                       result->nmethod.header, context);
+
+       if (klass)
+               result->nmethod.method.klass = klass;
+       else {
+               MonoType *declaring = mono_class_inflate_generic_type (
+                       &method->klass->byval_arg, context);
+               result->nmethod.method.klass = mono_class_from_mono_type (declaring);
        }
-       result->signature = inflate_generic_signature (method->klass->image, result->signature, tgen, mgen);
-       return result;
+
+       result->nmethod.method.signature = inflate_generic_signature (
+               method->klass->image, method->signature, context);
+
+       if (context->gmethod) {
+               result->context = g_new0 (MonoGenericContext, 1);
+               result->context->gmethod = context->gmethod;
+               result->context->ginst = result->nmethod.method.klass->generic_inst;
+
+               mono_stats.generics_metadata_size += sizeof (MonoGenericContext);
+       } else
+               result->context = result->nmethod.method.klass->generic_inst->context;
+
+       if (method->signature->is_inflated)
+               result->declaring = ((MonoMethodInflated *) method)->declaring;
+       else
+               result->declaring = method;
+
+       return (MonoMethod *) result;
 }
 
 /** 
@@ -286,6 +433,7 @@ class_compute_field_layout (MonoClass *class)
        guint32 rva;
        guint32 packing_size = 0;
        gboolean explicit_size;
+       MonoClassField *field;
 
        if (class->size_inited)
                return;
@@ -295,7 +443,7 @@ class_compute_field_layout (MonoClass *class)
                        class_compute_field_layout (class->parent);
                class->instance_size += class->parent->instance_size;
                class->min_align = class->parent->min_align;
-               blittable = class->blittable;
+               blittable = class->parent->blittable;
        } else {
                class->instance_size = sizeof (MonoObject);
                class->min_align = 1;
@@ -326,66 +474,59 @@ class_compute_field_layout (MonoClass *class)
        for (i = 0; i < top; i++){
                const char *sig;
                guint32 cols [MONO_FIELD_SIZE];
-               guint32 constant_cols [MONO_CONSTANT_SIZE];
-               guint32 cindex;
                int idx = class->field.first + i;
-               
-               mono_metadata_decode_row (t, idx, cols, CSIZE (cols));
+
+               field = &class->fields [i];
+               mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
                /* The name is needed for fieldrefs */
-               class->fields [i].name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
+               field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
                sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
                mono_metadata_decode_value (sig, &sig);
                /* FIELD signature == 0x06 */
                g_assert (*sig == 0x06);
-               class->fields [i].type = mono_metadata_parse_field_type (
+               field->type = mono_metadata_parse_field_type (
                        m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
+               if (mono_field_is_deleted (field))
+                       continue;
                if (class->generic_inst) {
-                       class->fields [i].type = inflate_generic_type (class->fields [i].type, class->generic_inst->data.generic_inst, NULL);
-                       class->fields [i].type->attrs = cols [MONO_FIELD_FLAGS];
+                       field->type = mono_class_inflate_generic_type (
+                               field->type, class->generic_inst->context);
+                       field->type->attrs = cols [MONO_FIELD_FLAGS];
                }
 
-               class->fields [i].parent = class;
+               field->parent = class;
 
-               if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)) {
-                       if (class->fields [i].type->byref) {
+               /* Only do these checks if we still think this type is blittable */
+               if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
+                       if (field->type->byref) {
                                blittable = FALSE;
                        } else {
-                               MonoClass *field_class = mono_class_from_mono_type (class->fields [i].type);
+                               MonoClass *field_class = mono_class_from_mono_type (field->type);
                                if (!field_class || !field_class->blittable)
                                        blittable = FALSE;
                        }
                }
+
                if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
-                       mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
-                       if (class->fields [i].offset == (guint32)-1)
-                               g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
+                       mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
+                       if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
+                               g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
                }
 
                if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
                        mono_metadata_field_info (m, idx, NULL, &rva, NULL);
                        if (!rva)
-                               g_warning ("field %s in %s should have RVA data, but hasn't", class->fields [i].name, class->name);
-                       class->fields [i].data = mono_cli_rva_map (class->image->image_info, rva);
+                               g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
+                       field->data = mono_cli_rva_map (class->image->image_info, rva);
                }
 
                if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
-                       class->enum_basetype = class->fields [i].type;
+                       class->enum_basetype = field->type;
                        class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
                        blittable = class->element_class->blittable;
                }
 
-               if ((class->fields [i].type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT) &&
-                       (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)) {
-                       cindex = mono_metadata_get_constant_index (class->image, MONO_TOKEN_FIELD_DEF | (class->field.first + i + 1));
-                       if (!cindex) {
-                               g_warning ("constant for field %s:%s not found", class->name, class->fields [i].name);
-                               continue;
-                       }
-                       mono_metadata_decode_row (&class->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
-                       class->fields [i].def_value = g_new0 (MonoConstant, 1);
-                       class->fields [i].def_value->type = constant_cols [MONO_CONSTANT_TYPE];
-                       class->fields [i].def_value->value = (gpointer)mono_metadata_blob_heap (class->image, constant_cols [MONO_CONSTANT_VALUE]);
-               }
+               /* The def_value of fields is compute lazily during vtable creation */
        }
 
        if (class == mono_defaults.string_class)
@@ -414,6 +555,24 @@ mono_class_layout_fields (MonoClass *class)
        const int top = class->field.count;
        guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
        guint32 pass, passes, real_size;
+       gboolean gc_aware_layout = FALSE;
+       MonoClassField *field;
+
+       /*
+        * Enable GC aware auto layout: in this mode, reference
+        * fields are grouped together inside objects, increasing collector 
+        * performance.
+        * Requires that all classes whose layout is known to native code be annotated
+        * with [StructLayout (LayoutKind.Sequential)]
+        * Value types have gc_aware_layout disabled by default, as per
+        * what the default is for other runtimes.
+        */
+        /* corlib is missing [StructLayout] directives in many places */
+       if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
+               if (class->image != mono_defaults.corlib &&
+                       class->byval_arg.type != MONO_TYPE_VALUETYPE)
+                       gc_aware_layout = TRUE;
+       }
 
        /*
         * Compute field layout and total size (not considering static fields)
@@ -423,11 +582,11 @@ mono_class_layout_fields (MonoClass *class)
        case TYPE_ATTRIBUTE_AUTO_LAYOUT:
        case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
 
-#ifdef GC_AWARE_AUTO_LAYOUT
-               passes = 2;
-#else
-               passes = 1;
-#endif
+               if (gc_aware_layout)
+                       passes = 2;
+               else
+                       passes = 1;
+
                if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
                        passes = 1;
 
@@ -439,13 +598,14 @@ mono_class_layout_fields (MonoClass *class)
                for (pass = 0; pass < passes; ++pass) {
                        for (i = 0; i < top; i++){
                                int size, align;
+                               field = &class->fields [i];
 
-                               if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
+                               if (mono_field_is_deleted (field))
+                                       continue;
+                               if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
                                        continue;
 
-#ifdef GC_AWARE_AUTO_LAYOUT
-                               /* FIXME: Fix mono_marshal_load_type_info () too */
-                               if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
+                               if (gc_aware_layout) {
                                        /* 
                                         * We process fields with reference type in the first pass,
                                         * and fields with non-reference type in the second pass.
@@ -453,7 +613,7 @@ mono_class_layout_fields (MonoClass *class)
                                         * some internal structures, we store GC_MALLOCed memory
                                         * in IntPtr fields...
                                         */
-                                       if (MONO_TYPE_IS_POINTER (class->fields [i].type)) {
+                                       if (MONO_TYPE_IS_POINTER (field->type)) {
                                                if (pass == 1)
                                                        continue;
                                        } else {
@@ -461,23 +621,22 @@ mono_class_layout_fields (MonoClass *class)
                                                        continue;
                                        }
                                }
-#endif
 
                                if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
-                                       (strcmp (class->fields [i].name, "$PRIVATE$") == 0)) {
+                                       (strcmp (field->name, "$PRIVATE$") == 0)) {
                                        /* This field is a hack inserted by MCS to empty structures */
                                        continue;
                                }
 
-                               size = mono_type_size (class->fields [i].type, &align);
+                               size = mono_type_size (field->type, &align);
                        
                                /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
                                align = class->packing_size ? MIN (class->packing_size, align): align;
                                class->min_align = MAX (align, class->min_align);
-                               class->fields [i].offset = real_size;
-                               class->fields [i].offset += align - 1;
-                               class->fields [i].offset &= ~(align - 1);
-                               real_size = class->fields [i].offset + size;
+                               field->offset = real_size;
+                               field->offset += align - 1;
+                               field->offset &= ~(align - 1);
+                               real_size = field->offset + size;
                        }
 
                        class->instance_size = MAX (real_size, class->instance_size);
@@ -492,29 +651,32 @@ mono_class_layout_fields (MonoClass *class)
                real_size = 0;
                for (i = 0; i < top; i++) {
                        int size, align;
+                       field = &class->fields [i];
 
                        /*
                         * There must be info about all the fields in a type if it
                         * uses explicit layout.
                         */
 
-                       if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
+                       if (mono_field_is_deleted (field))
+                               continue;
+                       if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
                                continue;
 
-                       size = mono_type_size (class->fields [i].type, &align);
+                       size = mono_type_size (field->type, &align);
                        
                        /*
-                        * When we get here, class->fields [i].offset is already set by the
+                        * When we get here, field->offset is already set by the
                         * loader (for either runtime fields or fields loaded from metadata).
                         * The offset is from the start of the object: this works for both
                         * classes and valuetypes.
                         */
-                       class->fields [i].offset += sizeof (MonoObject);
+                       field->offset += sizeof (MonoObject);
 
                        /*
                         * Calc max size.
                         */
-                       real_size = MAX (real_size, size + class->fields [i].offset);
+                       real_size = MAX (real_size, size + field->offset);
                }
                class->instance_size = MAX (real_size, class->instance_size);
                break;
@@ -527,15 +689,18 @@ mono_class_layout_fields (MonoClass *class)
         */
        for (i = 0; i < top; i++){
                int size, align;
+               field = &class->fields [i];
                        
-               if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
+               if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
+                       continue;
+               if (mono_field_is_deleted (field))
                        continue;
                        
-               size = mono_type_size (class->fields [i].type, &align);
-               class->fields [i].offset = class->class_size;
-               class->fields [i].offset += align - 1;
-               class->fields [i].offset &= ~(align - 1);
-               class->class_size = class->fields [i].offset + size;
+               size = mono_type_size (field->type, &align);
+               field->offset = class->class_size;
+               field->offset += align - 1;
+               field->offset &= ~(align - 1);
+               class->class_size = field->offset + size;
        }
 }
 
@@ -553,6 +718,7 @@ init_properties (MonoClass *class)
        class->properties = g_new0 (MonoProperty, class->property.count);
        for (i = class->property.first; i < class->property.last; ++i) {
                mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
+               class->properties [i - class->property.first].parent = class;
                class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
                class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
 
@@ -587,6 +753,7 @@ init_events (MonoClass *class)
        class->events = g_new0 (MonoEvent, class->event.count);
        for (i = class->event.first; i < class->event.last; ++i) {
                mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
+               class->events [i - class->event.first].parent = class;
                class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
                class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
 
@@ -620,7 +787,7 @@ mono_get_unique_iid (MonoClass *class)
        char *str;
        gpointer value;
        
-       g_assert (class->flags & TYPE_ATTRIBUTE_INTERFACE);
+       g_assert (MONO_CLASS_IS_INTERFACE (class));
 
        mono_loader_lock ();
 
@@ -690,7 +857,7 @@ setup_interface_offsets (MonoClass *class, int cur_slot)
                }
        }
 
-       if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
+       if (MONO_CLASS_IS_INTERFACE (class)) {
                if (max_iid < class->interface_id)
                        max_iid = class->interface_id;
        }
@@ -729,7 +896,7 @@ setup_interface_offsets (MonoClass *class, int cur_slot)
                }
        }
 
-       if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
+       if (MONO_CLASS_IS_INTERFACE (class))
                class->interface_offsets [class->interface_id] = cur_slot;
 
        return cur_slot;
@@ -780,7 +947,7 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
        /* override interface methods */
        for (i = 0; i < onum; i++) {
                MonoMethod *decl = overrides [i*2];
-               if (decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
+               if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
                        int dslot;
                        g_assert (decl->slot != -1);
                        dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
@@ -905,6 +1072,20 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                                        if (im->flags & METHOD_ATTRIBUTE_STATIC)
                                                        continue;
                                        g_assert (io + l <= max_vtsize);
+
+                                       /* 
+                                        * If one of our parents already implements this interface
+                                        * we can inherit the implementation.
+                                        */
+                                       if (!(vtable [io + l])) {
+                                               MonoClass *parent = class->parent;
+
+                                               if ((ic->interface_id <= parent->max_interface_id) && 
+                                                       (parent->interface_offsets [ic->interface_id]) &&
+                                                       parent->vtable)
+                                                       vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
+                                       }
+
                                        if (!(vtable [io + l])) {
                                                for (j = 0; j < onum; ++j) {
                                                        g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name, 
@@ -947,8 +1128,19 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                MonoMethod *cm;
               
                cm = class->methods [i];
-
-               if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && (cm->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
+               
+               /*
+                * Non-virtual method have no place in the vtable.
+                * This also catches static methods (since they are not virtual).
+                */
+               if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
+                       continue;
+               
+               /*
+                * If the method is REUSE_SLOT, we must check in the
+                * base class for a method to override.
+                */
+               if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
                        int slot = -1;
                        for (k = class->parent; k ; k = k->parent) {
                                int j;
@@ -976,14 +1168,14 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
                if (cm->slot < 0)
                        cm->slot = cur_slot++;
 
-               if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
+               if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
                        vtable [cm->slot] = cm;
        }
 
        /* override non interface methods */
        for (i = 0; i < onum; i++) {
                MonoMethod *decl = overrides [i*2];
-               if (!(decl->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+               if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
                        g_assert (decl->slot != -1);
                        vtable [decl->slot] = overrides [i*2 + 1];
                        overrides [i * 2 + 1]->slot = decl->slot;
@@ -1007,8 +1199,15 @@ mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
 
                mono_g_hash_table_destroy (override_map);
        }
-       
-       class->vtable_size = cur_slot;
+
+       if (class->generic_inst) {
+               MonoClass *gklass = mono_class_from_mono_type (class->generic_inst->generic_type);
+
+               mono_class_init (gklass);
+               class->vtable_size = gklass->vtable_size;
+       } else       
+               class->vtable_size = cur_slot;
+
        class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
        memcpy (class->vtable, vtable,  sizeof (gpointer) * class->vtable_size);
 
@@ -1100,8 +1299,83 @@ mono_class_init (MonoClass *class)
 
        class->init_pending = 1;
 
+       if (mono_debugger_start_class_init_func)
+               mono_debugger_start_class_init_func (class);
+
        mono_stats.initialized_class_count++;
 
+       if (class->generic_inst && !class->generic_inst->is_dynamic) {
+               MonoGenericInst *ginst = class->generic_inst;
+               MonoClass *gklass;
+               GList *list;
+
+               gklass = mono_class_from_mono_type (ginst->generic_type);
+               mono_class_init (gklass);
+
+               if (ginst->parent)
+                       class->parent = mono_class_from_mono_type (ginst->parent);
+               else
+                       class->parent = gklass->parent;
+
+               mono_class_setup_parent (class, class->parent);
+
+               if (MONO_CLASS_IS_INTERFACE (class))
+                       class->interface_id = mono_get_unique_iid (class);
+
+               class->method = gklass->method;
+               class->methods = g_new0 (MonoMethod *, class->method.count);
+
+               for (i = 0; i < class->method.count; i++)
+                       class->methods [i] = mono_class_inflate_generic_method (
+                               gklass->methods [i], ginst->context, ginst->klass);
+
+               class->field = gklass->field;
+               class->fields = g_new0 (MonoClassField, class->field.count);
+
+               for (i = 0; i < class->field.count; i++) {
+                       MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
+                       ifield->generic_type = gklass->fields [i].type;
+
+                       class->fields [i] = gklass->fields [i];
+                       class->fields [i].generic_info = ifield;
+                       class->fields [i].parent = class;
+                       class->fields [i].type = mono_class_inflate_generic_type (
+                               class->fields [i].type, ginst->context);
+               }
+
+               class->property = gklass->property;
+               class->properties = g_new0 (MonoProperty, class->property.count);
+
+               for (i = 0; i < class->property.count; i++) {
+                       MonoProperty *prop = &class->properties [i];
+
+                       *prop = gklass->properties [i];
+
+                       if (prop->get)
+                               prop->get = mono_class_inflate_generic_method (
+                                       prop->get, ginst->context, ginst->klass);
+                       if (prop->set)
+                               prop->set = mono_class_inflate_generic_method (
+                                       prop->set, ginst->context, ginst->klass);
+
+                       prop->parent = class;
+               }
+
+               class->interface_count = gklass->interface_count;
+               class->interfaces = g_new0 (MonoClass *, class->interface_count);
+               for (i = 0; i < class->interface_count; i++) {
+                       MonoType *it = &gklass->interfaces [i]->byval_arg;
+                       MonoType *inflated = mono_class_inflate_generic_type (
+                               it, ginst->context);
+                       class->interfaces [i] = mono_class_from_mono_type (inflated);
+                       mono_class_init (class->interfaces [i]);
+               }
+
+               for (list = gklass->nested_classes; list; list = list->next)
+                       class->nested_classes = g_list_append (
+                               class->nested_classes, list->data);
+       }
+
        if (class->parent && !class->parent->inited)
                mono_class_init (class->parent);
 
@@ -1157,23 +1431,25 @@ mono_class_init (MonoClass *class)
                }
        }
 
-       init_properties (class);
-       init_events (class);
+       if (!class->generic_inst) {
+               init_properties (class);
+               init_events (class);
 
-       i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
-       while (i) {
-               MonoClass* nclass;
-               guint32 cols [MONO_NESTED_CLASS_SIZE];
-               mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
-               nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
-               class->nested_classes = g_list_prepend (class->nested_classes, nclass);
+               i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
+               while (i) {
+                       MonoClass* nclass;
+                       guint32 cols [MONO_NESTED_CLASS_SIZE];
+                       mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
+                       nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
+                       class->nested_classes = g_list_prepend (class->nested_classes, nclass);
 
-               i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
+                       i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
+               }
        }
 
        mono_class_setup_supertypes (class);
 
-       if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
+       if (MONO_CLASS_IS_INTERFACE (class)) {
                for (i = 0; i < class->method.count; ++i)
                        class->methods [i]->slot = i;
                class->init_pending = 0;
@@ -1184,6 +1460,10 @@ mono_class_init (MonoClass *class)
                 */
                setup_interface_offsets (class, 0);
                mono_loader_unlock ();
+
+               if (mono_debugger_class_init_func)
+                       mono_debugger_class_init_func (class);
+
                return;
        }
 
@@ -1251,14 +1531,13 @@ mono_class_init (MonoClass *class)
                mono_debugger_class_init_func (class);
 }
 
-
 void
 mono_class_setup_mono_type (MonoClass *class)
 {
        const char *name = class->name;
        const char *nspace = class->name_space;
 
-       if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
+       if (MONO_CLASS_IS_INTERFACE (class))
                class->interface_id = mono_get_unique_iid (class);
 
        class->this_arg.byref = 1;
@@ -1273,6 +1552,7 @@ mono_class_setup_mono_type (MonoClass *class)
                         * do not set the valuetype bit for System.ValueType.
                         * class->valuetype = 1;
                         */
+                       class->blittable = TRUE;
                } else if (!strcmp (name, "Enum")) {
                        /*
                         * do not set the valuetype bit for System.Enum.
@@ -1389,12 +1669,21 @@ mono_class_setup_parent (MonoClass *class, MonoClass *parent)
                return;
        }
 
-       if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+       if (!MONO_CLASS_IS_INTERFACE (class)) {
                class->parent = parent;
 
                if (!parent)
                        g_assert_not_reached (); /* FIXME */
 
+               if (parent->generic_inst && !parent->name) {
+                       /*
+                        * If the parent is a generic instance, we may get
+                        * called before it is fully initialized, especially
+                        * before it has its name.
+                        */
+                       return;
+               }
+
                class->marshalbyref = parent->marshalbyref;
                class->contextbound  = parent->contextbound;
                class->delegate  = parent->delegate;
@@ -1473,22 +1762,13 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        }
 
        g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
-       
+
        mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
        
        name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
        nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
 
-       if (cols [MONO_TYPEDEF_EXTENDS])
-               parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
-       interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
-
        class = g_malloc0 (sizeof (MonoClass));
-                          
-       g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
-
-       class->interfaces = interfaces;
-       class->interface_count = icount;
 
        class->name = name;
        class->name_space = nspace;
@@ -1497,6 +1777,15 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        class->type_token = type_token;
        class->flags = cols [MONO_TYPEDEF_FLAGS];
 
+       g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
+
+       if (cols [MONO_TYPEDEF_EXTENDS])
+               parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
+       interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
+
+       class->interfaces = interfaces;
+       class->interface_count = icount;
+
        if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
                class->unicode = 1;
        /* fixme: maybe we must set this on windows 
@@ -1519,7 +1808,7 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
 
        if (tt->rows > tidx){           
-               mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
+               mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
                class->field.last  = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
                class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
        } else {
@@ -1558,125 +1847,115 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        return class;
 }
 
-static char*
-get_instantiation_name (const char *name, MonoGenericInst *ginst)
-{
-       GString *res = g_string_new (name);
-       const char *p;
-       int i;
-       MonoClass *argclass;
-       
-       p = strchr (name, '<');
-       if (p) {
-               g_string_truncate (res, (p - name) + 1);
-       } else {
-               g_string_append_c (res, '<');
-       }
-       for (i = 0; i < ginst->type_argc; ++i) {
-               if (i > 0)
-                       g_string_append_c (res, ',');
-               argclass = mono_class_from_mono_type (ginst->type_argv [i]);
-               g_string_append (res, argclass->name);
-       }
-       g_string_append_c (res, '>');
-       return g_string_free (res, FALSE);
-}
-
 MonoClass*
-mono_class_from_generic (MonoType *gtype, gboolean inflate_methods)
+mono_class_create_generic (MonoGenericInst *ginst)
 {
-       MonoGenericInst *ginst = gtype->data.generic_inst;
-       MonoClass *gklass = mono_class_from_mono_type (ginst->generic_type);
-       MonoClass *class;
-       MonoImage *image;
+       MonoClass *klass, *gklass;
 
-       mono_loader_lock ();
+       if (!ginst->klass)
+               ginst->klass = g_malloc0 (sizeof (MonoClass));
+       klass = ginst->klass;
 
-       image = gklass->image;
-       if ((class = g_hash_table_lookup (image->generics_cache, gtype))) {
-               mono_loader_unlock ();
-               return class;
-       }
+       gklass = mono_class_from_mono_type (ginst->generic_type);
 
-       mono_class_init (gklass);
+       klass->nested_in = gklass->nested_in;
 
-       class = g_malloc0 (sizeof (MonoClass));
-       class->name_space = gklass->name_space;
-       class->name = get_instantiation_name (gklass->name, ginst);
-       class->image = image;
-       class->flags = gklass->flags;
+       klass->name = gklass->name;
+       klass->name_space = gklass->name_space;
+       klass->image = gklass->image;
+       klass->flags = gklass->flags;
 
-       class->generic_inst = gtype;
+       klass->generic_inst = ginst;
 
-       class->cast_class = class->element_class = class;
+       klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
+       klass->this_arg.data.generic_inst = klass->byval_arg.data.generic_inst = ginst;
+       klass->this_arg.byref = TRUE;
 
-       if (inflate_methods) {
-               int i;
+       klass->cast_class = klass->element_class = klass;
 
-               mono_class_setup_parent (class, gklass->parent);
-               mono_class_setup_mono_type (class);
+       if (ginst->is_dynamic) {
+               klass->instance_size = gklass->instance_size;
+               klass->class_size = gklass->class_size;
+               klass->size_inited = 1;
 
-               class->field = gklass->field;
-               class->method = gklass->method;
-               class->methods = g_new0 (MonoMethod *, class->method.count);
-               for (i = 0; i < class->method.count; i++)
-                       class->methods [i] = mono_class_inflate_generic_method (gklass->methods [i], ginst, NULL);
+               klass->valuetype = gklass->valuetype;
        }
 
-       g_hash_table_insert (image->generics_cache, gtype, class);
-
-       mono_loader_unlock ();
-
-       return class;
+       return klass;
 }
 
 MonoClass *
-mono_class_from_gen_param (MonoGenericParam *param, gboolean mvar)
+mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
 {
-       MonoClass *result;
-       int key;
-       static GHashTable *cache = NULL;
+       MonoClass *klass, **ptr;
+       int count, pos, i;
 
-       if (param->klass)
-               return param->klass;
-
-       /*
-        * At this point, we're a type parameter which is loaded from reflection.
-        * We create a very minimalistic MonoClass here which just contains that
-        * type parameter's number since we cannot get more information from the
-        * metadata (and also don't need them).
-        */
+       if (param->pklass)
+               return param->pklass;
 
-       mono_loader_lock ();
+       klass = param->pklass = g_new0 (MonoClass, 1);
 
-       if (!cache)
-               cache = g_hash_table_new (NULL, NULL);
+       for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
+               ;
 
-       key = mvar? 1: 0;
-       key |= param->num << 1;
+       pos = 0;
+       if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
+               klass->parent = param->constraints [0];
+               pos++;
+       }
 
-       if ((result = g_hash_table_lookup (cache, GINT_TO_POINTER (key)))) {
-               mono_loader_unlock ();
-               return result;
+       if (count - pos > 0) {
+               klass->interface_count = count - pos;
+               klass->interfaces = g_new0 (MonoClass *, count - pos);
+               for (i = pos; i < count; i++)
+                       klass->interfaces [i - pos] = param->constraints [i];
        }
-       result = param->klass = g_new0 (MonoClass, 1);
 
-       result->parent = NULL;
-       result->name = g_strdup_printf ("%s%d", mvar? "!!": "!", param->num);
-       result->name_space = "";
-       result->image = mono_defaults.corlib; 
-       result->inited = TRUE;
-       result->cast_class = result->element_class = result;
-       result->enum_basetype = &result->element_class->byval_arg;
+       g_assert (param->name);
 
-       result->this_arg.type = result->byval_arg.type = mvar? MONO_TYPE_MVAR: MONO_TYPE_VAR;
-       result->this_arg.data.generic_param = result->byval_arg.data.generic_param = param;
-       result->this_arg.byref = TRUE;
+       klass->name = param->name;
+       klass->name_space = "";
+       klass->image = image;
+       klass->cast_class = klass->element_class = klass;
+       klass->enum_basetype = &klass->element_class->byval_arg;
+       klass->flags = TYPE_ATTRIBUTE_PUBLIC;
 
-       g_hash_table_insert (cache, GINT_TO_POINTER (key), result);
+       klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
+       klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
+       klass->this_arg.byref = TRUE;
 
-       mono_loader_unlock ();
-       return result;
+       mono_class_init (klass);
+
+       return klass;
+}
+
+static MonoClass *
+my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
+{
+       MonoClass *klass;
+
+       if (param->pklass)
+               return param->pklass;
+
+       klass = g_new0 (MonoClass, 1);
+
+       if (param->name)
+               klass->name = param->name;
+       else
+               klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
+       klass->name_space = "";
+       klass->image = mono_defaults.corlib;
+       klass->cast_class = klass->element_class = klass;
+       klass->enum_basetype = &klass->element_class->byval_arg;
+       klass->flags = TYPE_ATTRIBUTE_PUBLIC;
+
+       klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
+       klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
+       klass->this_arg.byref = TRUE;
+
+       mono_class_init (klass);
+
+       return klass;
 }
 
 MonoClass *
@@ -1707,6 +1986,7 @@ mono_ptr_class_get (MonoType *type)
        result->instance_size = sizeof (gpointer);
        result->cast_class = result->element_class = el_class;
        result->enum_basetype = &result->element_class->byval_arg;
+       result->blittable = TRUE;
 
        result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
        result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
@@ -1743,15 +2023,17 @@ mono_fnptr_class_get (MonoMethodSignature *sig)
        result->name_space = "MonoFNPtrFakeClass";
        result->image = NULL; /* need to fix... */
        result->inited = TRUE;
-       result->flags = TYPE_ATTRIBUTE_CLASS; // | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
+       result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
        /* Can pointers get boxed? */
        result->instance_size = sizeof (gpointer);
        result->cast_class = result->element_class = result;
+       result->blittable = TRUE;
 
        result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
        result->this_arg.data.method = result->byval_arg.data.method = sig;
        result->this_arg.byref = TRUE;
        result->enum_basetype = &result->element_class->byval_arg;
+       result->blittable = TRUE;
 
        mono_class_setup_supertypes (result);
 
@@ -1803,7 +2085,7 @@ mono_class_from_mono_type (MonoType *type)
        case MONO_TYPE_TYPEDBYREF:
                return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
        case MONO_TYPE_ARRAY:
-               return mono_array_class_get (type->data.array->eklass, type->data.array->rank);
+               return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
        case MONO_TYPE_PTR:
                return mono_ptr_class_get (type->data.type);
        case MONO_TYPE_FNPTR:
@@ -1814,14 +2096,12 @@ mono_class_from_mono_type (MonoType *type)
        case MONO_TYPE_VALUETYPE:
                return type->data.klass;
        case MONO_TYPE_GENERICINST:
-               return mono_class_from_generic (type, TRUE);
+               g_assert (type->data.generic_inst->klass);
+               return type->data.generic_inst->klass;
        case MONO_TYPE_VAR:
+               return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
        case MONO_TYPE_MVAR:
-               /* NOTE: Unless this is a dynamic type, only the `num' field is valid in
-                *       `type->data.generic_param'.  For dynamic types, its `klass' field
-                *       points to the already created class.
-                */
-               return mono_class_from_gen_param (type->data.generic_param, type->type == MONO_TYPE_MVAR);
+               return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
        default:
                g_warning ("implement me 0x%02x\n", type->type);
                g_assert_not_reached ();
@@ -1835,9 +2115,10 @@ mono_class_from_mono_type (MonoType *type)
  * @type_spec:  typespec token
  */
 static MonoClass *
-mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
+mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
+                                MonoGenericContext *context)
 {
-       MonoType *type;
+       MonoType *type, *inflated;
        MonoClass *class;
 
        type = mono_type_create_from_typespec (image, type_spec);
@@ -1850,7 +2131,11 @@ mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
                class = mono_array_class_get (type->data.klass, 1);
                break;
        case MONO_TYPE_PTR:
-               class = mono_class_from_mono_type (type->data.type);
+               class = mono_ptr_class_get (type->data.type);
+               break;
+       case MONO_TYPE_GENERICINST:
+               g_assert (type->data.generic_inst->klass);
+               class = type->data.generic_inst->klass;
                break;
        default:
                /* it seems any type can be stored in TypeSpec as well */
@@ -1858,21 +2143,25 @@ mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
                break;
        }
 
-       mono_metadata_free_type (type);
-       
-       return class;
+       if (!class || !context)
+               return class;
+
+       inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
+
+       return mono_class_from_mono_type (inflated);
 }
 
 /**
- * mono_array_class_get:
+ * mono_bounded_array_class_get:
  * @element_class: element class 
  * @rank: the dimension of the array class
+ * @bounded: whenever the array has non-zero bounds
  *
  * Returns: a class object describing the array with element type @element_type and 
  * dimension @rank. 
  */
 MonoClass *
-mono_array_class_get (MonoClass *eclass, guint32 rank)
+mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
 {
        MonoImage *image;
        MonoClass *class;
@@ -1884,6 +2173,10 @@ mono_array_class_get (MonoClass *eclass, guint32 rank)
 
        g_assert (rank <= 255);
 
+       if (rank > 1)
+               /* bounded only matters for one-dimensional arrays */
+               bounded = FALSE;
+
        image = eclass->image;
 
        mono_loader_lock ();
@@ -1891,7 +2184,7 @@ mono_array_class_get (MonoClass *eclass, guint32 rank)
        if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
                for (; list; list = list->next) {
                        class = list->data;
-                       if (class->rank == rank) {
+                       if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
                                mono_loader_unlock ();
                                return class;
                        }
@@ -1899,7 +2192,7 @@ mono_array_class_get (MonoClass *eclass, guint32 rank)
        }
 
        /* for the building corlib use System.Array from it */
-       if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "corlib") == 0) {
+       if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
                parent = mono_class_from_name (image, "System", "Array");
                corlib_type = TRUE;
        } else {
@@ -1939,7 +2232,7 @@ mono_array_class_get (MonoClass *eclass, guint32 rank)
 
        class->element_class = eclass;
 
-       if (rank > 1) {
+       if ((rank > 1) || bounded) {
                MonoArrayType *at = g_new0 (MonoArrayType, 1);
                class->byval_arg.type = MONO_TYPE_ARRAY;
                class->byval_arg.data.array = at;
@@ -1947,7 +2240,6 @@ mono_array_class_get (MonoClass *eclass, guint32 rank)
                at->rank = rank;
                /* FIXME: complete.... */
        } else {
-               /* FIXME: this is not correct. the lbound could be >0 */
                class->byval_arg.type = MONO_TYPE_SZARRAY;
                class->byval_arg.data.klass = eclass;
        }
@@ -1965,6 +2257,20 @@ mono_array_class_get (MonoClass *eclass, guint32 rank)
        return class;
 }
 
+/**
+ * mono_array_class_get:
+ * @element_class: element class 
+ * @rank: the dimension of the array class
+ *
+ * Returns: a class object describing the array with element type @element_type and 
+ * dimension @rank. 
+ */
+MonoClass *
+mono_array_class_get (MonoClass *eclass, guint32 rank)
+{
+       return mono_bounded_array_class_get (eclass, rank, FALSE);
+}
+
 /**
  * mono_class_instance_size:
  * @klass: a class 
@@ -2091,6 +2397,12 @@ mono_class_get_field_from_name (MonoClass *klass, const char *name)
        return NULL;
 }
 
+void *
+mono_vtable_get_static_field_data (MonoVTable *vt)
+{
+       return vt->data;
+}
+
 MonoProperty*
 mono_class_get_property_from_name (MonoClass *klass, const char *name)
 {
@@ -2117,9 +2429,9 @@ mono_class_get_property_from_name (MonoClass *klass, const char *name)
 MonoClass *
 mono_class_get (MonoImage *image, guint32 type_token)
 {
-       MonoClass *class;
+       MonoClass *class = NULL;
 
-       if (image->assembly->dynamic)
+       if (image->dynamic)
                return mono_lookup_dynamic_token (image, type_token);
 
        switch (type_token & 0xff000000){
@@ -2130,7 +2442,7 @@ mono_class_get (MonoImage *image, guint32 type_token)
                class = mono_class_from_typeref (image, type_token);
                break;
        case MONO_TOKEN_TYPE_SPEC:
-               class = mono_class_create_from_typespec (image, type_token);
+               class = mono_class_create_from_typespec (image, type_token, NULL);
                break;
        default:
                g_warning ("unknown token type %x", type_token & 0xff000000);
@@ -2144,58 +2456,71 @@ mono_class_get (MonoImage *image, guint32 type_token)
 }
 
 MonoClass *
-mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
+mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
 {
-       MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
-       guint32 cols [MONO_TYPEDEF_SIZE];
-       const char *n;
-       const char *nspace;
-       guint32 i, visib;
+       MonoClass *class = mono_class_get (image, type_token);
+       MonoType *inflated;
 
-       /* add a cache if needed */
-       for (i = 1; i <= t->rows; ++i) {
-               mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
-               /* nested types are accessed from the nesting name */
-               visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
-               if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
-                       continue;
-               n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
-               nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
-               if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
-                       return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
+       if (!class || !context)
+               return class;
+
+       switch (class->byval_arg.type) {
+       case MONO_TYPE_GENERICINST:
+               if (!class->generic_inst->is_open)
+                       return class;
+               break;
+       case MONO_TYPE_VAR:
+       case MONO_TYPE_MVAR:
+               break;
+       default:
+               return class;
        }
-       return NULL;
+
+       inflated = inflate_generic_type (&class->byval_arg, context);
+       if (!inflated)
+               return class;
+
+       return mono_class_from_mono_type (inflated);
 }
 
-static MonoImage*
-load_file_for_image (MonoImage *image, int fileidx)
+/**
+ * mono_class_from_name_case:
+ * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
+ * @name_space: the type namespace
+ * @name: the type short name.
+ *
+ * Obtains a MonoClass with a given namespace and a given name which
+ * is located in the given MonoImage.   The namespace and name
+ * lookups are case insensitive.
+ *
+ * You can also pass `NULL' to the image, and that will lookup for
+ * a type with the given namespace and name in all of the loaded
+ * assemblies: notice that since there might be a name clash in this
+ * case, passing NULL is not encouraged if you need a precise type.
+ *
+ */
+MonoClass *
+mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
 {
-       char *base_dir, *name;
-       MonoImage *res;
-       MonoTableInfo  *t = &image->tables [MONO_TABLE_FILE];
-       const char *fname;
-       guint32 fname_id;
+       MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
+       guint32 cols [MONO_TYPEDEF_SIZE];
+       const char *n;
+       const char *nspace;
+       guint32 i, visib;
 
-       if (fileidx < 1 || fileidx > t->rows)
-               return NULL;
-       fname_id = mono_metadata_decode_row_col (t, fileidx - 1, MONO_FILE_NAME);
-       fname = mono_metadata_string_heap (image, fname_id);
-       base_dir = g_path_get_dirname (image->name);
-       name = g_build_filename (base_dir, fname, NULL);
-       res = mono_image_open (name, NULL);
-       if (res) {
-               int i;
-               t = &res->tables [MONO_TABLE_MODULEREF];
-               //g_print ("loaded file %s from %s (%p)\n", name, image->name, image->assembly);
-               res->assembly = image->assembly;
-               for (i = 0; i < t->rows; ++i) {
-                       if (res->modules [i] && !res->modules [i]->assembly)
-                               res->modules [i]->assembly = image->assembly;
-               }
+       /* add a cache if needed */
+       for (i = 1; i <= t->rows; ++i) {
+               mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
+               /* nested types are accessed from the nesting name */
+               visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
+               if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
+                       continue;
+               n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
+               nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
+               if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
+                       return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
        }
-       g_free (name);
-       g_free (base_dir);
-       return res;
+       return NULL;
 }
 
 static MonoClass*
@@ -2219,6 +2544,22 @@ return_nested_in (MonoClass *class, char *nested) {
        return NULL;
 }
 
+
+/**
+ * mono_class_from_name_case:
+ * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
+ * @name_space: the type namespace
+ * @name: the type short name.
+ *
+ * Obtains a MonoClass with a given namespace and a given name which
+ * is located in the given MonoImage.   
+ *
+ * You can also pass `NULL' to the image, and that will lookup for
+ * a type with the given namespace and name in all of the loaded
+ * assemblies: notice that since there might be a name clash in this
+ * case, passing NULL is not encouraged if you need a precise type.
+ *
+ */
 MonoClass *
 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
 {
@@ -2248,35 +2589,31 @@ mono_class_from_name (MonoImage *image, const char* name_space, const char *name
                token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
 
        mono_loader_unlock ();
-       
-       if (!token) {
+
+       if (!token)
+               return NULL;
+
+       if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
                MonoTableInfo  *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
                guint32 cols [MONO_EXP_TYPE_SIZE];
-               int i;
+               guint32 idx, impl;
 
-               for (i = 0; i < t->rows; ++i) {
-                       const char *ename, *enspace;
-                       mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
-                       ename = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
-                       enspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
-
-                       if (strcmp (name, ename) == 0 && strcmp (name_space, enspace) == 0) {
-                               guint32 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
-                               if ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE) {
-                                       loaded_image = load_file_for_image (image, impl >> IMPLEMENTATION_BITS);
-                                       if (!loaded_image)
-                                               return NULL;
-                                       class = mono_class_from_name (loaded_image, name_space, name);
-                                       if (nested)
-                                               return return_nested_in (class, nested);
-                                       return class;
-                               } else {
-                                       g_error ("not yet implemented");
-                               }
-                       }
+               idx = mono_metadata_token_index (token);
+
+               mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
+
+               impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
+               if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
+                       loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
+                       if (!loaded_image)
+                               return NULL;
+                       class = mono_class_from_name (loaded_image, name_space, name);
+                       if (nested)
+                               return return_nested_in (class, nested);
+                       return class;
+               } else {
+                       g_error ("not yet implemented");
                }
-               /*g_warning ("token not found for %s.%s in image %s", name_space, name, image->name);*/
-               return NULL;
        }
 
        token = MONO_TOKEN_TYPE_DEF | token;
@@ -2289,13 +2626,14 @@ mono_class_from_name (MonoImage *image, const char* name_space, const char *name
 
 gboolean
 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc, 
-                                                  gboolean check_interfaces)
+                          gboolean check_interfaces)
 {
-       if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+ again:
+       if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
                if ((klassc->interface_id <= klass->max_interface_id) &&
                        (klass->interface_offsets [klassc->interface_id] >= 0))
                        return TRUE;
-       } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+       } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
                int i;
 
                for (i = 0; i < klass->interface_count; i ++) {
@@ -2304,8 +2642,18 @@ mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
                                return TRUE;
                }
        } else {
-               if (!(klass->flags & TYPE_ATTRIBUTE_INTERFACE) && mono_class_has_parent (klass, klassc))
+               if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
                        return TRUE;
+               if (klass->generic_inst) {
+                       MonoType *parent = klass->generic_inst->parent;
+                       if (!parent)
+                               return FALSE;
+
+                       if (mono_metadata_type_equal (parent, &klassc->byval_arg))
+                               return TRUE;
+                       klass = mono_class_from_mono_type (parent);
+                       goto again;
+               }
        }
 
        /* 
@@ -2327,7 +2675,7 @@ mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
        if (!oklass->inited)
                mono_class_init (oklass);
 
-       if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
+       if (MONO_CLASS_IS_INTERFACE (klass)) {
                if ((klass->interface_id <= oklass->max_interface_id) &&
                    (oklass->interface_offsets [klass->interface_id] != -1))
                        return TRUE;
@@ -2338,6 +2686,10 @@ mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
                        if (oklass->rank != klass->rank)
                                return FALSE;
 
+                       /* vectors vs. one dimensional arrays */
+                       if (oklass->byval_arg.type != klass->byval_arg.type)
+                               return FALSE;
+
                        eclass = klass->cast_class;
                        eoclass = oklass->cast_class;
 
@@ -2450,9 +2802,10 @@ mono_array_element_size (MonoClass *ac)
 }
 
 gpointer
-mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
+mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
+             MonoGenericContext *context)
 {
-       if (image->assembly->dynamic) {
+       if (image->dynamic) {
                gpointer obj = mono_lookup_dynamic_token (image, token);
 
                switch (token & 0xff000000) {
@@ -2481,7 +2834,7 @@ mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
                MonoClass *class;
                if (handle_class)
                        *handle_class = mono_defaults.typehandle_class;
-               class = mono_class_get (image, token);
+               class = mono_class_get_full (image, token, context);
                mono_class_init (class);
                /* We return a MonoType* as handle */
                return &class->byval_arg;
@@ -2490,14 +2843,14 @@ mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
                MonoClass *class;
                if (handle_class)
                        *handle_class = mono_defaults.typehandle_class;
-               class = mono_class_create_from_typespec (image, token);
+               class = mono_class_create_from_typespec (image, token, context);
                mono_class_init (class);
                return &class->byval_arg;
        }
        case MONO_TOKEN_FIELD_DEF: {
                MonoClass *class;
                guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
-               class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
+               class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
                mono_class_init (class);
                if (handle_class)
                        *handle_class = mono_defaults.fieldhandle_class;
@@ -2505,7 +2858,7 @@ mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
        }
        case MONO_TOKEN_METHOD_DEF: {
                MonoMethod *meth;
-               meth = mono_get_method (image, token, NULL);
+               meth = mono_get_method_full (image, token, NULL, context);
                if (handle_class)
                        *handle_class = mono_defaults.methodhandle_class;
                return meth;
@@ -2519,13 +2872,13 @@ mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class)
                if (*sig == 0x6) { /* it's a field */
                        MonoClass *klass;
                        MonoClassField *field;
-                       field = mono_field_from_token (image, token, &klass);
+                       field = mono_field_from_token (image, token, &klass, context);
                        if (handle_class)
                                *handle_class = mono_defaults.fieldhandle_class;
                        return field;
                } else {
                        MonoMethod *meth;
-                       meth = mono_get_method (image, token, NULL);
+                       meth = mono_get_method_full (image, token, NULL, context);
                        if (handle_class)
                                *handle_class = mono_defaults.methodhandle_class;
                        return meth;
@@ -2556,5 +2909,624 @@ mono_lookup_dynamic_token (MonoImage *image, guint32 token)
        return lookup_dynamic (image, token);
 }
 
+MonoImage*
+mono_class_get_image (MonoClass *klass)
+{
+       return klass->image;
+}
+
+/**
+ * mono_class_get_element_class:
+ * @klass: the MonoClass to act on
+ *
+ * Returns the element class of an array or an enumeration.
+ */
+MonoClass*
+mono_class_get_element_class (MonoClass *klass)
+{
+       return klass->element_class;
+}
+
+/**
+ * mono_class_is_valuetype:
+ * @klass: the MonoClass to act on
+ *
+ * Returns true if the MonoClass represents a ValueType.
+ */
+gboolean
+mono_class_is_valuetype (MonoClass *klass)
+{
+       return klass->valuetype;
+}
+
+/**
+ * mono_class_is_enum:
+ * @klass: the MonoClass to act on
+ *
+ * Returns true if the MonoClass represents an enumeration.
+ */
+gboolean
+mono_class_is_enum (MonoClass *klass)
+{
+       return klass->enumtype;
+}
+
+/**
+ * mono_class_enum_basetype:
+ * @klass: the MonoClass to act on
+ *
+ * Returns the underlying type representation for an enumeration.
+ */
+MonoType*
+mono_class_enum_basetype (MonoClass *klass)
+{
+       return klass->enum_basetype;
+}
+
+/**
+ * mono_class_get_parent
+ * @klass: the MonoClass to act on
+ *
+ * Returns the parent class for this class.
+ */
+MonoClass*
+mono_class_get_parent (MonoClass *klass)
+{
+       return klass->parent;
+}
+
+/**
+ * mono_class_get_nesting_type;
+ * @klass: the MonoClass to act on
+ *
+ * Returns the container type where this type is nested or NULL if this type is not a nested type.
+ */
+MonoClass*
+mono_class_get_nesting_type (MonoClass *klass)
+{
+       return klass->nested_in;
+}
+
+/**
+ * mono_class_get_rank:
+ * @klass: the MonoClass to act on
+ *
+ * Returns the rank for the array (the number of dimensions).
+ */
+int
+mono_class_get_rank (MonoClass *klass)
+{
+       return klass->rank;
+}
+
+/**
+ * mono_class_get_flags:
+ * @klass: the MonoClass to act on
+ *
+ * The type flags from the TypeDef table from the metadata.
+ * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
+ * different values.
+ *
+ * Returns the flags from the TypeDef table.
+ */
+guint32
+mono_class_get_flags (MonoClass *klass)
+{
+       return klass->flags;
+}
+
+/**
+ * mono_class_get_name
+ * @klass: the MonoClass to act on
+ *
+ * Returns the name of the class.
+ */
+const char*
+mono_class_get_name (MonoClass *klass)
+{
+       return klass->name;
+}
+
+/**
+ * mono_class_get_namespace:
+ * @klass: the MonoClass to act on
+ *
+ * Returns the namespace of the class.
+ */
+const char*
+mono_class_get_namespace (MonoClass *klass)
+{
+       return klass->name_space;
+}
+
+/**
+ * mono_class_get_type:
+ * @klass: the MonoClass to act on
+ *
+ * This method returns the internal Type representation for the class.
+ *
+ * Returns the MonoType from the class.
+ */
+MonoType*
+mono_class_get_type (MonoClass *klass)
+{
+       return &klass->byval_arg;
+}
+
+/**
+ * mono_class_get_byref_type:
+ * @klass: the MonoClass to act on
+ *
+ * 
+ */
+MonoType*
+mono_class_get_byref_type (MonoClass *klass)
+{
+       return &klass->this_arg;
+}
+
+/**
+ * mono_class_num_fields:
+ * @klass: the MonoClass to act on
+ *
+ * Returns the number of static and instance fields in the class.
+ */
+int
+mono_class_num_fields (MonoClass *klass)
+{
+       return klass->field.count;
+}
+
+/**
+ * mono_class_num_methods:
+ * @klass: the MonoClass to act on
+ *
+ * Returns the number of methods in the class.
+ */
+int
+mono_class_num_methods (MonoClass *klass)
+{
+       return klass->method.count;
+}
+
+/**
+ * mono_class_num_properties
+ * @klass: the MonoClass to act on
+ *
+ * Returns the number of properties in the class.
+ */
+int
+mono_class_num_properties (MonoClass *klass)
+{
+       return klass->property.count;
+}
+
+/**
+ * mono_class_num_events:
+ * @klass: the MonoClass to act on
+ *
+ * Returns the number of events in the class.
+ */
+int
+mono_class_num_events (MonoClass *klass)
+{
+       return klass->event.count;
+}
+
+/**
+ * mono_class_get_fields:
+ * @klass: the MonoClass to act on
+ *
+ * This routine is an iterator routine for retrieving the fields in a class.
+ *
+ * You must pass a gpointer that points to zero and is treated as an opaque handle to
+ * iterate over all of the elements.  When no more values are
+ * available, the return value is NULL.
+ *
+ * Returns a `MonoClassField *' on each iteration, or NULL when no more fields are available.
+ */
+MonoClassField*
+mono_class_get_fields (MonoClass* klass, gpointer *iter)
+{
+       MonoClassField* field;
+       if (!iter)
+               return NULL;
+       if (!klass->inited)
+               mono_class_init (klass);
+       if (!*iter) {
+               /* start from the first */
+               if (klass->field.count) {
+                       return *iter = &klass->fields [0];
+               } else {
+                       /* no fields */
+                       return NULL;
+               }
+       }
+       field = *iter;
+       field++;
+       if (field < &klass->fields [klass->field.count]) {
+               return *iter = field;
+       }
+       return NULL;
+}
+
+/**
+ * mono_class_get_methods
+ * @klass: the MonoClass to act on
+ *
+ * This routine is an iterator routine for retrieving the fields in a class.
+ *
+ * You must pass a gpointer that points to zero and is treated as an opaque handle to
+ * iterate over all of the elements.  When no more values are
+ * available, the return value is NULL.
+ *
+ * Returns a MonoMethod on each iteration or NULL when no more methods are available.
+ */
+MonoMethod*
+mono_class_get_methods (MonoClass* klass, gpointer *iter)
+{
+       MonoMethod** method;
+       if (!iter)
+               return NULL;
+       if (!klass->inited)
+               mono_class_init (klass);
+       if (!*iter) {
+               /* start from the first */
+               if (klass->method.count) {
+                       *iter = &klass->methods [0];
+                       return klass->methods [0];
+               } else {
+                       /* no method */
+                       return NULL;
+               }
+       }
+       method = *iter;
+       method++;
+       if (method < &klass->methods [klass->method.count]) {
+               *iter = method;
+               return *method;
+       }
+       return NULL;
+}
+
+/**
+ * mono_class_get_properties:
+ * @klass: the MonoClass to act on
+ *
+ * This routine is an iterator routine for retrieving the properties in a class.
+ *
+ * You must pass a gpointer that points to zero and is treated as an opaque handle to
+ * iterate over all of the elements.  When no more values are
+ * available, the return value is NULL.
+ *
+ * Returns a `MonoProperty *' on each invocation, or NULL when no more are available.
+ */
+MonoProperty*
+mono_class_get_properties (MonoClass* klass, gpointer *iter)
+{
+       MonoProperty* property;
+       if (!iter)
+               return NULL;
+       if (!klass->inited)
+               mono_class_init (klass);
+       if (!*iter) {
+               /* start from the first */
+               if (klass->property.count) {
+                       return *iter = &klass->properties [0];
+               } else {
+                       /* no fields */
+                       return NULL;
+               }
+       }
+       property = *iter;
+       property++;
+       if (property < &klass->properties [klass->property.count]) {
+               return *iter = property;
+       }
+       return NULL;
+}
+
+/**
+ * mono_class_get_events:
+ * @klass: the MonoClass to act on
+ *
+ * This routine is an iterator routine for retrieving the properties in a class.
+ *
+ * You must pass a gpointer that points to zero and is treated as an opaque handle to
+ * iterate over all of the elements.  When no more values are
+ * available, the return value is NULL.
+ *
+ * Returns a `MonoEvent *' on each invocation, or NULL when no more are available.
+ */
+MonoEvent*
+mono_class_get_events (MonoClass* klass, gpointer *iter)
+{
+       MonoEvent* event;
+       if (!iter)
+               return NULL;
+       if (!klass->inited)
+               mono_class_init (klass);
+       if (!*iter) {
+               /* start from the first */
+               if (klass->event.count) {
+                       return *iter = &klass->events [0];
+               } else {
+                       /* no fields */
+                       return NULL;
+               }
+       }
+       event = *iter;
+       event++;
+       if (event < &klass->events [klass->event.count]) {
+               return *iter = event;
+       }
+       return NULL;
+}
+
+/**
+ * mono_class_get_interfaaces
+ * @klass: the MonoClass to act on
+ *
+ * This routine is an iterator routine for retrieving the interfaces implemented by this class.
+ *
+ * You must pass a gpointer that points to zero and is treated as an opaque handle to
+ * iterate over all of the elements.  When no more values are
+ * available, the return value is NULL.
+ *
+ * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
+ */
+MonoClass*
+mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
+{
+       MonoClass** iface;
+       if (!iter)
+               return NULL;
+       if (!klass->inited)
+               mono_class_init (klass);
+       if (!*iter) {
+               /* start from the first */
+               if (klass->interface_count) {
+                       *iter = &klass->interfaces [0];
+                       return klass->interfaces [0];
+               } else {
+                       /* no interface */
+                       return NULL;
+               }
+       }
+       iface = *iter;
+       iface++;
+       if (iface < &klass->interfaces [klass->interface_count]) {
+               *iter = iface;
+               return *iface;
+       }
+       return NULL;
+}
+
+/**
+ * mono_class_get_interfaaces
+ * @klass: the MonoClass to act on
+ *
+ * This routine is an iterator routine for retrieving the nested types of a class.
+ *
+ * You must pass a gpointer that points to zero and is treated as an opaque handle to
+ * iterate over all of the elements.  When no more values are
+ * available, the return value is NULL.
+ *
+ * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
+ */
+MonoClass*
+mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
+{
+       GList *item;
+       if (!iter)
+               return NULL;
+       if (!klass->inited)
+               mono_class_init (klass);
+       if (!*iter) {
+               /* start from the first */
+               if (klass->nested_classes) {
+                       *iter = klass->nested_classes;
+                       return klass->nested_classes->data;
+               } else {
+                       /* no nested types */
+                       return NULL;
+               }
+       }
+       item = *iter;
+       item = item->next;
+       if (item) {
+               *iter = item;
+               return item->data;
+       }
+       return NULL;
+}
+
+/**
+ * mono_field_get_name:
+ * @field: the MonoClassField to act on
+ *
+ * Returns the name of the field.
+ */
+const char*
+mono_field_get_name (MonoClassField *field)
+{
+       return field->name;
+}
 
+/**
+ * mono_field_get_type:
+ * @field: the MonoClassField to act on
+ *
+ * Returns MonoType of the field.
+ */
+MonoType*
+mono_field_get_type (MonoClassField *field)
+{
+       return field->type;
+}
+
+/**
+ * mono_field_get_type:
+ * @field: the MonoClassField to act on
+ *
+ * Returns MonoClass where the field was defined.
+ */
+MonoClass*
+mono_field_get_parent (MonoClassField *field)
+{
+       return field->parent;
+}
+
+/**
+ * mono_field_get_flags;
+ * @field: the MonoClassField to act on
+ *
+ * The metadata flags for a field are encoded using the
+ * FIELD_ATTRIBUTE_* constants.  See the tabledefs.h file for details.
+ *
+ * Returns the flags for the field.
+ */
+guint32
+mono_field_get_flags (MonoClassField *field)
+{
+       return field->type->attrs;
+}
+
+/**
+ * mono_property_get_name: 
+ * @prop: the MonoProperty to act on
+ *
+ * Returns the name of the property
+ */
+const char*
+mono_property_get_name (MonoProperty *prop)
+{
+       return prop->name;
+}
+
+/**
+ * mono_property_get_set_method
+ * @prop: the MonoProperty to act on.
+ *
+ * Returns the setter method of the property (A MonoMethod)
+ */
+MonoMethod*
+mono_property_get_set_method (MonoProperty *prop)
+{
+       return prop->set;
+}
+
+/**
+ * mono_property_get_get_method
+ * @prop: the MonoProperty to act on.
+ *
+ * Returns the setter method of the property (A MonoMethod)
+ */
+MonoMethod*
+mono_property_get_get_method (MonoProperty *prop)
+{
+       return prop->get;
+}
+
+/**
+ * mono_property_get_parent:
+ * @prop: the MonoProperty to act on.
+ *
+ * Returns the MonoClass where the property was defined.
+ */
+MonoClass*
+mono_property_get_parent (MonoProperty *prop)
+{
+       return prop->parent;
+}
+
+/**
+ * mono_property_get_flags:
+ * @prop: the MonoProperty to act on.
+ *
+ * The metadata flags for a property are encoded using the
+ * PROPERTY_ATTRIBUTE_* constants.  See the tabledefs.h file for details.
+ *
+ * Returns the flags for the property.
+ */
+guint32
+mono_property_get_flags (MonoProperty *prop)
+{
+       return prop->attrs;
+}
+
+/**
+ * mono_event_get_name:
+ * @event: the MonoEvent to act on
+ *
+ * Returns the name of the event.
+ */
+const char*
+mono_event_get_name (MonoEvent *event)
+{
+       return event->name;
+}
+
+/**
+ * mono_event_get_add_method:
+ * @event: The MonoEvent to act on.
+ *
+ * Returns the `add' method for the event (a MonoMethod).
+ */
+MonoMethod*
+mono_event_get_add_method (MonoEvent *event)
+{
+       return event->add;
+}
+
+/**
+ * mono_event_get_remove_method:
+ * @event: The MonoEvent to act on.
+ *
+ * Returns the `remove' method for the event (a MonoMethod).
+ */
+MonoMethod*
+mono_event_get_remove_method (MonoEvent *event)
+{
+       return event->remove;
+}
+
+/**
+ * mono_event_get_raise_method:
+ * @event: The MonoEvent to act on.
+ *
+ * Returns the `raise' method for the event (a MonoMethod).
+ */
+MonoMethod*
+mono_event_get_raise_method (MonoEvent *event)
+{
+       return event->raise;
+}
+
+/**
+ * mono_event_get_parent:
+ * @event: the MonoEvent to act on.
+ *
+ * Returns the MonoClass where the event is defined.
+ */
+MonoClass*
+mono_event_get_parent (MonoEvent *event)
+{
+       return event->parent;
+}
+
+/**
+ * mono_event_get_flags
+ * @event: the MonoEvent to act on.
+ *
+ * The metadata flags for an event are encoded using the
+ * EVENT_* constants.  See the tabledefs.h file for details.
+ *
+ * Returns the flags for the event.
+ */
+guint32
+mono_event_get_flags (MonoEvent *event)
+{
+       return event->attrs;
+}