Merge pull request #4269 from kumpera/nuget-fixing-round2
[mono-project.git] / samples / embed / test-metadata.c
blobbcf3041696ede95c923bc4b7b5b12d1969e2eeab
1 #include <mono/jit/jit.h>
2 #include <glib.h>
4 /*
5 * Very simple mono embedding example.
6 * This sample shows how to access metadata elements from an image.
7 * Compile with:
8 * gcc -o test-metadata test-metadata.c `pkg-config --cflags --libs mono-2` -lm
9 * Run with:
10 * ./test-metadata namespace name
13 void
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));
19 void
20 output_ifaces (MonoClass* klass) {
21 MonoClass *iface;
22 gpointer iter = NULL;
24 printf ("Implements: ");
25 while ((iface = mono_class_get_interfaces (klass, &iter))) {
26 printf ("%s ", mono_class_get_name (iface));
28 printf ("\n");
31 void
32 output_nested (MonoClass* klass) {
33 MonoClass *nested;
34 gpointer iter = NULL;
36 printf ("Has nested types: ");
37 while ((nested = mono_class_get_nested_types (klass, &iter))) {
38 printf ("%s ", mono_class_get_name (nested));
40 printf ("\n");
43 void
44 output_fields (MonoClass* klass) {
45 MonoClassField *field;
46 gpointer iter = NULL;
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));
54 void
55 output_methods (MonoClass* klass) {
56 MonoMethod *method;
57 gpointer iter = NULL;
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);
67 void
68 output_properties (MonoClass* klass) {
69 MonoProperty *prop;
70 gpointer iter = NULL;
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));
78 void
79 output_events (MonoClass* klass) {
80 MonoEvent *event;
81 gpointer iter = NULL;
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));
89 int
90 main (int argc, char* argv[]) {
91 MonoDomain *domain;
92 MonoClass *klass;
93 MonoImage *image;
94 const char *ns, *name;
96 if (argc < 3){
97 fprintf (stderr, "Please provide namespace and name of a type in mscorlib.\n");
98 return 1;
100 ns = argv [1];
101 name = argv [2];
103 * mono_jit_init() creates a domain: each assembly is
104 * loaded and run in a MonoDomain.
106 domain = mono_jit_init (argv [0]);
107 if (argc > 3) {
108 MonoImageOpenStatus status;
109 image = mono_image_open (argv [3], &status);
110 if (!image) {
111 fprintf (stderr, "Can't load assembly \"%s\".\n", argv [3]);
112 return 1;
114 } else {
115 /* we default to using mscorlib */
116 image = mono_get_corlib ();
118 klass = mono_class_from_name (image, ns, name);
119 if (!klass) {
120 fprintf (stderr, "Class \"%s.%s\" not found.\n", ns, name);
121 return 1;
123 output_desc (klass);
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);
131 return 0;