1 <h1>Metadata Reading
</h1>
3 <p>The Metadata API gives developers low-level access to the
4 information encoded in CLI modules: type and
5 methods definitions encoded in metadata as well as access to
6 the CIL code and embedded resources.
8 <p>Managed developers access this information using either the
9 System.Reflection API or a library
10 like
<a href=
"http://www.mono-project.com/Cecil">Cecil
</a>.
12 <p>To start using the Metadata API it is necessary
13 to
<a href=
"mono-api-assembly.html#cil_assembly_load">open an
14 assembly
</a> or a CIL image (a .dll or .exe file) using one of
16 <a href=
"mono-api-image.html#cil_image_open">CIL image
17 opening
</a> API calls.
19 <h2>Metadata Heaps
</h2>
21 <p>ECMA CLI images contain four heaps that store different
22 kinds of information, these are:
25 <li>GUID heap (called #GUID) contains
128-bit GUIDs,
26 you can get pointer to a GUID value by
27 calling
<a href=
"#api:mono_metadata_guid_heap">mono_metadata_guid_heap
</a>.
29 <li>User string heap (called #US), it contains user
30 visible strings, you can fetch a string from this heap
32 <a href=
"#api:mono_metadata_user_string">mono_metadata_user_string
</a>.
34 <li>Blob heap (called #blob), it contains binary
35 blobs of data, you can get a blob
36 by
<a href=
"#api:mono_metadata_blob_heap">mono_metadata_blob_heap
</a>.
37 You can determine the size of the blob using
38 the
<a href=
"#api:mono_metadata_decode_blob_size">mono_metadata_decode_blob_size
</a>.
42 <p>The ECMA file format also has an extra section called the
43 "#~" stream, this stream is the one that holds the metadata
44 tables. There is a high-level API to get access to the
45 contents of this API, described in the
46 section
<a href=
"#metadata-tables">Metadata Tables
</a>.
48 <h4><a name=
"api:mono_metadata_guid_heap">mono_metadata_guid_heap
</a></h4>
49 <h4><a name=
"api:mono_metadata_string_heap">mono_metadata_string_heap
</a></h4>
50 <h4><a name=
"api:mono_metadata_blob_heap">mono_metadata_blob_heap
</a></h4>
51 <h4><a name=
"api:mono_metadata_user_string">mono_metadata_user_string
</a></h4>
52 <h4><a name=
"api:mono_metadata_decode_blob_size">mono_metadata_decode_blob_size
</a></h4>
54 <a name=
"metadata-tables"/>
55 <h2>Metadata Tables
</h2>
57 <p>Metadata is encoded in a number of tables included on every
58 CIL image. These tables contain type definitions, member
59 definitions and so on, these constants are defined in the ECMA
335
60 specification Partition II section
22. The following table
61 shows the C constants defined in the Mono runtime and how they
62 map to the equivalent ECMA CLI metadata table:
68 <b>ECMA CLI Table Name
</b>
71 <b>C Constant Name
</b>
74 <b>Table Schema (Array Size + Columns Constants)
</b>
77 <td>Assembly
</td><td>MONO_TABLE_ASSEMBLY
</td><td>
81 <li>MONO_ASSEMBLY_HASH_ALG
82 <li>MONO_ASSEMBLY_MAJOR_VERSION
83 <li>MONO_ASSEMBLY_MINOR_VERSION
84 <li>MONO_ASSEMBLY_BUILD_NUMBER
85 <li>MONO_ASSEMBLY_REV_NUMBER
86 <li>MONO_ASSEMBLY_FLAGS
87 <li>MONO_ASSEMBLY_PUBLIC_KEY
88 <li>MONO_ASSEMBLY_NAME
89 <li>MONO_ASSEMBLY_CULTURE
94 <td>AssemblyOS
</td><td>MONO_TABLE_ASSEMBLYOS
</td><td>
98 <li>MONO_ASSEMBLYOS_PLATFORM
99 <li>MONO_ASSEMBLYOS_MAJOR_VERSION
100 <li>MONO_ASSEMBLYOS_MINOR_VERSION
105 <td>AssemblyProcessor
</td><td>MONO_TABLE_ASSEMBLYPROCESSOR
</td><td>Array
106 size: MONO_ASSEMBLYPROCESSOR_SIZE
108 <li>MONO_ASSEMBLY_PROCESSOR
113 <td>AssemblyRef
</td><td>MONO_TABLE_ASSEMBLYREF
</td><td>
115 MONO_ASSEMBLYREF_SIZE
117 <li>MONO_ASSEMBLYREF_MAJOR_VERSION
118 <li>MONO_ASSEMBLYREF_MINOR_VERSION
119 <li>MONO_ASSEMBLYREF_BUILD_NUMBER
120 <li>MONO_ASSEMBLYREF_REV_NUMBER
121 <li>MONO_ASSEMBLYREF_FLAGS
122 <li>MONO_ASSEMBLYREF_PUBLIC_KEY
123 <li>MONO_ASSEMBLYREF_NAME
124 <li>MONO_ASSEMBLYREF_CULTURE
125 <li>MONO_ASSEMBLYREF_HASH_VALUE
130 <td>AssemblyRefProcessor
</td><td>MONO_TABLE_ASSEMBLYREFPROCESSOR
</td><td>Array
131 size: MONO_ASSEMBLYREFPROC_SIZE
133 <li>MONO_ASSEMBLYREFPROC_PROCESSOR
134 <li>MONO_ASSEMBLYREFPROC_ASSEMBLYREF
139 <td>AssemblyRefOS
</td><td>MONO_TABLE_ASSEMBLYREFOS
</td><td>
141 <li>MONO_ASSEMBLYREFOS_SIZE
143 <li>MONO_ASSEMBLYREFOS_PLATFORM
144 <li>MONO_ASSEMBLYREFOS_MAJOR_VERSION
145 <li>MONO_ASSEMBLYREFOS_MINOR_VERSION
146 <li>MONO_ASSEMBLYREFOS_ASSEMBLYREF
151 <td>ClassLayout
</td><td>MONO_TABLE_CLASSLAYOUT
</td><td>
153 MONO_CLASSLAYOUT_SIZE
155 <li>MONO_CLASS_LAYOUT_PACKING_SIZE
156 <li>MONO_CLASS_LAYOUT_CLASS_SIZE
157 <li>MONO_CLASS_LAYOUT_PARENT
162 <td>Constant
</td><td>MONO_TABLE_CONSTANT
</td><td>
166 <li>MONO_CONSTANT_TYPE
167 <li>MONO_CONSTANT_PADDING
168 <li>MONO_CONSTANT_PARENT
169 <li>MONO_CONSTANT_VALUE
174 <td>CustomAttribute
</td><td>MONO_TABLE_CUSTOMATTRIBUTE
</td><td>
176 MONO_CUSTOM_ATTR_SIZE
178 <li>MONO_CUSTOM_ATTR_PARENT
179 <li>MONO_CUSTOM_ATTR_TYPE
180 <li>MONO_CUSTOM_ATTR_VALUE
185 <td>DeclSecurity
</td><td>MONO_TABLE_DECLSECURITY
</td><td>
187 MONO_DECL_SECURITY_SIZE
189 <li>MONO_DECL_SECURITY_ACTION
190 <li>MONO_DECL_SECURITY_PARENT
191 <li>MONO_DECL_SECURITY_PERMISSIONSET
196 <td>EventMap
</td><td>MONO_TABLE_EVENTMAP
</td><td>
200 <li>MONO_EVENT_MAP_PARENT
201 <li>MONO_EVENT_MAP_EVENTLIST
206 <td>EventPtr
</td><td>MONO_TABLE_EVENT_POINTER
</td><td>
208 MONO_EVENT_POINTER_SIZE
210 <li>MONO_EVENT_POINTER_EVENT
215 <td>Event
</td><td>MONO_TABLE_EVENT
</td><td>
216 Array size: MONO_EVENT_SIZE
225 <td>ExportedType
</td><td>MONO_TABLE_EXPORTEDTYPE
</td><td>
227 MONO_EXPORTEDTYPE_SIZE
229 <li>MONO_EXP_TYPE_FLAGS
230 <li>MONO_EXP_TYPE_TYPEDEF
231 <li>MONO_EXP_TYPE_NAME
232 <li>MONO_EXP_TYPE_NAMESPACE
233 <li>MONO_EXP_TYPE_IMPLEMENTATION
238 <td>Field
</td><td>MONO_TABLE_FIELD
</td><td>
239 Array size: MONO_FIELD_SIZE
243 <li>MONO_FIELD_SIGNATURE
248 <td>FieldLayoutt
</td><td>MONO_TABLE_FIELDLAYOUT
</td><td>
250 MONO_FIELDLAYOUT_SIZE
252 <li>MONO_FIELD_LAYOUT_OFFSET
253 <li>MONO_FIELD_LAYOUT_FIELD
258 <td>FieldMarshal
</td><td>MONO_TABLE_FIELDMARSHAL
</td><td>
260 MONO_FIELD_MARSHAL_SIZE
262 <li>MONO_FIELD_MARSHAL_PARENT
263 <li>MONO_FIELD_MARSHAL_NATIVE_TYPE
268 <td>FieldPtr
</td><td>MONO_TABLE_FIELD_POINTER
</td><td>
270 MONO_FIELD_POINTER_SIZE
272 <li>MONO_FIELD_POINTER_FIELD
277 <td>FieldRVA
</td><td>MONO_TABLE_FIELDRVA
</td><td>
281 <li>MONO_FIELD_RVA_RVA
282 <li>MONO_FIELD_RVA_FIELD
287 <td>File
</td><td>MONO_TABLE_FILE
</td><td>
288 Array size: MONO_FILE_SIZE
292 <li>MONO_FILE_HASH_VALUE
297 <td>GenericParam
</td><td>MONO_TABLE_GENERICPARAM
</td><td>
299 MONO_GENERICPARAM_SIZE
301 <li>MONO_GENERICPARAM_NUMBER
302 <li>MONO_GENERICPARAM_FLAGS
303 <li>MONO_GENERICPARAM_OWNER
304 <li>MONO_GENERICPARAM_NAME
309 <td>GenericParamConstraint
</td><td>MONO_TABLE_GENERICPARAMCONSTRAINT
</td><td>Array
310 size: MONO_GENERICPARAMCONSTRAINT_SIZE
312 <li>MONO_GENPARCONSTRAINT_GENERICPAR
313 <li>MONO_GENPARCONSTRAINT_CONSTRAINT
318 <td>ImplMap
</td><td>MONO_TABLE_IMPLMAP
</td><td>
322 <li>MONO_IMPLMAP_FLAGS
323 <li>MONO_IMPLMAP_MEMBER
324 <li>MONO_IMPLMAP_NAME
325 <li>MONO_IMPLMAP_SCOPE
331 <td>InterfaceImpl
</td><td>MONO_TABLE_INTERFACEIMPL
</td><td>
333 <li>MONO_INTERFACEIMPL_SIZE
335 <li>MONO_INTERFACEIMPL_CLASS
336 <li>MONO_INTERFACEIMPL_INTERFACE
342 <td>ManifestResource
</td><td>MONO_TABLE_MANIFESTRESOURCE
</td><td>Array
343 size: MONO_MANIFESTRESOURCE_SIZE
345 <li>MONO_MANIFEST_OFFSET
346 <li>MONO_MANIFEST_FLAGS
347 <li>MONO_MANIFEST_NAME
348 <li>MONO_MANIFEST_IMPLEMENTATION
354 <td>MemberRef
</td><td>MONO_TABLE_MEMBERREF
</td><td>
358 <li>MONO_MEMBERREF_CLASS
359 <li>MONO_MEMBERREF_NAME
360 <li>MONO_MEMBERREF_SIGNATURE
365 <td>MethodImpl
</td><td>MONO_TABLE_METHODIMPL
</td><td>
369 <li>MONO_METHODIMPL_CLASS
370 <li>MONO_METHODIMPL_BODY
371 <li>MONO_METHODIMPL_DECLARATION
377 <td>MethodSpec
</td><td>MONO_TABLE_METHODSPEC
</td><td>
381 <li>MONO_METHODSPEC_METHOD
382 <li>MONO_METHODSPEC_SIGNATURE
387 <td>MethodSemantics
</td><td>MONO_TABLE_METHODSEMANTICS
</td><td>Array
388 size: MONO_METHOD_SEMA_SIZE
390 <li>MONO_METHOD_SEMA_SEMANTICS
391 <li>MONO_METHOD_SEMA_METHOD
392 <li>MONO_METHOD_SEMA_ASSOCIATION
397 <td>Moduleref
</td><td>MONO_TABLE_MODULEREF
</td><td>
401 <li>MONO_MODULEREF_NAME
407 <td>Module
</td><td>MONO_TABLE_MODULE
</td><td>
413 <li>MONO_MODULE_GENERATION
417 <li>MONO_MODULE_ENCBASE
422 <td>TypeRef
</td><td>MONO_TABLE_TYPEREF
</td><td>
426 <li>MONO_TYPEREF_SCOPE
427 <li>MONO_TYPEREF_NAME
428 <li>MONO_TYPEREF_NAMESPACE
434 <td>MethodPtr
</td><td>MONO_TABLE_METHOD_POINTER
</td><td>
436 MONO_METHOD_POINTER_SIZE
438 <li>MONO_METHOD_POINTER_METHOD
443 <td>Method
</td><td>MONO_TABLE_METHOD
</td><td>
448 <li>MONO_METHOD_IMPLFLAGS
449 <li>MONO_METHOD_FLAGS
451 <li>MONO_METHOD_SIGNATURE
452 <li>MONO_METHOD_PARAMLIST
458 <td>NestedClass
</td><td>MONO_TABLE_NESTEDCLASS
</td><td>
460 MONO_NESTEDCLASS_SIZE
462 <li>MONO_NESTED_CLASS_NESTED
463 <li>MONO_NESTED_CLASS_ENCLOSING
469 <td>ParamPtr
</td><td>MONO_TABLE_PARAM_POINTER
</td><td>
471 MONO_PARAM_POINTER_SIZE
473 <li>MONO_PARAM_POINTER_PARAM
478 <td>Param
</td><td>MONO_TABLE_PARAM
</td><td>
479 Array size: MONO_PARAM_SIZE
482 <li>MONO_PARAM_SEQUENCE
488 <td>PropertyMap
</td><td>MONO_TABLE_PROPERTYMAP
</td><td>
490 MONO_PROPERTY_MAP_SIZE
492 <li>MONO_PROPERTY_MAP_PARENT
493 <li>MONO_PROPERTY_MAP_PROPERTY_LIST
498 <td>PropertyPtr
</td><td>MONO_TABLE_PROPERTY_POINTER
</td><td>Array
499 size: MONO_PROPERTY_POINTER_SIZE
501 <li>MONO_PROPERTY_POINTER_PROPERTY
506 <td>Property
</td><td>MONO_TABLE_PROPERTY
</td><td>
510 <li>MONO_PROPERTY_FLAGS
511 <li>MONO_PROPERTY_NAME
512 <li>MONO_PROPERTY_TYPE
517 <td>StandaloneSig
</td><td>MONO_TABLE_STANDALONESIG
</td><td>
519 <li>MONO_STAND_ALONE_SIGNATURE_SIZE
521 <li>MONO_STAND_ALONE_SIGNATURE
527 <td>TypeDef
</td><td>MONO_TABLE_TYPEDEF
</td><td>
531 <li>MONO_TYPEDEF_FLAGS
532 <li>MONO_TYPEDEF_NAME
533 <li>MONO_TYPEDEF_NAMESPACE
534 <li>MONO_TYPEDEF_EXTENDS
535 <li>MONO_TYPEDEF_FIELD_LIST
536 <li>MONO_TYPEDEF_METHOD_LIST
542 <td>TypeSpec
</td><td>MONO_TABLE_TYPESPEC
</td><td>
546 <li>MONO_TYPESPEC_SIGNATURE
554 <p>Each table can contain zero or more rows, you must call the
555 <a href=
"#api:mono_metadata_table_rows">mono_metadata_table_rows
</a>
556 to obtain the number of rows in a table, and then you can
557 extract individual row values by using
558 the
<a href=
"#api:mono_metadata_decode_row">mono_metadata_decode_row
</a>
560 the
<a href=
"#api:mono_metadata_decode_row_col">mono_metadata_decode_row_col
</a>.
561 When decoding rows you must provide an guint32 array large
562 enough to hold as many columns as the table contains.
564 <p>The metadata tables are stored in the MonoImage, you obtain
565 a pointer to the MonoTableInfo by calling
566 the
<a href=
"#api:mono_image_get_table_info">mono_image_get_table_info
</a>
567 and then you can scan those tables, for example:
569 <pre class=
"prettyprint">
571 * Dumps a few fields from the AssemblyRef table
573 void DumpAssemblyRefs (MonoImage *image)
575 /* Get a pointer to the AssemblyRef metadata table */
576 MonoTableInfo *t = mono_image_get_table_info (image, MONO_TABLE_ASSEMBLYREF);
578 /* Fetch the number of rows available in the table */
579 int rows = mono_table_info_get_rows (t);
582 /* For each row, print some of its values */
583 for (i =
0; i
< rows; i++){
584 /* Space where we extract one row from the metadata table */
585 guint32 cols [MONO_ASSEMBLYREF_SIZE];
587 /* Extract the row into the array cols */
588 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
590 fprintf (output,
"%d: Version=%d.%d.%d.%d\n\tName=%s\n", i +
1,
591 cols [MONO_ASSEMBLYREF_MAJOR_VERSION],
592 cols [MONO_ASSEMBLYREF_MINOR_VERSION],
593 cols [MONO_ASSEMBLYREF_BUILD_NUMBER],
594 cols [MONO_ASSEMBLYREF_REV_NUMBER],
595 mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME]));
600 <p>The above program shows the following output when ran on
604 1: Version=
1.0.5000.0
606 2: Version=
1.0.5000.0
608 3: Version=
1.0.5000.0
614 <h3>Metadata Tables API
</h3>
616 <p>These are the APIs for dealing with tables:
618 <h4><a name=
"api:mono_image_get_table_info">mono_image_get_table_info
</a></h4>
619 <h4><a name=
"api:mono_image_get_table_rows">mono_image_get_table_rows
</a></h4>
620 <h4><a name=
"api:mono_metadata_decode_row_col">mono_metadata_decode_row_col
</a></h4>
621 <h4><a name=
"api:mono_metadata_decode_row">mono_metadata_decode_row
</a></h4>
622 <h4><a name=
"api:mono_metadata_compute_size">mono_metadata_compute_size
</a></h4>
623 <h4><a name=
"api:mono_metadata_custom_attrs_from_index">mono_metadata_custom_attrs_from_index
</a></h4>
624 <h4><a name=
"api:mono_metadata_decode_signed_value">mono_metadata_decode_signed_value
</a></h4>
625 <h4><a name=
"api:mono_metadata_decode_value">mono_metadata_decode_value
</a></h4>
626 <h4><a name=
"api:mono_metadata_encode_value">mono_metadata_encode_value
</a></h4>
628 <h2>Metadata access API
</h2>
630 <p>This is the low-level API for accessing the metadata
633 <h4><a name=
"api:mono_pe_file_open">mono_pe_file_open
</a></h4>
634 <h4><a name=
"api:mono_metadata_events_from_typedef">mono_metadata_events_from_typedef
</a></h4>
635 <h4><a name=
"api:mono_metadata_decode_table_row">mono_metadata_decode_table_row
</a></h4>
636 <h4><a name=
"api:mono_metadata_decode_table_row_col">mono_metadata_decode_table_row_col
</a></h4>
637 <h4><a name=
"api:mono_metadata_field_info">mono_metadata_field_info
</a></h4>
638 <h4><a name=
"api:mono_metadata_free_array">mono_metadata_free_array
</a></h4>
639 <h4><a name=
"api:mono_metadata_free_marshal_spec">mono_metadata_free_marshal_spec
</a></h4>
640 <h4><a name=
"api:mono_metadata_free_mh">mono_metadata_free_mh
</a></h4>
641 <h4><a name=
"api:mono_metadata_free_type">mono_metadata_free_type
</a></h4>
642 <h4><a name=
"api:mono_metadata_get_constant_index">mono_metadata_get_constant_index
</a></h4>
643 <h4><a name=
"api:mono_metadata_get_marshal_info">mono_metadata_get_marshal_info
</a></h4>
644 <h4><a name=
"api:mono_metadata_implmap_from_method">mono_metadata_implmap_from_method
</a></h4>
645 <h4><a name=
"api:mono_metadata_interfaces_from_typedef">mono_metadata_interfaces_from_typedef
</a></h4>
646 <h4><a name=
"api:mono_metadata_locate">mono_metadata_locate
</a></h4>
647 <h4><a name=
"api:mono_metadata_locate_token">mono_metadata_locate_token
</a></h4>
648 <h4><a name=
"api:mono_metadata_methods_from_event">mono_metadata_methods_from_event
</a></h4>
649 <h4><a name=
"api:mono_metadata_methods_from_property">mono_metadata_methods_from_property
</a></h4>
650 <h4><a name=
"api:mono_metadata_nested_in_typedef">mono_metadata_nested_in_typedef
</a></h4>
651 <h4><a name=
"api:mono_metadata_nesting_typedef">mono_metadata_nesting_typedef
</a></h4>
652 <h4><a name=
"api:mono_metadata_packing_from_typedef">mono_metadata_packing_from_typedef
</a></h4>
653 <h4><a name=
"api:mono_metadata_properties_from_typedef">mono_metadata_properties_from_typedef
</a></h4>
654 <h4><a name=
"api:mono_metadata_token_from_dor">mono_metadata_token_from_dor
</a></h4>
655 <h4><a name=
"api:mono_metadata_translate_token_index">mono_metadata_translate_token_index
</a></h4>
656 <h4><a name=
"api:mono_metadata_typedef_from_field">mono_metadata_typedef_from_field
</a></h4>
657 <h4><a name=
"api:mono_metadata_typedef_from_method">mono_metadata_typedef_from_method
</a></h4>
658 <h4><a name=
"api:mono_metadata_type_equal">mono_metadata_type_equal
</a></h4>
659 <h4><a name=
"api:mono_metadata_type_hash">mono_metadata_type_hash
</a></h4>
660 <h4><a name=
"api:mono_metadata_declsec_from_index">mono_metadata_declsec_from_index
</a></h4>
661 <h4><a name=
"api:mono_metadata_free_method_signature">mono_metadata_free_method_signature
</a></h4>
663 <h3>Retrieving Objects from Tokens
</h3>
665 <h4><a name=
"api:mono_metadata_parse_array">mono_metadata_parse_array
</a></h4>
666 <h4><a name=
"api:mono_metadata_parse_custom_mod">mono_metadata_parse_custom_mod
</a></h4>
667 <h4><a name=
"api:mono_metadata_parse_field_type">mono_metadata_parse_field_type
</a></h4>
668 <h4><a name=
"api:mono_metadata_parse_marshal_spec">mono_metadata_parse_marshal_spec
</a></h4>
669 <h4><a name=
"api:mono_metadata_parse_method_signature">mono_metadata_parse_method_signature
</a></h4>
670 <h4><a name=
"api:mono_metadata_parse_mh">mono_metadata_parse_mh
</a></h4>
671 <h4><a name=
"api:mono_metadata_parse_param">mono_metadata_parse_param
</a></h4>
672 <h4><a name=
"api:mono_metadata_parse_ret_type">mono_metadata_parse_ret_type
</a></h4>
673 <h4><a name=
"api:mono_metadata_parse_signature">mono_metadata_parse_signature
</a></h4>
674 <h4><a name=
"api:mono_metadata_parse_typedef_or_ref">mono_metadata_parse_typedef_or_ref
</a></h4>
675 <h4><a name=
"api:mono_metadata_parse_type">mono_metadata_parse_type
</a></h4>
677 <h2>Generics Support
</h2>
679 <h4><a name=
"api:mono_metadata_generic_class_equal">mono_metadata_generic_class_equal
</a></h4>
680 <h4><a name=
"api:mono_metadata_generic_class_hash">mono_metadata_generic_class_hash
</a></h4>
681 <h4><a name=
"api:mono_metadata_generic_class_is_valuetype">mono_metadata_generic_class_is_valuetype
</a></h4>
685 <h4><a name=
"api:mono_ldtoken">mono_ldtoken
</a></h4>
686 <h4><a name=
"api:mono_ldstr">mono_ldstr
</a></h4>
687 <h4><a name=
"api:mono_exception_from_token">mono_exception_from_token
</a></h4>
691 <h4><a name=
"api:mono_opcode_name">mono_opcode_name
</a></h4>
692 <h4><a name=
"api:mono_opcode_value">mono_opcode_value
</a></h4>