2009-03-10 Zoltan Varga <vargaz@gmail.com>
[mono-debugger.git] / samples / embed / test-metadata.c
blob689a30d55ac4c1db090cb7128b3833d3e367d0ca
1 #include <mono/jit/jit.h>
3 /*
4 * Very simple mono embedding example.
5 * This sample shows how to access metadata elements from an image.
6 * Compile with:
7 * gcc -o test-metadata test-metadata.c `pkg-config --cflags --libs mono` -lm
8 * Run with:
9 * ./test-metadata namespace name
12 void
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));
18 void
19 output_ifaces (MonoClass* klass) {
20 MonoClass *iface;
21 gpointer iter = NULL;
23 printf ("Implements: ");
24 while ((iface = mono_class_get_interfaces (klass, &iter))) {
25 printf ("%s ", mono_class_get_name (iface));
27 printf ("\n");
30 void
31 output_nested (MonoClass* klass) {
32 MonoClass *nested;
33 gpointer iter = NULL;
35 printf ("Has nested types: ");
36 while ((nested = mono_class_get_nested_types (klass, &iter))) {
37 printf ("%s ", mono_class_get_name (nested));
39 printf ("\n");
42 void
43 output_fields (MonoClass* klass) {
44 MonoClassField *field;
45 gpointer iter = NULL;
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));
53 void
54 output_methods (MonoClass* klass) {
55 MonoMethod *method;
56 gpointer iter = NULL;
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);
66 void
67 output_properties (MonoClass* klass) {
68 MonoProperty *prop;
69 gpointer iter = NULL;
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));
77 void
78 output_events (MonoClass* klass) {
79 MonoEvent *event;
80 gpointer iter = NULL;
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));
88 int
89 main (int argc, char* argv[]) {
90 MonoDomain *domain;
91 MonoClass *klass;
92 MonoImage *image;
93 const char *ns, *name;
95 if (argc < 3){
96 fprintf (stderr, "Please provide namespace and name of a type in mscorlib.\n");
97 return 1;
99 ns = argv [1];
100 name = argv [2];
102 * mono_jit_init() creates a domain: each assembly is
103 * loaded and run in a MonoDomain.
105 domain = mono_jit_init (argv [0]);
106 if (argc > 3) {
107 MonoImageOpenStatus status;
108 image = mono_image_open (argv [3], &status);
109 if (!image) {
110 fprintf (stderr, "Can't load assembly \"%s\".\n", argv [3]);
111 return 1;
113 } else {
114 /* we default to using mscorlib */
115 image = mono_get_corlib ();
117 klass = mono_class_from_name (image, ns, name);
118 if (!klass) {
119 fprintf (stderr, "Class \"%s.%s\" not found.\n", ns, name);
120 return 1;
122 output_desc (klass);
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);
130 return 0;