1 #include <mono/jit/jit.h>
4 * Very simple mono embedding example.
5 * This sample shows how to access metadata elements from an image.
7 * gcc -o test-metadata test-metadata.c `pkg-config --cflags --libs mono` -lm
9 * ./test-metadata namespace name
13 output_desc (MonoClass
* klass
) {
14 printf ("Class name: %s.%s\n", mono_class_get_namespace (klass
), mono_class_get_name (klass
));
15 printf ("Rank: %d, flags 0x%0x\n", mono_class_get_rank (klass
), mono_class_get_flags (klass
));
19 output_ifaces (MonoClass
* klass
) {
23 printf ("Implements: ");
24 while ((iface
= mono_class_get_interfaces (klass
, &iter
))) {
25 printf ("%s ", mono_class_get_name (iface
));
31 output_nested (MonoClass
* klass
) {
35 printf ("Has nested types: ");
36 while ((nested
= mono_class_get_nested_types (klass
, &iter
))) {
37 printf ("%s ", mono_class_get_name (nested
));
43 output_fields (MonoClass
* klass
) {
44 MonoClassField
*field
;
47 while ((field
= mono_class_get_fields (klass
, &iter
))) {
48 printf ("Field: %s, flags 0x%x\n", mono_field_get_name (field
),
49 mono_field_get_flags (field
));
54 output_methods (MonoClass
* klass
) {
58 while ((method
= mono_class_get_methods (klass
, &iter
))) {
59 guint32 flags
, iflags
;
60 flags
= mono_method_get_flags (method
, &iflags
);
61 printf ("Method: %s, flags 0x%x, iflags 0x%x\n",
62 mono_method_get_name (method
), flags
, iflags
);
67 output_properties (MonoClass
* klass
) {
71 while ((prop
= mono_class_get_properties (klass
, &iter
))) {
72 printf ("Property: %s, flags 0x%x\n", mono_property_get_name (prop
),
73 mono_property_get_flags (prop
));
78 output_events (MonoClass
* klass
) {
82 while ((event
= mono_class_get_events (klass
, &iter
))) {
83 printf ("Event: %s, flags: 0x%x\n", mono_event_get_name (event
),
84 mono_event_get_flags (event
));
89 main (int argc
, char* argv
[]) {
93 const char *ns
, *name
;
96 fprintf (stderr
, "Please provide namespace and name of a type in mscorlib.\n");
102 * mono_jit_init() creates a domain: each assembly is
103 * loaded and run in a MonoDomain.
105 domain
= mono_jit_init (argv
[0]);
107 MonoImageOpenStatus status
;
108 image
= mono_image_open (argv
[3], &status
);
110 fprintf (stderr
, "Can't load assembly \"%s\".\n", argv
[3]);
114 /* we default to using mscorlib */
115 image
= mono_get_corlib ();
117 klass
= mono_class_from_name (image
, ns
, name
);
119 fprintf (stderr
, "Class \"%s.%s\" not found.\n", ns
, name
);
123 output_ifaces (klass
);
124 output_nested (klass
);
125 output_fields (klass
);
126 output_methods (klass
);
127 output_properties (klass
);
128 output_events (klass
);
129 mono_jit_cleanup (domain
);