1 #include <mono/jit/jit.h>
5 * Very simple mono embedding example.
6 * This sample shows how to access metadata elements from an image.
8 * gcc -o test-metadata test-metadata.c `pkg-config --cflags --libs mono-2` -lm
10 * ./test-metadata namespace name
14 output_desc (MonoClass
* klass
) {
15 printf ("Class name: %s.%s\n", mono_class_get_namespace (klass
), mono_class_get_name (klass
));
16 printf ("Rank: %d, flags 0x%0x\n", mono_class_get_rank (klass
), mono_class_get_flags (klass
));
20 output_ifaces (MonoClass
* klass
) {
24 printf ("Implements: ");
25 while ((iface
= mono_class_get_interfaces (klass
, &iter
))) {
26 printf ("%s ", mono_class_get_name (iface
));
32 output_nested (MonoClass
* klass
) {
36 printf ("Has nested types: ");
37 while ((nested
= mono_class_get_nested_types (klass
, &iter
))) {
38 printf ("%s ", mono_class_get_name (nested
));
44 output_fields (MonoClass
* klass
) {
45 MonoClassField
*field
;
48 while ((field
= mono_class_get_fields (klass
, &iter
))) {
49 printf ("Field: %s, flags 0x%x\n", mono_field_get_name (field
),
50 mono_field_get_flags (field
));
55 output_methods (MonoClass
* klass
) {
59 while ((method
= mono_class_get_methods (klass
, &iter
))) {
60 guint32 flags
, iflags
;
61 flags
= mono_method_get_flags (method
, &iflags
);
62 printf ("Method: %s, flags 0x%x, iflags 0x%x\n",
63 mono_method_get_name (method
), flags
, iflags
);
68 output_properties (MonoClass
* klass
) {
72 while ((prop
= mono_class_get_properties (klass
, &iter
))) {
73 printf ("Property: %s, flags 0x%x\n", mono_property_get_name (prop
),
74 mono_property_get_flags (prop
));
79 output_events (MonoClass
* klass
) {
83 while ((event
= mono_class_get_events (klass
, &iter
))) {
84 printf ("Event: %s, flags: 0x%x\n", mono_event_get_name (event
),
85 mono_event_get_flags (event
));
90 main (int argc
, char* argv
[]) {
94 const char *ns
, *name
;
97 fprintf (stderr
, "Please provide namespace and name of a type in mscorlib.\n");
103 * mono_jit_init() creates a domain: each assembly is
104 * loaded and run in a MonoDomain.
106 domain
= mono_jit_init (argv
[0]);
108 MonoImageOpenStatus status
;
109 image
= mono_image_open (argv
[3], &status
);
111 fprintf (stderr
, "Can't load assembly \"%s\".\n", argv
[3]);
115 /* we default to using mscorlib */
116 image
= mono_get_corlib ();
118 klass
= mono_class_from_name (image
, ns
, name
);
120 fprintf (stderr
, "Class \"%s.%s\" not found.\n", ns
, name
);
124 output_ifaces (klass
);
125 output_nested (klass
);
126 output_fields (klass
);
127 output_methods (klass
);
128 output_properties (klass
);
129 output_events (klass
);
130 mono_jit_cleanup (domain
);