3 * System.Type icalls and related reflection queries.
6 * Paolo Molaro (lupus@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Rodrigo Kumpera
11 * Copyright 2016 Microsoft
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include "mono/utils/mono-membar.h"
17 #include "mono/metadata/reflection-internals.h"
18 #include "mono/metadata/tabledefs.h"
19 #include "mono/metadata/metadata-internals.h"
20 #include <mono/metadata/profiler-private.h>
21 #include "mono/metadata/class-internals.h"
22 #include "mono/metadata/gc-internals.h"
23 #include "mono/metadata/domain-internals.h"
24 #include "mono/metadata/opcodes.h"
25 #include "mono/metadata/assembly.h"
26 #include "mono/metadata/object-internals.h"
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/marshal.h>
29 #include <mono/metadata/security-manager.h>
30 #include <mono/metadata/reflection-cache.h>
31 #include <mono/metadata/sre-internals.h>
40 #include "mono-endian.h"
41 #include <mono/metadata/gc-internals.h>
42 #include <mono/metadata/mempool-internals.h>
43 #include <mono/metadata/security-core-clr.h>
44 #include <mono/metadata/debug-helpers.h>
45 #include <mono/metadata/verify-internals.h>
46 #include <mono/metadata/mono-ptr-array.h>
47 #include <mono/utils/mono-string.h>
48 #include <mono/utils/mono-error-internals.h>
49 #include <mono/utils/checked-build.h>
50 #include <mono/utils/mono-counters.h>
52 static void get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types);
53 static MonoType* mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error);
55 /* Class lazy loading functions */
56 static GENERATE_GET_CLASS_WITH_CACHE (mono_assembly, "System.Reflection", "MonoAssembly")
57 static GENERATE_GET_CLASS_WITH_CACHE (mono_module, "System.Reflection", "MonoModule")
58 static GENERATE_GET_CLASS_WITH_CACHE (mono_method, "System.Reflection", "MonoMethod");
59 static GENERATE_GET_CLASS_WITH_CACHE (mono_cmethod, "System.Reflection", "MonoCMethod");
60 static GENERATE_GET_CLASS_WITH_CACHE (mono_field, "System.Reflection", "MonoField");
61 static GENERATE_GET_CLASS_WITH_CACHE (mono_event, "System.Reflection", "MonoEvent");
62 static GENERATE_GET_CLASS_WITH_CACHE (mono_property, "System.Reflection", "MonoProperty");
63 static GENERATE_GET_CLASS_WITH_CACHE (mono_parameter_info, "System.Reflection", "MonoParameterInfo");
64 static GENERATE_GET_CLASS_WITH_CACHE (missing, "System.Reflection", "Missing");
65 static GENERATE_GET_CLASS_WITH_CACHE (method_body, "System.Reflection", "MethodBody");
66 static GENERATE_GET_CLASS_WITH_CACHE (local_variable_info, "System.Reflection", "LocalVariableInfo");
67 static GENERATE_GET_CLASS_WITH_CACHE (exception_handling_clause, "System.Reflection", "ExceptionHandlingClause");
68 static GENERATE_GET_CLASS_WITH_CACHE (type_builder, "System.Reflection.Emit", "TypeBuilder");
69 static GENERATE_GET_CLASS_WITH_CACHE (dbnull, "System", "DBNull");
72 static int class_ref_info_handle_count;
75 mono_reflection_init (void)
77 mono_reflection_emit_init ();
79 mono_counters_register ("MonoClass::ref_info_handle count",
80 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &class_ref_info_handle_count);
85 * mono_class_get_ref_info:
87 * Return the type builder/generic param builder corresponding to KLASS, if it exists.
90 mono_class_get_ref_info (MonoClass *klass)
92 MONO_REQ_GC_UNSAFE_MODE;
93 guint32 ref_info_handle = mono_class_get_ref_info_handle (klass);
95 if (ref_info_handle == 0)
96 return MONO_HANDLE_NEW (MonoObject, NULL);
98 return mono_gchandle_get_target_handle (ref_info_handle);
102 mono_class_has_ref_info (MonoClass *klass)
104 MONO_REQ_GC_UNSAFE_MODE;
105 return 0 != mono_class_get_ref_info_handle (klass);
109 mono_class_get_ref_info_raw (MonoClass *klass)
111 /* FIXME callers of mono_class_get_ref_info_raw should use handles */
112 MONO_REQ_GC_UNSAFE_MODE;
113 guint32 ref_info_handle = mono_class_get_ref_info_handle (klass);
115 if (ref_info_handle == 0)
118 return mono_gchandle_get_target (ref_info_handle);
122 mono_class_set_ref_info (MonoClass *klass, MonoObjectHandle obj)
124 MONO_REQ_GC_UNSAFE_MODE;
126 guint32 candidate = mono_gchandle_from_handle (obj, FALSE);
127 guint32 handle = mono_class_set_ref_info_handle (klass, candidate);
128 ++class_ref_info_handle_count;
130 if (handle != candidate)
131 mono_gchandle_free (candidate);
135 mono_class_free_ref_info (MonoClass *klass)
137 MONO_REQ_GC_NEUTRAL_MODE;
138 guint32 handle = mono_class_get_ref_info_handle (klass);
141 mono_gchandle_free (handle);
142 mono_class_set_ref_info_handle (klass, 0);
148 mono_custom_attrs_free (MonoCustomAttrInfo *ainfo)
150 MONO_REQ_GC_NEUTRAL_MODE;
152 if (ainfo && !ainfo->cached)
158 reflected_equal (gconstpointer a, gconstpointer b)
160 const ReflectedEntry *ea = (const ReflectedEntry *)a;
161 const ReflectedEntry *eb = (const ReflectedEntry *)b;
163 return (ea->item == eb->item) && (ea->refclass == eb->refclass);
167 reflected_hash (gconstpointer a) {
168 const ReflectedEntry *ea = (const ReflectedEntry *)a;
169 /* Combine hashes for item and refclass. Identical to boost's hash_combine */
170 guint seed = mono_aligned_addr_hash (ea->item) + 0x9e3779b9;
171 seed ^= mono_aligned_addr_hash (ea->refclass) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
177 clear_cached_object (MonoDomain *domain, gpointer o, MonoClass *klass)
179 mono_domain_lock (domain);
180 if (domain->refobject_hash) {
182 gpointer orig_pe, orig_value;
186 if (mono_g_hash_table_lookup_extended (domain->refobject_hash, &pe, &orig_pe, &orig_value)) {
187 mono_g_hash_table_remove (domain->refobject_hash, &pe);
188 FREE_REFENTRY (orig_pe);
191 mono_domain_unlock (domain);
194 #ifdef REFENTRY_REQUIRES_CLEANUP
196 cleanup_refobject_hash (gpointer key, gpointer value, gpointer user_data)
203 mono_reflection_cleanup_domain (MonoDomain *domain)
205 if (domain->refobject_hash) {
206 /*let's avoid scanning the whole hashtable if not needed*/
207 #ifdef REFENTRY_REQUIRES_CLEANUP
208 mono_g_hash_table_foreach (domain->refobject_hash, cleanup_refobject_hash, NULL);
210 mono_g_hash_table_destroy (domain->refobject_hash);
211 domain->refobject_hash = NULL;
217 * mono_assembly_get_object:
218 * \param domain an app domain
219 * \param assembly an assembly
220 * \returns a \c System.Reflection.Assembly object representing the \c MonoAssembly \p assembly.
222 MonoReflectionAssembly*
223 mono_assembly_get_object (MonoDomain *domain, MonoAssembly *assembly)
225 HANDLE_FUNCTION_ENTER ();
227 MonoReflectionAssemblyHandle result = mono_assembly_get_object_handle (domain, assembly, &error);
228 mono_error_cleanup (&error); /* FIXME new API that doesn't swallow the error */
229 HANDLE_FUNCTION_RETURN_OBJ (result);
232 static MonoReflectionAssemblyHandle
233 assembly_object_construct (MonoDomain *domain, MonoClass *unused_klass, MonoAssembly *assembly, gpointer user_data, MonoError *error)
236 MonoReflectionAssemblyHandle res = MONO_HANDLE_NEW (MonoReflectionAssembly, mono_object_new_checked (domain, mono_class_get_mono_assembly_class (), error));
237 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE));
238 MONO_HANDLE_SETVAL (res, assembly, MonoAssembly*, assembly);
243 * mono_assembly_get_object_handle:
244 * @domain: an app domain
245 * @assembly: an assembly
247 * Return an System.Reflection.Assembly object representing the MonoAssembly @assembly.
249 MonoReflectionAssemblyHandle
250 mono_assembly_get_object_handle (MonoDomain *domain, MonoAssembly *assembly, MonoError *error)
253 return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionAssemblyHandle, assembly, NULL, assembly_object_construct, NULL);
257 * mono_module_get_object:
259 MonoReflectionModule*
260 mono_module_get_object (MonoDomain *domain, MonoImage *image)
262 HANDLE_FUNCTION_ENTER ();
264 MonoReflectionModuleHandle result = mono_module_get_object_handle (domain, image, &error);
265 mono_error_cleanup (&error);
266 HANDLE_FUNCTION_RETURN_OBJ (result);
269 static MonoReflectionModuleHandle
270 module_object_construct (MonoDomain *domain, MonoClass *unused_klass, MonoImage *image, gpointer user_data, MonoError *error)
275 MonoReflectionModuleHandle res = MONO_HANDLE_NEW (MonoReflectionModule, mono_object_new_checked (domain, mono_class_get_mono_module_class (), error));
279 MONO_HANDLE_SETVAL (res, image, MonoImage *, image);
280 MonoReflectionAssemblyHandle assm_obj = mono_assembly_get_object_handle (domain, image->assembly, error);
283 MONO_HANDLE_SET (res, assembly, assm_obj);
285 MONO_HANDLE_SET (res, fqname, mono_string_new_handle (domain, image->name, error));
288 basename = g_path_get_basename (image->name);
289 MONO_HANDLE_SET (res, name, mono_string_new_handle (domain, basename, error));
292 MONO_HANDLE_SET (res, scopename, mono_string_new_handle (domain, image->module_name, error));
299 if (image->assembly->image == image) {
300 token = mono_metadata_make_token (MONO_TABLE_MODULE, 1);
303 if (image->assembly->image->modules) {
304 for (i = 0; i < image->assembly->image->module_count; i++) {
305 if (image->assembly->image->modules [i] == image)
306 token = mono_metadata_make_token (MONO_TABLE_MODULEREF, i + 1);
308 g_assert (token != 0);
311 MONO_HANDLE_SETVAL (res, token, guint32, token);
315 return MONO_HANDLE_CAST (MonoReflectionModule, NULL_HANDLE);
318 MonoReflectionModuleHandle
319 mono_module_get_object_handle (MonoDomain *domain, MonoImage *image, MonoError *error)
322 return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionModuleHandle, image, NULL, module_object_construct, NULL);
326 * mono_module_file_get_object:
328 MonoReflectionModule*
329 mono_module_file_get_object (MonoDomain *domain, MonoImage *image, int table_index)
331 HANDLE_FUNCTION_ENTER ();
333 MonoReflectionModuleHandle result = mono_module_file_get_object_handle (domain, image, table_index, &error);
334 mono_error_cleanup (&error);
335 HANDLE_FUNCTION_RETURN_OBJ (result);
338 MonoReflectionModuleHandle
339 mono_module_file_get_object_handle (MonoDomain *domain, MonoImage *image, int table_index, MonoError *error)
341 MonoTableInfo *table;
342 guint32 cols [MONO_FILE_SIZE];
349 MonoReflectionModuleHandle res = MONO_HANDLE_NEW (MonoReflectionModule, mono_object_new_checked (domain, mono_class_get_mono_module_class (), error));
353 table = &image->tables [MONO_TABLE_FILE];
354 g_assert (table_index < table->rows);
355 mono_metadata_decode_row (table, table_index, cols, MONO_FILE_SIZE);
357 MONO_HANDLE_SETVAL (res, image, MonoImage*, NULL);
358 MonoReflectionAssemblyHandle assm_obj = mono_assembly_get_object_handle (domain, image->assembly, error);
361 MONO_HANDLE_SET (res, assembly, assm_obj);
362 name = mono_metadata_string_heap (image, cols [MONO_FILE_NAME]);
364 /* Check whenever the row has a corresponding row in the moduleref table */
365 table = &image->tables [MONO_TABLE_MODULEREF];
366 for (i = 0; i < table->rows; ++i) {
367 name_idx = mono_metadata_decode_row_col (table, i, MONO_MODULEREF_NAME);
368 val = mono_metadata_string_heap (image, name_idx);
369 if (strcmp (val, name) == 0)
370 MONO_HANDLE_SETVAL (res, image, MonoImage*, image->modules [i]);
373 MONO_HANDLE_SET (res, fqname, mono_string_new_handle (domain, name, error));
376 MONO_HANDLE_SET (res, name, mono_string_new_handle (domain, name, error));
379 MONO_HANDLE_SET (res, scopename, mono_string_new_handle (domain, name, error));
382 MONO_HANDLE_SETVAL (res, is_resource, MonoBoolean, cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA);
383 MONO_HANDLE_SETVAL (res, token, guint32, mono_metadata_make_token (MONO_TABLE_FILE, table_index + 1));
387 return MONO_HANDLE_CAST (MonoReflectionModule, NULL_HANDLE);
391 mono_type_normalize (MonoType *type)
394 MonoGenericClass *gclass;
395 MonoGenericInst *ginst;
397 MonoGenericContainer *gcontainer;
398 MonoType **argv = NULL;
399 gboolean is_denorm_gtd = TRUE, requires_rebind = FALSE;
401 if (type->type != MONO_TYPE_GENERICINST)
404 gclass = type->data.generic_class;
405 ginst = gclass->context.class_inst;
409 gtd = gclass->container_class;
410 gcontainer = mono_class_get_generic_container (gtd);
411 argv = g_newa (MonoType*, ginst->type_argc);
413 for (i = 0; i < ginst->type_argc; ++i) {
414 MonoType *t = ginst->type_argv [i], *norm;
415 if (t->type != MONO_TYPE_VAR || t->data.generic_param->num != i || t->data.generic_param->owner != gcontainer)
416 is_denorm_gtd = FALSE;
417 norm = mono_type_normalize (t);
420 requires_rebind = TRUE;
424 return type->byref == gtd->byval_arg.byref ? >d->byval_arg : >d->this_arg;
426 if (requires_rebind) {
427 MonoClass *klass = mono_class_bind_generic_parameters (gtd, ginst->type_argc, argv, gclass->is_dynamic);
428 return type->byref == klass->byval_arg.byref ? &klass->byval_arg : &klass->this_arg;
434 * mono_type_get_object:
435 * @domain: an app domain
438 * Return an System.MonoType object representing the type @type.
441 mono_type_get_object (MonoDomain *domain, MonoType *type)
444 MonoReflectionType *ret = mono_type_get_object_checked (domain, type, &error);
445 mono_error_cleanup (&error);
451 mono_type_get_object_checked (MonoDomain *domain, MonoType *type, MonoError *error)
454 MonoReflectionType *res;
459 g_assert (type != NULL);
460 klass = mono_class_from_mono_type (type);
462 /*we must avoid using @type as it might have come
463 * from a mono_metadata_type_dup and the caller
464 * expects that is can be freed.
465 * Using the right type from
467 type = klass->byval_arg.byref == type->byref ? &klass->byval_arg : &klass->this_arg;
469 /* void is very common */
470 if (type->type == MONO_TYPE_VOID && domain->typeof_void)
471 return (MonoReflectionType*)domain->typeof_void;
474 * If the vtable of the given class was already created, we can use
475 * the MonoType from there and avoid all locking and hash table lookups.
477 * We cannot do this for TypeBuilders as mono_reflection_create_runtime_class expects
478 * that the resulting object is different.
480 if (type == &klass->byval_arg && !image_is_dynamic (klass->image)) {
481 MonoVTable *vtable = mono_class_try_get_vtable (domain, klass);
482 if (vtable && vtable->type)
483 return (MonoReflectionType *)vtable->type;
486 mono_loader_lock (); /*FIXME mono_class_init and mono_class_vtable acquire it*/
487 mono_domain_lock (domain);
488 if (!domain->type_hash)
489 domain->type_hash = mono_g_hash_table_new_type ((GHashFunc)mono_metadata_type_hash,
490 (GCompareFunc)mono_metadata_type_equal, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, "domain reflection types table");
491 if ((res = (MonoReflectionType *)mono_g_hash_table_lookup (domain->type_hash, type))) {
492 mono_domain_unlock (domain);
493 mono_loader_unlock ();
497 /*Types must be normalized so a generic instance of the GTD get's the same inner type.
498 * For example in: Foo<A,B>; Bar<A> : Foo<A, Bar<A>>
499 * The second Bar will be encoded a generic instance of Bar with <A> as parameter.
500 * On all other places, Bar<A> will be encoded as the GTD itself. This is an implementation
501 * artifact of how generics are encoded and should be transparent to managed code so we
502 * need to weed out this diference when retrieving managed System.Type objects.
504 norm_type = mono_type_normalize (type);
505 if (norm_type != type) {
506 res = mono_type_get_object_checked (domain, norm_type, error);
507 if (!mono_error_ok (error))
509 mono_g_hash_table_insert (domain->type_hash, type, res);
510 mono_domain_unlock (domain);
511 mono_loader_unlock ();
515 if ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->is_dynamic && !type->data.generic_class->container_class->wastypebuilder) {
516 /* This can happen if a TypeBuilder for a generic class K<T,U>
517 * had reflection_create_generic_class) called on it, but not
518 * ves_icall_TypeBuilder_create_runtime_class. This can happen
519 * if the K`2 is refernced from a generic instantiation
520 * (e.g. K<int,string>) that appears as type argument
521 * (e.g. Dict<string,K<int,string>>), field (e.g. K<int,string>
522 * Foo) or method signature, parent class or any of the above
523 * in a nested class of some other TypeBuilder. Such an
524 * occurrence caused mono_reflection_type_get_handle to be
525 * called on the sre generic instance (K<int,string>) which
526 * required the container_class for the generic class K`2 to be
527 * set up, but the remainder of class construction for K`2 has
529 char * full_name = mono_type_get_full_name (klass);
530 /* I would have expected ReflectionTypeLoadException, but evidently .NET throws TLE in this case. */
531 mono_error_set_type_load_class (error, klass, "TypeBuilder.CreateType() not called for generic class %s", full_name);
533 mono_domain_unlock (domain);
534 mono_loader_unlock ();
538 if (mono_class_has_ref_info (klass) && !klass->wastypebuilder && !type->byref) {
539 mono_domain_unlock (domain);
540 mono_loader_unlock ();
541 return (MonoReflectionType *)mono_class_get_ref_info_raw (klass); /* FIXME use handles */
543 /* This is stored in vtables/JITted code so it has to be pinned */
544 res = (MonoReflectionType *)mono_object_new_pinned (domain, mono_defaults.runtimetype_class, error);
545 if (!mono_error_ok (error))
549 mono_g_hash_table_insert (domain->type_hash, type, res);
551 if (type->type == MONO_TYPE_VOID)
552 domain->typeof_void = (MonoObject*)res;
554 mono_domain_unlock (domain);
555 mono_loader_unlock ();
559 MonoReflectionTypeHandle
560 mono_type_get_object_handle (MonoDomain *domain, MonoType *type, MonoError *error)
562 /* NOTE: We happen to know that mono_type_get_object_checked returns
563 * pinned objects, so we can just wrap its return value in a handle for
564 * uniformity. If it ever starts returning unpinned, objects, this
565 * implementation would need to change!
567 return MONO_HANDLE_NEW (MonoReflectionType, mono_type_get_object_checked (domain, type, error));
571 * mono_method_get_object:
572 * \param domain an app domain
573 * \param method a method
574 * \param refclass the reflected type (can be NULL)
575 * \returns A \c System.Reflection.MonoMethod object representing the method \p method.
577 MonoReflectionMethod*
578 mono_method_get_object (MonoDomain *domain, MonoMethod *method, MonoClass *refclass)
580 HANDLE_FUNCTION_ENTER ();
582 MonoReflectionMethodHandle ret = mono_method_get_object_handle (domain, method, refclass, &error);
583 mono_error_cleanup (&error);
584 HANDLE_FUNCTION_RETURN_OBJ (ret);
587 static MonoReflectionMethodHandle
588 method_object_construct (MonoDomain *domain, MonoClass *refclass, MonoMethod *method, gpointer user_data, MonoError *error)
591 g_assert (refclass != NULL);
593 * We use the same C representation for methods and constructors, but the type
594 * name in C# is different.
600 if (*method->name == '.' && (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)) {
601 klass = mono_class_get_mono_cmethod_class ();
604 klass = mono_class_get_mono_method_class ();
606 MonoReflectionMethodHandle ret = MONO_HANDLE_NEW (MonoReflectionMethod, mono_object_new_checked (domain, klass, error));
609 MONO_HANDLE_SETVAL (ret, method, MonoMethod*, method);
611 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, &refclass->byval_arg, error);
615 MONO_HANDLE_SET (ret, reftype, rt);
620 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
624 * mono_method_get_object_handle:
625 * @domain: an app domain
627 * @refclass: the reflected type (can be NULL)
628 * @error: set on error.
630 * Return an System.Reflection.MonoMethod object representing the method @method.
631 * Returns NULL and sets @error on error.
633 MonoReflectionMethodHandle
634 mono_method_get_object_handle (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
638 refclass = method->klass;
640 return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionMethodHandle, method, refclass, method_object_construct, NULL);
643 * mono_method_get_object_checked:
644 * @domain: an app domain
646 * @refclass: the reflected type (can be NULL)
647 * @error: set on error.
649 * Return an System.Reflection.MonoMethod object representing the method @method.
650 * Returns NULL and sets @error on error.
652 MonoReflectionMethod*
653 mono_method_get_object_checked (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
655 HANDLE_FUNCTION_ENTER ();
656 MonoReflectionMethodHandle result = mono_method_get_object_handle (domain, method, refclass, error);
657 HANDLE_FUNCTION_RETURN_OBJ (result);
661 * mono_method_clear_object:
663 * Clear the cached reflection objects for the dynamic method METHOD.
666 mono_method_clear_object (MonoDomain *domain, MonoMethod *method)
669 g_assert (method_is_dynamic (method));
671 klass = method->klass;
673 clear_cached_object (domain, method, klass);
674 klass = klass->parent;
676 /* Added by mono_param_get_objects () */
677 clear_cached_object (domain, &(method->signature), NULL);
678 klass = method->klass;
680 clear_cached_object (domain, &(method->signature), klass);
681 klass = klass->parent;
686 * mono_field_get_object:
687 * \param domain an app domain
688 * \param klass a type
689 * \param field a field
690 * \returns A \c System.Reflection.MonoField object representing the field \p field
694 mono_field_get_object (MonoDomain *domain, MonoClass *klass, MonoClassField *field)
696 HANDLE_FUNCTION_ENTER ();
698 MonoReflectionFieldHandle result = mono_field_get_object_handle (domain, klass, field, &error);
699 mono_error_cleanup (&error);
700 HANDLE_FUNCTION_RETURN_OBJ (result);
703 static MonoReflectionFieldHandle
704 field_object_construct (MonoDomain *domain, MonoClass *klass, MonoClassField *field, gpointer user_data, MonoError *error)
708 MonoReflectionFieldHandle res = MONO_HANDLE_NEW (MonoReflectionField, mono_object_new_checked (domain, mono_class_get_mono_field_class (), error));
711 MONO_HANDLE_SETVAL (res, klass, MonoClass *, klass);
712 MONO_HANDLE_SETVAL (res, field, MonoClassField *, field);
713 MonoStringHandle name = mono_string_new_handle (domain, mono_field_get_name (field), error);
716 MONO_HANDLE_SET (res, name, name);
719 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, field->type, error);
723 MONO_HANDLE_SET (res, type, rt);
725 MONO_HANDLE_SETVAL (res, attrs, guint32, mono_field_get_flags (field));
728 return MONO_HANDLE_CAST (MonoReflectionField, NULL_HANDLE);
732 * mono_field_get_object_handle:
733 * @domain: an app domain
736 * @error: set on error
738 * Return an System.Reflection.MonoField object representing the field @field
739 * in class @klass. On error, returns NULL and sets @error.
741 MonoReflectionFieldHandle
742 mono_field_get_object_handle (MonoDomain *domain, MonoClass *klass, MonoClassField *field, MonoError *error)
745 return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionFieldHandle, field, klass, field_object_construct, NULL);
750 * mono_field_get_object_checked:
751 * @domain: an app domain
754 * @error: set on error
756 * Return an System.Reflection.MonoField object representing the field @field
757 * in class @klass. On error, returns NULL and sets @error.
760 mono_field_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoClassField *field, MonoError *error)
762 HANDLE_FUNCTION_ENTER ();
763 MonoReflectionFieldHandle result = mono_field_get_object_handle (domain, klass, field, error);
764 HANDLE_FUNCTION_RETURN_OBJ (result);
768 * mono_property_get_object:
769 * @domain: an app domain
771 * @property: a property
773 * Return an System.Reflection.MonoProperty object representing the property @property
776 MonoReflectionProperty*
777 mono_property_get_object (MonoDomain *domain, MonoClass *klass, MonoProperty *property)
779 HANDLE_FUNCTION_ENTER ();
781 MonoReflectionPropertyHandle result = mono_property_get_object_handle (domain, klass, property, &error);
782 mono_error_cleanup (&error);
783 HANDLE_FUNCTION_RETURN_OBJ (result);
786 static MonoReflectionPropertyHandle
787 property_object_construct (MonoDomain *domain, MonoClass *klass, MonoProperty *property, gpointer user_data, MonoError *error)
791 MonoReflectionPropertyHandle res = MONO_HANDLE_NEW (MonoReflectionProperty, mono_object_new_checked (domain, mono_class_get_mono_property_class (), error));
794 MONO_HANDLE_SETVAL (res, klass, MonoClass *, klass);
795 MONO_HANDLE_SETVAL (res, property, MonoProperty *, property);
798 return MONO_HANDLE_CAST (MonoReflectionProperty, NULL_HANDLE);
802 * mono_property_get_object_handle:
803 * \param domain an app domain
804 * \param klass a type
805 * \param property a property
806 * \param error set on error
808 * \returns A \c System.Reflection.MonoProperty object representing the property \p property
809 * in class \p klass. On error returns NULL and sets \p error.
811 MonoReflectionPropertyHandle
812 mono_property_get_object_handle (MonoDomain *domain, MonoClass *klass, MonoProperty *property, MonoError *error)
814 return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionPropertyHandle, property, klass, property_object_construct, NULL);
818 * mono_property_get_object:
819 * \param domain an app domain
820 * \param klass a type
821 * \param property a property
822 * \param error set on error
823 * \returns a \c System.Reflection.MonoProperty object representing the property \p property
824 * in class \p klass. On error returns NULL and sets \p error.
826 MonoReflectionProperty*
827 mono_property_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoProperty *property, MonoError *error)
829 HANDLE_FUNCTION_ENTER ();
830 MonoReflectionPropertyHandle res = mono_property_get_object_handle (domain, klass, property, error);
831 HANDLE_FUNCTION_RETURN_OBJ (res);
835 * mono_event_get_object:
836 * \param domain an app domain
837 * \param klass a type
838 * \param event a event
839 * \returns A \c System.Reflection.MonoEvent object representing the event \p event
843 mono_event_get_object (MonoDomain *domain, MonoClass *klass, MonoEvent *event)
845 HANDLE_FUNCTION_ENTER ();
847 MonoReflectionEventHandle result = mono_event_get_object_handle (domain, klass, event, &error);
848 mono_error_cleanup (&error);
849 HANDLE_FUNCTION_RETURN_OBJ (result);
852 static MonoReflectionEventHandle
853 event_object_construct (MonoDomain *domain, MonoClass *klass, MonoEvent *event, gpointer user_data, MonoError *error)
857 MonoReflectionMonoEventHandle mono_event = MONO_HANDLE_NEW (MonoReflectionMonoEvent, mono_object_new_checked (domain, mono_class_get_mono_event_class (), error));
859 return MONO_HANDLE_CAST (MonoReflectionEvent, NULL_HANDLE);
860 MONO_HANDLE_SETVAL (mono_event, klass, MonoClass* , klass);
861 MONO_HANDLE_SETVAL (mono_event, event, MonoEvent* , event);
862 return MONO_HANDLE_CAST (MonoReflectionEvent, mono_event);
866 * mono_event_get_object_handle:
867 * \param domain an app domain
868 * \param klass a type
869 * \param event a event
870 * \param error set on error
871 * \returns a \c System.Reflection.MonoEvent object representing the event \p event
872 * in class \p klass. On failure sets \p error and returns NULL
874 MonoReflectionEventHandle
875 mono_event_get_object_handle (MonoDomain *domain, MonoClass *klass, MonoEvent *event, MonoError *error)
878 return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionEventHandle, event, klass, event_object_construct, NULL);
883 * mono_get_reflection_missing_object:
884 * \param domain Domain where the object lives
886 * \returns the \c System.Reflection.Missing.Value singleton object
887 * (of type \c System.Reflection.Missing).
889 * Used as the value for \c ParameterInfo.DefaultValue when Optional
892 static MonoObjectHandle
893 mono_get_reflection_missing_object (MonoDomain *domain)
896 static MonoClassField *missing_value_field = NULL;
898 if (!missing_value_field) {
899 MonoClass *missing_klass;
900 missing_klass = mono_class_get_missing_class ();
901 mono_class_init (missing_klass);
902 missing_value_field = mono_class_get_field_from_name (missing_klass, "Value");
903 g_assert (missing_value_field);
905 /* FIXME change mono_field_get_value_object_checked to return a handle */
906 MonoObjectHandle obj = MONO_HANDLE_NEW (MonoObject, mono_field_get_value_object_checked (domain, missing_value_field, NULL, &error));
907 mono_error_assert_ok (&error);
911 static MonoObjectHandle
912 get_dbnull_object (MonoDomain *domain, MonoError *error)
914 static MonoClassField *dbnull_value_field = NULL;
918 if (!dbnull_value_field) {
919 MonoClass *dbnull_klass;
920 dbnull_klass = mono_class_get_dbnull_class ();
921 dbnull_value_field = mono_class_get_field_from_name (dbnull_klass, "Value");
922 g_assert (dbnull_value_field);
924 /* FIXME change mono_field_get_value_object_checked to return a handle */
925 MonoObjectHandle obj = MONO_HANDLE_NEW (MonoObject, mono_field_get_value_object_checked (domain, dbnull_value_field, NULL, error));
929 static MonoObjectHandle
930 get_dbnull (MonoDomain *domain, MonoObjectHandle dbnull, MonoError *error)
933 if (MONO_HANDLE_IS_NULL (dbnull))
934 MONO_HANDLE_ASSIGN (dbnull, get_dbnull_object (domain, error));
938 static MonoObjectHandle
939 get_reflection_missing (MonoDomain *domain, MonoObjectHandleOut reflection_missing)
941 if (MONO_HANDLE_IS_NULL (reflection_missing))
942 MONO_HANDLE_ASSIGN (reflection_missing, mono_get_reflection_missing_object (domain));
943 return reflection_missing;
947 add_parameter_object_to_array (MonoDomain *domain, MonoMethod *method, MonoObjectHandle member, int idx, const char *name, MonoType *sig_param, guint32 blob_type_enum, const char *blob, MonoMarshalSpec *mspec, MonoObjectHandle missing, MonoObjectHandle dbnull, MonoArrayHandle dest, MonoError *error)
949 HANDLE_FUNCTION_ENTER ();
951 MonoReflectionParameterHandle param = MONO_HANDLE_NEW (MonoReflectionParameter, mono_object_new_checked (domain, mono_class_get_mono_parameter_info_class (), error));
955 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, sig_param, error);
959 MONO_HANDLE_SET (param, ClassImpl, rt);
961 MONO_HANDLE_SET (param, MemberImpl, member);
963 MonoStringHandle name_str = mono_string_new_handle (domain, name, error);
967 MONO_HANDLE_SET (param, NameImpl, name_str);
969 MONO_HANDLE_SETVAL (param, PositionImpl, gint32, idx);
971 MONO_HANDLE_SETVAL (param, AttrsImpl, guint32, sig_param->attrs);
973 if (!(sig_param->attrs & PARAM_ATTRIBUTE_HAS_DEFAULT)) {
974 if (sig_param->attrs & PARAM_ATTRIBUTE_OPTIONAL)
975 MONO_HANDLE_SET (param, DefaultValueImpl, get_reflection_missing (domain, missing));
977 MONO_HANDLE_SET (param, DefaultValueImpl, get_dbnull (domain, dbnull, error));
984 blob_type.type = (MonoTypeEnum)blob_type_enum;
985 blob_type.data.klass = NULL;
986 if (blob_type_enum == MONO_TYPE_CLASS)
987 blob_type.data.klass = mono_defaults.object_class;
988 else if ((sig_param->type == MONO_TYPE_VALUETYPE) && sig_param->data.klass->enumtype) {
989 /* For enums, types [i] contains the base type */
991 blob_type.type = MONO_TYPE_VALUETYPE;
992 blob_type.data.klass = mono_class_from_mono_type (sig_param);
994 blob_type.data.klass = mono_class_from_mono_type (&blob_type);
996 MonoObjectHandle default_val_obj = MONO_HANDLE_NEW (MonoObject, mono_get_object_from_blob (domain, &blob_type, blob, error)); /* FIXME make mono_get_object_from_blob return a handle */
999 MONO_HANDLE_SET (param, DefaultValueImpl, default_val_obj);
1001 /* Type in the Constant table is MONO_TYPE_CLASS for nulls */
1002 if (blob_type_enum != MONO_TYPE_CLASS && MONO_HANDLE_IS_NULL(default_val_obj)) {
1003 if (sig_param->attrs & PARAM_ATTRIBUTE_OPTIONAL)
1004 MONO_HANDLE_SET (param, DefaultValueImpl, get_reflection_missing (domain, missing));
1006 MONO_HANDLE_SET (param, DefaultValueImpl, get_dbnull (domain, dbnull, error));
1013 MonoReflectionMarshalAsAttributeHandle mobj = mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspec, error);
1016 MONO_HANDLE_SET (param, MarshalAsImpl, mobj);
1019 MONO_HANDLE_ARRAY_SETREF (dest, idx, param);
1022 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
1025 static MonoArrayHandle
1026 param_objects_construct (MonoDomain *domain, MonoClass *refclass, MonoMethodSignature **addr_of_sig, gpointer user_data, MonoError *error)
1028 MonoMethod *method = (MonoMethod*)user_data;
1029 MonoMethodSignature *sig = *addr_of_sig; /* see note in mono_param_get_objects_internal */
1031 MonoArrayHandle res = MONO_HANDLE_NEW (MonoArray, NULL);
1032 char **names = NULL, **blobs = NULL;
1033 guint32 *types = NULL;
1034 MonoMarshalSpec **mspecs = NULL;
1039 MonoReflectionMethodHandle member = mono_method_get_object_handle (domain, method, refclass, error);
1042 names = g_new (char *, sig->param_count);
1043 mono_method_get_param_names (method, (const char **) names);
1045 mspecs = g_new (MonoMarshalSpec*, sig->param_count + 1);
1046 mono_method_get_marshal_info (method, mspecs);
1048 res = mono_array_new_handle (domain, mono_class_get_mono_parameter_info_class (), sig->param_count, error);
1052 gboolean any_default_value = FALSE;
1053 for (i = 0; i < sig->param_count; ++i) {
1054 if ((sig->params [i]->attrs & PARAM_ATTRIBUTE_HAS_DEFAULT) != 0) {
1055 any_default_value = TRUE;
1059 if (any_default_value) {
1060 blobs = g_new0 (char *, sig->param_count);
1061 types = g_new0 (guint32, sig->param_count);
1062 get_default_param_value_blobs (method, blobs, types);
1065 /* Handles missing and dbnull are assigned in add_parameter_object_to_array when needed */
1066 MonoObjectHandle missing = MONO_HANDLE_NEW (MonoObject, NULL);
1067 MonoObjectHandle dbnull = MONO_HANDLE_NEW (MonoObject, NULL);
1068 for (i = 0; i < sig->param_count; ++i) {
1069 if (!add_parameter_object_to_array (domain, method, MONO_HANDLE_CAST(MonoObject, member), i, names[i], sig->params[i], types ? types[i] : 0, blobs ? blobs[i] : NULL, mspecs [i + 1], missing, dbnull, res, error))
1078 if (sig && mspecs) {
1079 for (i = sig->param_count; i >= 0; i--) {
1081 mono_metadata_free_marshal_spec (mspecs [i]);
1093 * mono_param_get_objects:
1094 * @domain: an app domain
1097 * Return an System.Reflection.ParameterInfo array object representing the parameters
1098 * in the method @method.
1101 mono_param_get_objects_internal (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
1105 /* side-effect: sets method->signature non-NULL on success */
1106 MonoMethodSignature *sig = mono_method_signature_checked (method, error);
1110 if (!sig->param_count) {
1111 MonoArrayHandle res = mono_array_new_handle (domain, mono_class_get_mono_parameter_info_class (), 0, error);
1118 /* Note: the cache is based on the address of the signature into the method
1119 * since we already cache MethodInfos with the method as keys.
1121 return CHECK_OR_CONSTRUCT_HANDLE (MonoArrayHandle, &method->signature, refclass, param_objects_construct, method);
1123 return MONO_HANDLE_NEW (MonoArray, NULL_HANDLE);
1127 * mono_param_get_objects:
1130 mono_param_get_objects (MonoDomain *domain, MonoMethod *method)
1132 HANDLE_FUNCTION_ENTER ();
1134 MonoArrayHandle result = mono_param_get_objects_internal (domain, method, NULL, &error);
1135 mono_error_assert_ok (&error);
1136 HANDLE_FUNCTION_RETURN_OBJ (result);
1140 add_local_var_info_to_array (MonoDomain *domain, MonoMethodHeader *header, int idx, MonoArrayHandle dest, MonoError *error)
1142 HANDLE_FUNCTION_ENTER ();
1144 MonoReflectionLocalVariableInfoHandle info = MONO_HANDLE_NEW (MonoReflectionLocalVariableInfo, mono_object_new_checked (domain, mono_class_get_local_variable_info_class (), error));
1148 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, header->locals [idx], error);
1152 MONO_HANDLE_SET (info, local_type, rt);
1154 MONO_HANDLE_SETVAL (info, is_pinned, MonoBoolean, header->locals [idx]->pinned);
1155 MONO_HANDLE_SETVAL (info, local_index, guint16, idx);
1157 MONO_HANDLE_ARRAY_SETREF (dest, idx, info);
1160 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
1164 add_exception_handling_clause_to_array (MonoDomain *domain, MonoMethodHeader *header, int idx, MonoArrayHandle dest, MonoError *error)
1166 HANDLE_FUNCTION_ENTER ();
1168 MonoReflectionExceptionHandlingClauseHandle info = MONO_HANDLE_NEW (MonoReflectionExceptionHandlingClause, mono_object_new_checked (domain, mono_class_get_exception_handling_clause_class (), error));
1171 MonoExceptionClause *clause = &header->clauses [idx];
1173 MONO_HANDLE_SETVAL (info, flags, gint32, clause->flags);
1174 MONO_HANDLE_SETVAL (info, try_offset, gint32, clause->try_offset);
1175 MONO_HANDLE_SETVAL (info, try_length, gint32, clause->try_len);
1176 MONO_HANDLE_SETVAL (info, handler_offset, gint32, clause->handler_offset);
1177 MONO_HANDLE_SETVAL (info, handler_length, gint32, clause->handler_len);
1178 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1179 MONO_HANDLE_SETVAL (info, filter_offset, gint32, clause->data.filter_offset);
1180 else if (clause->data.catch_class) {
1181 MonoReflectionTypeHandle rt = mono_type_get_object_handle (mono_domain_get (), &clause->data.catch_class->byval_arg, error);
1185 MONO_HANDLE_SET (info, catch_type, rt);
1188 MONO_HANDLE_ARRAY_SETREF (dest, idx, info);
1190 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
1194 * mono_method_body_get_object:
1195 * \param domain an app domain
1196 * \param method a method
1197 * \return A \c System.Reflection.MethodBody object representing the method \p method.
1199 MonoReflectionMethodBody*
1200 mono_method_body_get_object (MonoDomain *domain, MonoMethod *method)
1202 HANDLE_FUNCTION_ENTER ();
1204 MonoReflectionMethodBodyHandle result = mono_method_body_get_object_handle (domain, method, &error);
1205 mono_error_cleanup (&error);
1206 HANDLE_FUNCTION_RETURN_OBJ (result);
1209 static MonoReflectionMethodBodyHandle
1210 method_body_object_construct (MonoDomain *domain, MonoClass *unused_class, MonoMethod *method, gpointer user_data, MonoError *error)
1212 MonoMethodHeader *header = NULL;
1214 guint32 method_rva, local_var_sig_token;
1216 unsigned char format, flags;
1221 /* for compatibility with .net */
1222 if (method_is_dynamic (method)) {
1223 mono_error_set_generic_error (error, "System", "InvalidOperationException", "");
1227 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1228 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
1229 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1230 (method->klass->image->raw_data && method->klass->image->raw_data [1] != 'Z') ||
1231 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME))
1232 return MONO_HANDLE_CAST (MonoReflectionMethodBody, NULL_HANDLE);
1234 image = method->klass->image;
1235 header = mono_method_get_header_checked (method, error);
1239 if (!image_is_dynamic (image)) {
1240 /* Obtain local vars signature token */
1241 method_rva = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD], mono_metadata_token_index (method->token) - 1, MONO_METHOD_RVA);
1242 ptr = mono_image_rva_map (image, method_rva);
1243 flags = *(const unsigned char *) ptr;
1244 format = flags & METHOD_HEADER_FORMAT_MASK;
1246 case METHOD_HEADER_TINY_FORMAT:
1247 local_var_sig_token = 0;
1249 case METHOD_HEADER_FAT_FORMAT:
1253 local_var_sig_token = read32 (ptr);
1256 g_assert_not_reached ();
1259 local_var_sig_token = 0; //FIXME
1261 MonoReflectionMethodBodyHandle ret = MONO_HANDLE_NEW (MonoReflectionMethodBody, mono_object_new_checked (domain, mono_class_get_method_body_class (), error));
1265 MONO_HANDLE_SETVAL (ret, init_locals, MonoBoolean, header->init_locals);
1266 MONO_HANDLE_SETVAL (ret, max_stack, guint32, header->max_stack);
1267 MONO_HANDLE_SETVAL (ret, local_var_sig_token, guint32, local_var_sig_token);
1268 MonoArrayHandle il_arr = mono_array_new_handle (domain, mono_defaults.byte_class, header->code_size, error);
1271 MONO_HANDLE_SET (ret, il, il_arr);
1272 uint32_t il_gchandle;
1273 guint8* il_data = MONO_ARRAY_HANDLE_PIN (il_arr, guint8, 0, &il_gchandle);
1274 memcpy (il_data, header->code, header->code_size);
1275 mono_gchandle_free (il_gchandle);
1278 MonoArrayHandle locals_arr = mono_array_new_handle (domain, mono_class_get_local_variable_info_class (), header->num_locals, error);
1281 MONO_HANDLE_SET (ret, locals, locals_arr);
1282 for (i = 0; i < header->num_locals; ++i) {
1283 if (!add_local_var_info_to_array (domain, header, i, locals_arr, error))
1288 MonoArrayHandle exn_clauses = mono_array_new_handle (domain, mono_class_get_exception_handling_clause_class (), header->num_clauses, error);
1291 MONO_HANDLE_SET (ret, clauses, exn_clauses);
1292 for (i = 0; i < header->num_clauses; ++i) {
1293 if (!add_exception_handling_clause_to_array (domain, header, i, exn_clauses, error))
1297 mono_metadata_free_mh (header);
1301 mono_metadata_free_mh (header);
1306 * mono_method_body_get_object_handle:
1307 * \param domain an app domain
1308 * \param method a method
1309 * \param error set on error
1310 * \returns a \c System.Reflection.MethodBody object representing the
1311 * method \p method. On failure, returns NULL and sets \p error.
1313 MonoReflectionMethodBodyHandle
1314 mono_method_body_get_object_handle (MonoDomain *domain, MonoMethod *method, MonoError *error)
1317 return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionMethodBodyHandle, method, NULL, method_body_object_construct, NULL);
1322 * mono_get_dbnull_object:
1323 * \param domain Domain where the object lives
1324 * Used as the value for \c ParameterInfo.DefaultValue
1325 * \returns the \c System.DBNull.Value singleton object
1328 mono_get_dbnull_object (MonoDomain *domain)
1330 HANDLE_FUNCTION_ENTER ();
1332 MonoObjectHandle obj = get_dbnull_object (domain, &error);
1333 mono_error_assert_ok (&error);
1334 HANDLE_FUNCTION_RETURN_OBJ (obj);
1338 get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types)
1340 guint32 param_index, i, lastp, crow = 0;
1341 guint32 param_cols [MONO_PARAM_SIZE], const_cols [MONO_CONSTANT_SIZE];
1344 MonoClass *klass = method->klass;
1345 MonoImage *image = klass->image;
1346 MonoMethodSignature *methodsig = mono_method_signature (method);
1348 MonoTableInfo *constt;
1349 MonoTableInfo *methodt;
1350 MonoTableInfo *paramt;
1352 if (!methodsig->param_count)
1355 mono_class_init (klass);
1357 if (image_is_dynamic (klass->image)) {
1358 MonoReflectionMethodAux *aux;
1359 if (method->is_inflated)
1360 method = ((MonoMethodInflated*)method)->declaring;
1361 aux = (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1362 if (aux && aux->param_defaults) {
1363 memcpy (blobs, &(aux->param_defaults [1]), methodsig->param_count * sizeof (char*));
1364 memcpy (types, &(aux->param_default_types [1]), methodsig->param_count * sizeof (guint32));
1369 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1370 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1371 constt = &image->tables [MONO_TABLE_CONSTANT];
1373 idx = mono_method_get_index (method) - 1;
1374 g_assert (idx != -1);
1376 param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1377 if (idx + 1 < methodt->rows)
1378 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
1380 lastp = paramt->rows + 1;
1382 for (i = param_index; i < lastp; ++i) {
1385 mono_metadata_decode_row (paramt, i - 1, param_cols, MONO_PARAM_SIZE);
1386 paramseq = param_cols [MONO_PARAM_SEQUENCE];
1388 if (!(param_cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_DEFAULT))
1391 crow = mono_metadata_get_constant_index (image, MONO_TOKEN_PARAM_DEF | i, crow + 1);
1396 mono_metadata_decode_row (constt, crow - 1, const_cols, MONO_CONSTANT_SIZE);
1397 blobs [paramseq - 1] = (char *)mono_metadata_blob_heap (image, const_cols [MONO_CONSTANT_VALUE]);
1398 types [paramseq - 1] = const_cols [MONO_CONSTANT_TYPE];
1405 mono_get_object_from_blob (MonoDomain *domain, MonoType *type, const char *blob, MonoError *error)
1410 MonoType *basetype = type;
1417 klass = mono_class_from_mono_type (type);
1418 if (klass->valuetype) {
1419 object = mono_object_new_checked (domain, klass, error);
1420 return_val_if_nok (error, NULL);
1421 retval = ((gchar *) object + sizeof (MonoObject));
1422 if (klass->enumtype)
1423 basetype = mono_class_enum_basetype (klass);
1428 if (!mono_get_constant_value_from_blob (domain, basetype->type, blob, retval, error))
1435 assembly_name_to_aname (MonoAssemblyName *assembly, char *p) {
1438 gboolean quoted = FALSE;
1440 memset (assembly, 0, sizeof (MonoAssemblyName));
1441 assembly->culture = "";
1442 memset (assembly->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1449 while (*p && (isalnum (*p) || *p == '.' || *p == '-' || *p == '_' || *p == '$' || *p == '@' || g_ascii_isspace (*p)))
1460 /* Remove trailing whitespace */
1462 while (*s && g_ascii_isspace (*s))
1465 while (g_ascii_isspace (*p))
1468 if (*p == 'V' && g_ascii_strncasecmp (p, "Version=", 8) == 0) {
1470 assembly->major = strtoul (p, &s, 10);
1471 if (s == p || *s != '.')
1474 assembly->minor = strtoul (p, &s, 10);
1475 if (s == p || *s != '.')
1478 assembly->build = strtoul (p, &s, 10);
1479 if (s == p || *s != '.')
1482 assembly->revision = strtoul (p, &s, 10);
1486 } else if (*p == 'C' && g_ascii_strncasecmp (p, "Culture=", 8) == 0) {
1488 if (g_ascii_strncasecmp (p, "neutral", 7) == 0) {
1489 assembly->culture = "";
1492 assembly->culture = p;
1493 while (*p && *p != ',') {
1497 } else if (*p == 'P' && g_ascii_strncasecmp (p, "PublicKeyToken=", 15) == 0) {
1499 if (strncmp (p, "null", 4) == 0) {
1504 while (*p && *p != ',') {
1507 len = (p - start + 1);
1508 if (len > MONO_PUBLIC_KEY_TOKEN_LENGTH)
1509 len = MONO_PUBLIC_KEY_TOKEN_LENGTH;
1510 g_strlcpy ((char*)assembly->public_key_token, start, len);
1513 while (*p && *p != ',')
1517 while (g_ascii_isspace (*p) || *p == ',') {
1531 * mono_reflection_parse_type:
1534 * Parse a type name as accepted by the GetType () method and output the info
1535 * extracted in the info structure.
1536 * the name param will be mangled, so, make a copy before passing it to this function.
1537 * The fields in info will be valid until the memory pointed to by name is valid.
1539 * See also mono_type_get_name () below.
1541 * Returns: 0 on parse error.
1544 _mono_reflection_parse_type (char *name, char **endptr, gboolean is_recursed,
1545 MonoTypeNameParse *info)
1547 char *start, *p, *w, *last_point, *startn;
1548 int in_modifiers = 0;
1549 int isbyref = 0, rank = 0, isptr = 0;
1551 start = p = w = name;
1553 //FIXME could we just zero the whole struct? memset (&info, 0, sizeof (MonoTypeNameParse))
1554 memset (&info->assembly, 0, sizeof (MonoAssemblyName));
1555 info->name = info->name_space = NULL;
1556 info->nested = NULL;
1557 info->modifiers = NULL;
1558 info->type_arguments = NULL;
1560 /* last_point separates the namespace from the name */
1563 while (*p == ' ') p++, start++, w++, name++;
1568 *p = 0; /* NULL terminate the name */
1570 info->nested = g_list_append (info->nested, startn);
1571 /* we have parsed the nesting namespace + name */
1575 info->name_space = start;
1577 info->name = last_point + 1;
1579 info->name_space = (char *)"";
1607 info->name_space = start;
1609 info->name = last_point + 1;
1611 info->name_space = (char *)"";
1618 if (isbyref) /* only one level allowed by the spec */
1622 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (0));
1626 if (isbyref) /* pointer to ref not okay */
1628 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-1));
1633 if (isbyref) /* array of ref and generic ref are not okay */
1635 //Decide if it's an array of a generic argument list
1640 if (*p == ',' || *p == '*' || *p == ']') { //array
1648 else if (*p == '*') /* '*' means unknown lower bound */
1649 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-2));
1656 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (rank));
1658 if (rank || isptr) /* generic args after array spec or ptr*/ //XXX test
1661 info->type_arguments = g_ptr_array_new ();
1663 MonoTypeNameParse *subinfo = g_new0 (MonoTypeNameParse, 1);
1664 gboolean fqname = FALSE;
1666 g_ptr_array_add (info->type_arguments, subinfo);
1668 while (*p == ' ') p++;
1674 if (!_mono_reflection_parse_type (p, &p, TRUE, subinfo))
1677 /*MS is lenient on [] delimited parameters that aren't fqn - and F# uses them.*/
1678 if (fqname && (*p != ']')) {
1686 while (*p && (*p != ']'))
1694 if (g_ascii_isspace (*aname)) {
1701 !assembly_name_to_aname (&subinfo->assembly, aname))
1703 } else if (fqname && (*p == ']')) {
1725 if (g_ascii_isspace (*p)) {
1732 return 0; /* missing assembly name */
1733 if (!assembly_name_to_aname (&info->assembly, p))
1739 if (info->assembly.name)
1742 // *w = 0; /* terminate class name */
1744 if (!info->name || !*info->name)
1748 /* add other consistency checks */
1754 * mono_identifier_unescape_type_name_chars:
1755 * \param identifier the display name of a mono type
1757 * \returns The name in internal form, that is without escaping backslashes.
1759 * The string is modified in place!
1762 mono_identifier_unescape_type_name_chars(char* identifier)
1767 for (w = r = identifier; *r != 0; r++)
1785 mono_identifier_unescape_info (MonoTypeNameParse* info);
1788 unescape_each_type_argument(void* data, void* user_data)
1790 MonoTypeNameParse* info = (MonoTypeNameParse*)data;
1791 mono_identifier_unescape_info (info);
1795 unescape_each_nested_name (void* data, void* user_data)
1797 char* nested_name = (char*) data;
1798 mono_identifier_unescape_type_name_chars(nested_name);
1802 * mono_identifier_unescape_info:
1804 * \param info a parsed display form of an (optionally assembly qualified) full type name.
1806 * Destructively updates the info by unescaping the identifiers that
1807 * comprise the type namespace, name, nested types (if any) and
1808 * generic type arguments (if any).
1810 * The resulting info has the names in internal form.
1814 mono_identifier_unescape_info (MonoTypeNameParse *info)
1818 mono_identifier_unescape_type_name_chars(info->name_space);
1819 mono_identifier_unescape_type_name_chars(info->name);
1820 // but don't escape info->assembly
1821 if (info->type_arguments)
1822 g_ptr_array_foreach(info->type_arguments, &unescape_each_type_argument, NULL);
1824 g_list_foreach(info->nested, &unescape_each_nested_name, NULL);
1828 mono_reflection_parse_type (char *name, MonoTypeNameParse *info)
1830 int ok = _mono_reflection_parse_type (name, NULL, FALSE, info);
1832 mono_identifier_unescape_info (info);
1838 _mono_reflection_get_type_from_info (MonoTypeNameParse *info, MonoImage *image, gboolean ignorecase, MonoError *error)
1840 gboolean type_resolve = FALSE;
1842 MonoImage *rootimage = image;
1846 if (info->assembly.name) {
1847 MonoAssembly *assembly = mono_assembly_loaded (&info->assembly);
1848 if (!assembly && image && image->assembly && mono_assembly_names_equal (&info->assembly, &image->assembly->aname))
1850 * This could happen in the AOT compiler case when the search hook is not
1853 assembly = image->assembly;
1855 /* then we must load the assembly ourselve - see #60439 */
1856 assembly = mono_assembly_load (&info->assembly, image->assembly->basedir, NULL);
1860 image = assembly->image;
1861 } else if (!image) {
1862 image = mono_defaults.corlib;
1865 type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1866 if (type == NULL && !info->assembly.name && image != mono_defaults.corlib) {
1867 /* ignore the error and try again */
1868 mono_error_cleanup (error);
1870 image = mono_defaults.corlib;
1871 type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1878 * mono_reflection_get_type_internal:
1880 * Returns: may return NULL on success, sets error on failure.
1883 mono_reflection_get_type_internal (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
1888 gboolean bounded = FALSE;
1892 image = mono_defaults.corlib;
1895 rootimage = mono_defaults.corlib;
1898 klass = mono_class_from_name_case_checked (image, info->name_space, info->name, error);
1900 klass = mono_class_from_name_checked (image, info->name_space, info->name, error);
1905 for (mod = info->nested; mod; mod = mod->next) {
1906 gpointer iter = NULL;
1910 mono_class_init (parent);
1912 while ((klass = mono_class_get_nested_types (parent, &iter))) {
1914 char *nested_name, *nested_nspace;
1915 gboolean match = TRUE;
1917 lastp = strrchr ((const char *)mod->data, '.');
1919 /* Nested classes can have namespaces */
1922 nested_name = g_strdup (lastp + 1);
1923 nspace_len = lastp - (char*)mod->data;
1924 nested_nspace = (char *)g_malloc (nspace_len + 1);
1925 memcpy (nested_nspace, mod->data, nspace_len);
1926 nested_nspace [nspace_len] = '\0';
1929 nested_name = (char *)mod->data;
1930 nested_nspace = NULL;
1933 if (nested_nspace) {
1935 if (!(klass->name_space && mono_utf8_strcasecmp (klass->name_space, nested_nspace) == 0))
1938 if (!(klass->name_space && strcmp (klass->name_space, nested_nspace) == 0))
1944 if (mono_utf8_strcasecmp (klass->name, nested_name) != 0)
1947 if (strcmp (klass->name, nested_name) != 0)
1952 g_free (nested_name);
1953 g_free (nested_nspace);
1965 if (info->type_arguments) {
1966 MonoType **type_args = g_new0 (MonoType *, info->type_arguments->len);
1967 MonoReflectionTypeHandle the_type;
1971 for (i = 0; i < info->type_arguments->len; i++) {
1972 MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
1974 type_args [i] = _mono_reflection_get_type_from_info (subinfo, rootimage, ignorecase, error);
1975 if (!type_args [i]) {
1981 the_type = mono_type_get_object_handle (mono_domain_get (), &klass->byval_arg, error);
1982 if (!is_ok (error) || MONO_HANDLE_IS_NULL (the_type))
1985 instance = mono_reflection_bind_generic_parameters (
1986 the_type, info->type_arguments->len, type_args, error);
1992 klass = mono_class_from_mono_type (instance);
1995 for (mod = info->modifiers; mod; mod = mod->next) {
1996 modval = GPOINTER_TO_UINT (mod->data);
1997 if (!modval) { /* byref: must be last modifier */
1998 return &klass->this_arg;
1999 } else if (modval == -1) {
2000 klass = mono_ptr_class_get (&klass->byval_arg);
2001 } else if (modval == -2) {
2003 } else { /* array rank */
2004 klass = mono_bounded_array_class_get (klass, modval, bounded);
2008 return &klass->byval_arg;
2012 * mono_reflection_get_type:
2013 * @image: a metadata context
2014 * @info: type description structure
2015 * @ignorecase: flag for case-insensitive string compares
2016 * @type_resolve: whenever type resolve was already tried
2018 * Build a MonoType from the type description in @info.
2023 mono_reflection_get_type (MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve) {
2025 MonoType *result = mono_reflection_get_type_with_rootimage (image, image, info, ignorecase, type_resolve, &error);
2026 mono_error_cleanup (&error);
2031 * mono_reflection_get_type_checked:
2032 * \param rootimage the image of the currently active managed caller
2033 * \param image a metadata context
2034 * \param info type description structure
2035 * \param ignorecase flag for case-insensitive string compares
2036 * \param type_resolve whenever type resolve was already tried
2037 * \param error set on error.
2038 * Build a \c MonoType from the type description in \p info. On failure returns NULL and sets \p error.
2041 mono_reflection_get_type_checked (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error) {
2043 return mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, type_resolve, error);
2048 module_builder_array_get_type (MonoArrayHandle module_builders, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
2050 HANDLE_FUNCTION_ENTER ();
2052 MonoType *type = NULL;
2053 MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW (MonoReflectionModuleBuilder, NULL);
2054 MONO_HANDLE_ARRAY_GETREF (mb, module_builders, i);
2055 MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (mb, dynamic_image);
2056 type = mono_reflection_get_type_internal (rootimage, &dynamic_image->image, info, ignorecase, error);
2057 HANDLE_FUNCTION_RETURN_VAL (type);
2061 module_array_get_type (MonoArrayHandle modules, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
2063 HANDLE_FUNCTION_ENTER ();
2065 MonoType *type = NULL;
2066 MonoReflectionModuleHandle mod = MONO_HANDLE_NEW (MonoReflectionModule, NULL);
2067 MONO_HANDLE_ARRAY_GETREF (mod, modules, i);
2068 MonoImage *image = MONO_HANDLE_GETVAL (mod, image);
2069 type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error);
2070 HANDLE_FUNCTION_RETURN_VAL (type);
2074 mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *assembly, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
2076 HANDLE_FUNCTION_ENTER ();
2077 MonoType *type = NULL;
2081 g_assert (assembly_is_dynamic (assembly));
2082 MonoReflectionAssemblyBuilderHandle abuilder = MONO_HANDLE_CAST (MonoReflectionAssemblyBuilder, mono_assembly_get_object_handle (((MonoDynamicAssembly*)assembly)->domain, assembly, error));
2086 /* Enumerate all modules */
2088 MonoArrayHandle modules = MONO_HANDLE_NEW (MonoArray, NULL);
2089 MONO_HANDLE_GET (modules, abuilder, modules);
2090 if (!MONO_HANDLE_IS_NULL (modules)) {
2091 int n = mono_array_handle_length (modules);
2092 for (i = 0; i < n; ++i) {
2093 type = module_builder_array_get_type (modules, i, rootimage, info, ignorecase, error);
2101 MonoArrayHandle loaded_modules = MONO_HANDLE_NEW (MonoArray, NULL);
2102 MONO_HANDLE_GET (loaded_modules, abuilder, loaded_modules);
2103 if (!type && !MONO_HANDLE_IS_NULL(loaded_modules)) {
2104 int n = mono_array_handle_length (loaded_modules);
2105 for (i = 0; i < n; ++i) {
2106 type = module_array_get_type (loaded_modules, i, rootimage, info, ignorecase, error);
2115 HANDLE_FUNCTION_RETURN_VAL (type);
2119 mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error)
2122 MonoReflectionAssembly *assembly;
2128 if (image && image_is_dynamic (image))
2129 type = mono_reflection_get_type_internal_dynamic (rootimage, image->assembly, info, ignorecase, error);
2131 type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error);
2133 return_val_if_nok (error, NULL);
2137 if (!mono_domain_has_type_resolve (mono_domain_get ()))
2144 *type_resolve = TRUE;
2147 /* Reconstruct the type name */
2148 fullName = g_string_new ("");
2149 if (info->name_space && (info->name_space [0] != '\0'))
2150 g_string_printf (fullName, "%s.%s", info->name_space, info->name);
2152 g_string_printf (fullName, "%s", info->name);
2153 for (mod = info->nested; mod; mod = mod->next)
2154 g_string_append_printf (fullName, "+%s", (char*)mod->data);
2156 assembly = mono_domain_try_type_resolve_checked ( mono_domain_get (), fullName->str, NULL, error);
2157 if (!is_ok (error)) {
2158 g_string_free (fullName, TRUE);
2163 if (assembly_is_dynamic (assembly->assembly))
2164 type = mono_reflection_get_type_internal_dynamic (rootimage, assembly->assembly,
2165 info, ignorecase, error);
2167 type = mono_reflection_get_type_internal (rootimage, assembly->assembly->image,
2168 info, ignorecase, error);
2170 g_string_free (fullName, TRUE);
2171 return_val_if_nok (error, NULL);
2176 mono_reflection_free_type_info (MonoTypeNameParse *info)
2178 g_list_free (info->modifiers);
2179 g_list_free (info->nested);
2181 if (info->type_arguments) {
2184 for (i = 0; i < info->type_arguments->len; i++) {
2185 MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
2187 mono_reflection_free_type_info (subinfo);
2188 /*We free the subinfo since it is allocated by _mono_reflection_parse_type*/
2192 g_ptr_array_free (info->type_arguments, TRUE);
2197 * mono_reflection_type_from_name:
2199 * @image: a metadata context (can be NULL).
2201 * Retrieves a MonoType from its @name. If the name is not fully qualified,
2202 * it defaults to get the type from @image or, if @image is NULL or loading
2203 * from it fails, uses corlib.
2207 mono_reflection_type_from_name (char *name, MonoImage *image)
2210 MonoType *result = mono_reflection_type_from_name_checked (name, image, &error);
2211 mono_error_cleanup (&error);
2216 * mono_reflection_type_from_name_checked:
2217 * \param name type name.
2218 * \param image a metadata context (can be NULL).
2219 * \param error set on errror.
2220 * Retrieves a MonoType from its \p name. If the name is not fully qualified,
2221 * it defaults to get the type from \p image or, if \p image is NULL or loading
2222 * from it fails, uses corlib. On failure returns NULL and sets \p error.
2225 mono_reflection_type_from_name_checked (char *name, MonoImage *image, MonoError *error)
2227 MonoType *type = NULL;
2228 MonoTypeNameParse info;
2232 /* Make a copy since parse_type modifies its argument */
2233 tmp = g_strdup (name);
2235 /*g_print ("requested type %s\n", str);*/
2236 if (mono_reflection_parse_type (tmp, &info)) {
2237 type = _mono_reflection_get_type_from_info (&info, image, FALSE, error);
2238 if (!is_ok (error)) {
2240 mono_reflection_free_type_info (&info);
2246 mono_reflection_free_type_info (&info);
2251 * mono_reflection_get_token:
2253 * Return the metadata token of OBJ which should be an object
2254 * representing a metadata element.
2257 mono_reflection_get_token (MonoObject *obj_raw)
2259 HANDLE_FUNCTION_ENTER ();
2260 MONO_HANDLE_DCL (MonoObject, obj);
2262 guint32 result = mono_reflection_get_token_checked (obj, &error);
2263 mono_error_assert_ok (&error);
2264 HANDLE_FUNCTION_RETURN_VAL (result);
2268 * mono_reflection_get_token_checked:
2269 * \param obj the object
2270 * \param error set on error
2271 * \returns the metadata token of \p obj which should be an object
2272 * representing a metadata element. On failure sets \p error.
2275 mono_reflection_get_token_checked (MonoObjectHandle obj, MonoError *error)
2281 MonoClass *klass = mono_handle_class (obj);
2283 if (strcmp (klass->name, "MethodBuilder") == 0) {
2284 MonoReflectionMethodBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionMethodBuilder, obj);
2286 token = MONO_HANDLE_GETVAL (mb, table_idx) | MONO_TOKEN_METHOD_DEF;
2287 } else if (strcmp (klass->name, "ConstructorBuilder") == 0) {
2288 MonoReflectionCtorBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionCtorBuilder, obj);
2290 token = MONO_HANDLE_GETVAL (mb, table_idx) | MONO_TOKEN_METHOD_DEF;
2291 } else if (strcmp (klass->name, "FieldBuilder") == 0) {
2292 MonoReflectionFieldBuilderHandle fb = MONO_HANDLE_CAST (MonoReflectionFieldBuilder, obj);
2294 token = MONO_HANDLE_GETVAL (fb, table_idx) | MONO_TOKEN_FIELD_DEF;
2295 } else if (strcmp (klass->name, "TypeBuilder") == 0) {
2296 MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_CAST (MonoReflectionTypeBuilder, obj);
2297 token = MONO_HANDLE_GETVAL (tb, table_idx) | MONO_TOKEN_TYPE_DEF;
2298 } else if (strcmp (klass->name, "RuntimeType") == 0) {
2299 MonoType *type = mono_reflection_type_get_handle (MONO_HANDLE_RAW (MONO_HANDLE_CAST (MonoReflectionType, obj)), error); /* FIXME use handles */
2300 return_val_if_nok (error, 0);
2301 MonoClass *mc = mono_class_from_mono_type (type);
2302 if (!mono_class_init (mc)) {
2303 mono_error_set_for_class_failure (error, mc);
2307 token = mc->type_token;
2308 } else if (strcmp (klass->name, "MonoCMethod") == 0 ||
2309 strcmp (klass->name, "MonoMethod") == 0) {
2310 MonoReflectionMethodHandle m = MONO_HANDLE_CAST (MonoReflectionMethod, obj);
2311 MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
2312 if (method->is_inflated) {
2313 MonoMethodInflated *inflated = (MonoMethodInflated *) method;
2314 return inflated->declaring->token;
2316 token = method->token;
2318 } else if (strcmp (klass->name, "MonoField") == 0) {
2319 MonoReflectionFieldHandle f = MONO_HANDLE_CAST (MonoReflectionField, obj);
2321 token = mono_class_get_field_token (MONO_HANDLE_GETVAL (f, field));
2322 } else if (strcmp (klass->name, "MonoProperty") == 0) {
2323 MonoReflectionPropertyHandle p = MONO_HANDLE_CAST (MonoReflectionProperty, obj);
2325 token = mono_class_get_property_token (MONO_HANDLE_GETVAL (p, property));
2326 } else if (strcmp (klass->name, "MonoEvent") == 0) {
2327 MonoReflectionMonoEventHandle p = MONO_HANDLE_CAST (MonoReflectionMonoEvent, obj);
2329 token = mono_class_get_event_token (MONO_HANDLE_GETVAL (p, event));
2330 } else if (strcmp (klass->name, "ParameterInfo") == 0 || strcmp (klass->name, "MonoParameterInfo") == 0) {
2331 MonoReflectionParameterHandle p = MONO_HANDLE_CAST (MonoReflectionParameter, obj);
2332 MonoObjectHandle member_impl = MONO_HANDLE_NEW (MonoObject, NULL);
2333 MONO_HANDLE_GET (member_impl, p, MemberImpl);
2334 MonoClass *member_class = mono_handle_class (member_impl);
2335 g_assert (mono_class_is_reflection_method_or_constructor (member_class));
2336 MonoMethod *method = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionMethod, member_impl), method);
2338 token = mono_method_get_param_token (method, MONO_HANDLE_GETVAL (p, PositionImpl));
2339 } else if (strcmp (klass->name, "Module") == 0 || strcmp (klass->name, "MonoModule") == 0) {
2340 MonoReflectionModuleHandle m = MONO_HANDLE_CAST (MonoReflectionModule, obj);
2342 token = MONO_HANDLE_GETVAL (m, token);
2343 } else if (strcmp (klass->name, "Assembly") == 0 || strcmp (klass->name, "MonoAssembly") == 0) {
2344 token = mono_metadata_make_token (MONO_TABLE_ASSEMBLY, 1);
2346 mono_error_set_not_implemented (error, "MetadataToken is not supported for type '%s.%s'",
2347 klass->name_space, klass->name);
2356 mono_reflection_is_usertype (MonoReflectionTypeHandle ref)
2358 MonoClass *klass = mono_handle_class (ref);
2359 return klass->image != mono_defaults.corlib || strcmp ("TypeDelegator", klass->name) == 0;
2363 * mono_reflection_bind_generic_parameters:
2364 * \param type a managed type object (which should be some kind of generic (instance? definition?))
2365 * \param type_args the number of type arguments to bind
2366 * \param types array of type arguments
2367 * \param error set on error
2368 * Given a managed type object for a generic type instance, binds each of its arguments to the specified types.
2369 * \returns the \c MonoType* for the resulting type instantiation. On failure returns NULL and sets \p error.
2372 mono_reflection_bind_generic_parameters (MonoReflectionTypeHandle reftype, int type_argc, MonoType **types, MonoError *error)
2374 gboolean is_dynamic = FALSE;
2379 mono_loader_lock ();
2381 MonoClass *klass = mono_handle_class (reftype);
2382 if (mono_is_sre_type_builder (klass)) {
2384 } else if (mono_is_sre_generic_instance (klass)) {
2385 /* Does this ever make sense? what does instantiating a generic instance even mean? */
2386 g_assert_not_reached ();
2387 MonoReflectionGenericClassHandle rgi = MONO_HANDLE_CAST (MonoReflectionGenericClass, reftype);
2388 MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, generic_type);
2390 if (mono_is_sre_type_builder (mono_handle_class (gtd)))
2394 MonoType *t = mono_reflection_type_handle_mono_type (reftype, error);
2395 if (!is_ok (error)) {
2396 mono_loader_unlock ();
2400 klass = mono_class_from_mono_type (t);
2401 if (!mono_class_is_gtd (klass)) {
2402 mono_loader_unlock ();
2403 mono_error_set_type_load_class (error, klass, "Cannot bind generic parameters of a non-generic type");
2407 guint gtd_type_argc = mono_class_get_generic_container (klass)->type_argc;
2408 if (gtd_type_argc != type_argc) {
2409 mono_loader_unlock ();
2410 mono_error_set_argument (error, "types", "The generic type definition needs %d type arguments, but was instantiated with %d ", gtd_type_argc, type_argc);
2415 if (klass->wastypebuilder)
2418 mono_loader_unlock ();
2420 geninst = mono_class_bind_generic_parameters (klass, type_argc, types, is_dynamic);
2422 return &geninst->byval_arg;
2426 mono_class_bind_generic_parameters (MonoClass *klass, int type_argc, MonoType **types, gboolean is_dynamic)
2428 MonoGenericClass *gclass;
2429 MonoGenericInst *inst;
2431 g_assert (mono_class_is_gtd (klass));
2433 inst = mono_metadata_get_generic_inst (type_argc, types);
2434 gclass = mono_metadata_lookup_generic_class (klass, inst, is_dynamic);
2436 return mono_generic_class_get_class (gclass);
2439 static MonoGenericInst*
2440 generic_inst_from_type_array_handle (MonoArrayHandle types, MonoError *error)
2442 HANDLE_FUNCTION_ENTER ();
2444 MonoGenericInst *ginst = NULL;
2445 int count = mono_array_handle_length (types);
2446 MonoType **type_argv = g_new0 (MonoType *, count);
2447 MonoReflectionTypeHandle garg = MONO_HANDLE_NEW (MonoReflectionType, NULL);
2448 for (int i = 0; i < count; i++) {
2449 MONO_HANDLE_ARRAY_GETREF (garg, types, i);
2450 type_argv [i] = mono_reflection_type_handle_mono_type (garg, error);
2455 ginst = mono_metadata_get_generic_inst (count, type_argv);
2458 HANDLE_FUNCTION_RETURN_VAL (ginst);
2462 reflection_bind_generic_method_parameters (MonoMethod *method, MonoArrayHandle types, MonoError *error)
2465 MonoMethod *inflated;
2466 MonoGenericContext tmp_context;
2470 klass = method->klass;
2472 if (method->is_inflated)
2473 method = ((MonoMethodInflated *) method)->declaring;
2475 int count = mono_method_signature (method)->generic_param_count;
2476 if (count != mono_array_handle_length (types)) {
2477 mono_error_set_argument (error, "typeArguments", "Incorrect number of generic arguments");
2481 MonoGenericInst *ginst = generic_inst_from_type_array_handle (types, error);
2482 return_val_if_nok (error, NULL);
2484 tmp_context.class_inst = mono_class_is_ginst (klass) ? mono_class_get_generic_class (klass)->context.class_inst : NULL;
2485 tmp_context.method_inst = ginst;
2487 inflated = mono_class_inflate_generic_method_checked (method, &tmp_context, error);
2488 mono_error_assert_ok (error);
2490 if (!mono_verifier_is_method_valid_generic_instantiation (inflated)) {
2491 mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
2498 MonoReflectionMethodHandle
2499 ves_icall_MonoMethod_MakeGenericMethod_impl (MonoReflectionMethodHandle rmethod, MonoArrayHandle types, MonoError *error)
2502 g_assert (0 != strcmp (mono_handle_class (rmethod)->name, "MethodBuilder"));
2504 MonoMethod *method = MONO_HANDLE_GETVAL (rmethod, method);
2505 MonoMethod *imethod = reflection_bind_generic_method_parameters (method, types, error);
2506 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
2508 /*FIXME but I think this is no longer necessary*/
2509 if (image_is_dynamic (method->klass->image)) {
2510 MonoDynamicImage *image = (MonoDynamicImage*)method->klass->image;
2512 * This table maps metadata structures representing inflated methods/fields
2513 * to the reflection objects representing their generic definitions.
2515 mono_image_lock ((MonoImage*)image);
2516 mono_g_hash_table_insert (image->generic_def_objects, imethod, MONO_HANDLE_RAW (rmethod));
2517 mono_image_unlock ((MonoImage*)image);
2520 return mono_method_get_object_handle (MONO_HANDLE_DOMAIN (rmethod), imethod, NULL, error);
2524 /* SECURITY_ACTION_* are defined in mono/metadata/tabledefs.h */
2525 const static guint32 declsec_flags_map[] = {
2526 0x00000000, /* empty */
2527 MONO_DECLSEC_FLAG_REQUEST, /* SECURITY_ACTION_REQUEST (x01) */
2528 MONO_DECLSEC_FLAG_DEMAND, /* SECURITY_ACTION_DEMAND (x02) */
2529 MONO_DECLSEC_FLAG_ASSERT, /* SECURITY_ACTION_ASSERT (x03) */
2530 MONO_DECLSEC_FLAG_DENY, /* SECURITY_ACTION_DENY (x04) */
2531 MONO_DECLSEC_FLAG_PERMITONLY, /* SECURITY_ACTION_PERMITONLY (x05) */
2532 MONO_DECLSEC_FLAG_LINKDEMAND, /* SECURITY_ACTION_LINKDEMAND (x06) */
2533 MONO_DECLSEC_FLAG_INHERITANCEDEMAND, /* SECURITY_ACTION_INHERITANCEDEMAND (x07) */
2534 MONO_DECLSEC_FLAG_REQUEST_MINIMUM, /* SECURITY_ACTION_REQUEST_MINIMUM (x08) */
2535 MONO_DECLSEC_FLAG_REQUEST_OPTIONAL, /* SECURITY_ACTION_REQUEST_OPTIONAL (x09) */
2536 MONO_DECLSEC_FLAG_REQUEST_REFUSE, /* SECURITY_ACTION_REQUEST_REFUSE (x0A) */
2537 MONO_DECLSEC_FLAG_PREJIT_GRANT, /* SECURITY_ACTION_PREJIT_GRANT (x0B) */
2538 MONO_DECLSEC_FLAG_PREJIT_DENY, /* SECURITY_ACTION_PREJIT_DENY (x0C) */
2539 MONO_DECLSEC_FLAG_NONCAS_DEMAND, /* SECURITY_ACTION_NONCAS_DEMAND (x0D) */
2540 MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND, /* SECURITY_ACTION_NONCAS_LINKDEMAND (x0E) */
2541 MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND, /* SECURITY_ACTION_NONCAS_INHERITANCEDEMAND (x0F) */
2542 MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE, /* SECURITY_ACTION_LINKDEMAND_CHOICE (x10) */
2543 MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE, /* SECURITY_ACTION_INHERITANCEDEMAND_CHOICE (x11) */
2544 MONO_DECLSEC_FLAG_DEMAND_CHOICE, /* SECURITY_ACTION_DEMAND_CHOICE (x12) */
2548 * Returns flags that includes all available security action associated to the handle.
2549 * @token: metadata token (either for a class or a method)
2550 * @image: image where resides the metadata.
2553 mono_declsec_get_flags (MonoImage *image, guint32 token)
2555 int index = mono_metadata_declsec_from_index (image, token);
2556 MonoTableInfo *t = &image->tables [MONO_TABLE_DECLSECURITY];
2561 /* HasSecurity can be present for other, not specially encoded, attributes,
2562 e.g. SuppressUnmanagedCodeSecurityAttribute */
2566 for (i = index; i < t->rows; i++) {
2567 guint32 cols [MONO_DECL_SECURITY_SIZE];
2569 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2570 if (cols [MONO_DECL_SECURITY_PARENT] != token)
2573 action = cols [MONO_DECL_SECURITY_ACTION];
2574 if ((action >= MONO_DECLSEC_ACTION_MIN) && (action <= MONO_DECLSEC_ACTION_MAX)) {
2575 result |= declsec_flags_map [action];
2577 g_assert_not_reached ();
2584 * Get the security actions (in the form of flags) associated with the specified method.
2586 * @method: The method for which we want the declarative security flags.
2587 * Return the declarative security flags for the method (only).
2589 * Note: To keep MonoMethod size down we do not cache the declarative security flags
2590 * (except for the stack modifiers which are kept in the MonoJitInfo structure)
2593 mono_declsec_flags_from_method (MonoMethod *method)
2595 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2596 /* FIXME: No cache (for the moment) */
2597 guint32 idx = mono_method_get_index (method);
2598 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2599 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2600 return mono_declsec_get_flags (method->klass->image, idx);
2606 * Get the security actions (in the form of flags) associated with the specified class.
2608 * @klass: The class for which we want the declarative security flags.
2609 * Return the declarative security flags for the class.
2611 * Note: We cache the flags inside the MonoClass structure as this will get
2612 * called very often (at least for each method).
2615 mono_declsec_flags_from_class (MonoClass *klass)
2617 if (mono_class_get_flags (klass) & TYPE_ATTRIBUTE_HAS_SECURITY) {
2618 guint32 flags = mono_class_get_declsec_flags (klass);
2623 idx = mono_metadata_token_index (klass->type_token);
2624 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2625 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2626 flags = mono_declsec_get_flags (klass->image, idx);
2627 /* we cache the flags on classes */
2628 mono_class_set_declsec_flags (klass, flags);
2636 * Get the security actions (in the form of flags) associated with the specified assembly.
2638 * @assembly: The assembly for which we want the declarative security flags.
2639 * Return the declarative security flags for the assembly.
2642 mono_declsec_flags_from_assembly (MonoAssembly *assembly)
2644 guint32 idx = 1; /* there is only one assembly */
2645 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2646 idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2647 return mono_declsec_get_flags (assembly->image, idx);
2652 * Fill actions for the specific index (which may either be an encoded class token or
2653 * an encoded method token) from the metadata image.
2654 * Returns TRUE if some actions requiring code generation are present, FALSE otherwise.
2657 fill_actions_from_index (MonoImage *image, guint32 token, MonoDeclSecurityActions* actions,
2658 guint32 id_std, guint32 id_noncas, guint32 id_choice)
2660 MonoBoolean result = FALSE;
2662 guint32 cols [MONO_DECL_SECURITY_SIZE];
2663 int index = mono_metadata_declsec_from_index (image, token);
2666 t = &image->tables [MONO_TABLE_DECLSECURITY];
2667 for (i = index; i < t->rows; i++) {
2668 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2670 if (cols [MONO_DECL_SECURITY_PARENT] != token)
2673 /* if present only replace (class) permissions with method permissions */
2674 /* if empty accept either class or method permissions */
2675 if (cols [MONO_DECL_SECURITY_ACTION] == id_std) {
2676 if (!actions->demand.blob) {
2677 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2678 actions->demand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2679 actions->demand.blob = (char*) (blob + 2);
2680 actions->demand.size = mono_metadata_decode_blob_size (blob, &blob);
2683 } else if (cols [MONO_DECL_SECURITY_ACTION] == id_noncas) {
2684 if (!actions->noncasdemand.blob) {
2685 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2686 actions->noncasdemand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2687 actions->noncasdemand.blob = (char*) (blob + 2);
2688 actions->noncasdemand.size = mono_metadata_decode_blob_size (blob, &blob);
2691 } else if (cols [MONO_DECL_SECURITY_ACTION] == id_choice) {
2692 if (!actions->demandchoice.blob) {
2693 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2694 actions->demandchoice.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2695 actions->demandchoice.blob = (char*) (blob + 2);
2696 actions->demandchoice.size = mono_metadata_decode_blob_size (blob, &blob);
2706 mono_declsec_get_class_demands_params (MonoClass *klass, MonoDeclSecurityActions* demands,
2707 guint32 id_std, guint32 id_noncas, guint32 id_choice)
2709 guint32 idx = mono_metadata_token_index (klass->type_token);
2710 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2711 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2712 return fill_actions_from_index (klass->image, idx, demands, id_std, id_noncas, id_choice);
2716 mono_declsec_get_method_demands_params (MonoMethod *method, MonoDeclSecurityActions* demands,
2717 guint32 id_std, guint32 id_noncas, guint32 id_choice)
2719 guint32 idx = mono_method_get_index (method);
2720 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2721 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2722 return fill_actions_from_index (method->klass->image, idx, demands, id_std, id_noncas, id_choice);
2726 * Collect all actions (that requires to generate code in mini) assigned for
2727 * the specified method.
2728 * Note: Don't use the content of actions if the function return FALSE.
2731 mono_declsec_get_demands (MonoMethod *method, MonoDeclSecurityActions* demands)
2733 guint32 mask = MONO_DECLSEC_FLAG_DEMAND | MONO_DECLSEC_FLAG_NONCAS_DEMAND |
2734 MONO_DECLSEC_FLAG_DEMAND_CHOICE;
2735 MonoBoolean result = FALSE;
2738 /* quick exit if no declarative security is present in the metadata */
2739 if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2742 /* we want the original as the wrapper is "free" of the security informations */
2743 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2744 method = mono_marshal_method_from_wrapper (method);
2749 /* First we look for method-level attributes */
2750 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2751 mono_class_init (method->klass);
2752 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2754 result = mono_declsec_get_method_demands_params (method, demands,
2755 SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2758 /* Here we use (or create) the class declarative cache to look for demands */
2759 flags = mono_declsec_flags_from_class (method->klass);
2762 mono_class_init (method->klass);
2763 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2765 result |= mono_declsec_get_class_demands_params (method->klass, demands,
2766 SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2769 /* The boolean return value is used as a shortcut in case nothing needs to
2770 be generated (e.g. LinkDemand[Choice] and InheritanceDemand[Choice]) */
2776 * Collect all Link actions: LinkDemand, NonCasLinkDemand and LinkDemandChoice (2.0).
2778 * Note: Don't use the content of actions if the function return FALSE.
2781 mono_declsec_get_linkdemands (MonoMethod *method, MonoDeclSecurityActions* klass, MonoDeclSecurityActions *cmethod)
2783 MonoBoolean result = FALSE;
2786 /* quick exit if no declarative security is present in the metadata */
2787 if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2790 /* we want the original as the wrapper is "free" of the security informations */
2791 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2792 method = mono_marshal_method_from_wrapper (method);
2797 /* results are independant - zeroize both */
2798 memset (cmethod, 0, sizeof (MonoDeclSecurityActions));
2799 memset (klass, 0, sizeof (MonoDeclSecurityActions));
2801 /* First we look for method-level attributes */
2802 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2803 mono_class_init (method->klass);
2805 result = mono_declsec_get_method_demands_params (method, cmethod,
2806 SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2809 /* Here we use (or create) the class declarative cache to look for demands */
2810 flags = mono_declsec_flags_from_class (method->klass);
2811 if (flags & (MONO_DECLSEC_FLAG_LINKDEMAND | MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND | MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE)) {
2812 mono_class_init (method->klass);
2814 result |= mono_declsec_get_class_demands_params (method->klass, klass,
2815 SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2822 * Collect all Inherit actions: InheritanceDemand, NonCasInheritanceDemand and InheritanceDemandChoice (2.0).
2824 * @klass The inherited class - this is the class that provides the security check (attributes)
2826 * return TRUE if inheritance demands (any kind) are present, FALSE otherwise.
2828 * Note: Don't use the content of actions if the function return FALSE.
2831 mono_declsec_get_inheritdemands_class (MonoClass *klass, MonoDeclSecurityActions* demands)
2833 MonoBoolean result = FALSE;
2836 /* quick exit if no declarative security is present in the metadata */
2837 if (!klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2840 /* Here we use (or create) the class declarative cache to look for demands */
2841 flags = mono_declsec_flags_from_class (klass);
2842 if (flags & (MONO_DECLSEC_FLAG_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE)) {
2843 mono_class_init (klass);
2844 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2846 result |= mono_declsec_get_class_demands_params (klass, demands,
2847 SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2854 * Collect all Inherit actions: InheritanceDemand, NonCasInheritanceDemand and InheritanceDemandChoice (2.0).
2856 * Note: Don't use the content of actions if the function return FALSE.
2859 mono_declsec_get_inheritdemands_method (MonoMethod *method, MonoDeclSecurityActions* demands)
2861 /* quick exit if no declarative security is present in the metadata */
2862 if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2865 /* we want the original as the wrapper is "free" of the security informations */
2866 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2867 method = mono_marshal_method_from_wrapper (method);
2872 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2873 mono_class_init (method->klass);
2874 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2876 return mono_declsec_get_method_demands_params (method, demands,
2877 SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2884 get_declsec_action (MonoImage *image, guint32 token, guint32 action, MonoDeclSecurityEntry *entry)
2886 guint32 cols [MONO_DECL_SECURITY_SIZE];
2890 int index = mono_metadata_declsec_from_index (image, token);
2894 t = &image->tables [MONO_TABLE_DECLSECURITY];
2895 for (i = index; i < t->rows; i++) {
2896 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2898 /* shortcut - index are ordered */
2899 if (token != cols [MONO_DECL_SECURITY_PARENT])
2902 if (cols [MONO_DECL_SECURITY_ACTION] == action) {
2903 const char *metadata = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2904 entry->blob = (char*) (metadata + 2);
2905 entry->size = mono_metadata_decode_blob_size (metadata, &metadata);
2914 mono_declsec_get_method_action (MonoMethod *method, guint32 action, MonoDeclSecurityEntry *entry)
2916 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2917 guint32 idx = mono_method_get_index (method);
2918 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2919 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2920 return get_declsec_action (method->klass->image, idx, action, entry);
2926 mono_declsec_get_class_action (MonoClass *klass, guint32 action, MonoDeclSecurityEntry *entry)
2929 guint32 flags = mono_declsec_flags_from_class (klass);
2930 if (declsec_flags_map [action] & flags) {
2931 guint32 idx = mono_metadata_token_index (klass->type_token);
2932 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2933 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2934 return get_declsec_action (klass->image, idx, action, entry);
2940 mono_declsec_get_assembly_action (MonoAssembly *assembly, guint32 action, MonoDeclSecurityEntry *entry)
2942 guint32 idx = 1; /* there is only one assembly */
2943 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2944 idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2946 return get_declsec_action (assembly->image, idx, action, entry);
2950 mono_reflection_call_is_assignable_to (MonoClass *klass, MonoClass *oklass, MonoError *error)
2952 MonoObject *res, *exc;
2954 static MonoMethod *method = NULL;
2958 if (method == NULL) {
2959 method = mono_class_get_method_from_name (mono_class_get_type_builder_class (), "IsAssignableTo", 1);
2964 * The result of mono_type_get_object_checked () might be a System.MonoType but we
2965 * need a TypeBuilder so use mono_class_get_ref_info (klass).
2967 g_assert (mono_class_has_ref_info (klass));
2968 g_assert (!strcmp (mono_object_class (mono_class_get_ref_info_raw (klass))->name, "TypeBuilder")); /* FIXME use handles */
2970 params [0] = mono_type_get_object_checked (mono_domain_get (), &oklass->byval_arg, error);
2971 return_val_if_nok (error, FALSE);
2973 MonoError inner_error;
2974 res = mono_runtime_try_invoke (method, mono_class_get_ref_info_raw (klass), params, &exc, &inner_error); /* FIXME use handles */
2976 if (exc || !is_ok (&inner_error)) {
2977 mono_error_cleanup (&inner_error);
2980 return *(MonoBoolean*)mono_object_unbox (res);
2984 * mono_reflection_type_get_type:
2985 * \param reftype the \c System.Type object
2986 * \returns the \c MonoType* associated with the C# \c System.Type object \p reftype.
2989 mono_reflection_type_get_type (MonoReflectionType *reftype)
2994 MonoType *result = mono_reflection_type_get_handle (reftype, &error);
2995 mono_error_assert_ok (&error);
3000 * mono_reflection_assembly_get_assembly:
3001 * \param refassembly the \c System.Reflection.Assembly object
3002 * \returns the \c MonoAssembly* associated with the C# \c System.Reflection.Assembly object \p refassembly.
3005 mono_reflection_assembly_get_assembly (MonoReflectionAssembly *refassembly)
3007 g_assert (refassembly);
3009 return refassembly->assembly;
3013 * mono_class_from_mono_type_handle:
3014 * \param reftype the \c System.Type handle
3015 * \returns the \c MonoClass* corresponding to the given type.
3018 mono_class_from_mono_type_handle (MonoReflectionTypeHandle reftype)
3020 return mono_class_from_mono_type (MONO_HANDLE_RAW (reftype)->type);