Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Molaro <lupus@oddwiz.org>2004-06-19 19:10:45 +0400
committerPaolo Molaro <lupus@oddwiz.org>2004-06-19 19:10:45 +0400
commit0e43f1677e4c13faec3aa3fd4a411744999cccbc (patch)
treefd43de6a96f1c6f1c885859d37c0b98427b3919d /samples
parentca60c993754ab4ff57051926144013361495f582 (diff)
Test with metadata-related API sample usage.
svn path=/trunk/mono/; revision=29943
Diffstat (limited to 'samples')
-rw-r--r--samples/embed/test-metadata.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/samples/embed/test-metadata.c b/samples/embed/test-metadata.c
new file mode 100644
index 00000000000..689a30d55ac
--- /dev/null
+++ b/samples/embed/test-metadata.c
@@ -0,0 +1,132 @@
+#include <mono/jit/jit.h>
+
+/*
+ * Very simple mono embedding example.
+ * This sample shows how to access metadata elements from an image.
+ * Compile with:
+ * gcc -o test-metadata test-metadata.c `pkg-config --cflags --libs mono` -lm
+ * Run with:
+ * ./test-metadata namespace name
+ */
+
+void
+output_desc (MonoClass* klass) {
+ printf ("Class name: %s.%s\n", mono_class_get_namespace (klass), mono_class_get_name (klass));
+ printf ("Rank: %d, flags 0x%0x\n", mono_class_get_rank (klass), mono_class_get_flags (klass));
+}
+
+void
+output_ifaces (MonoClass* klass) {
+ MonoClass *iface;
+ gpointer iter = NULL;
+
+ printf ("Implements: ");
+ while ((iface = mono_class_get_interfaces (klass, &iter))) {
+ printf ("%s ", mono_class_get_name (iface));
+ }
+ printf ("\n");
+}
+
+void
+output_nested (MonoClass* klass) {
+ MonoClass *nested;
+ gpointer iter = NULL;
+
+ printf ("Has nested types: ");
+ while ((nested = mono_class_get_nested_types (klass, &iter))) {
+ printf ("%s ", mono_class_get_name (nested));
+ }
+ printf ("\n");
+}
+
+void
+output_fields (MonoClass* klass) {
+ MonoClassField *field;
+ gpointer iter = NULL;
+
+ while ((field = mono_class_get_fields (klass, &iter))) {
+ printf ("Field: %s, flags 0x%x\n", mono_field_get_name (field),
+ mono_field_get_flags (field));
+ }
+}
+
+void
+output_methods (MonoClass* klass) {
+ MonoMethod *method;
+ gpointer iter = NULL;
+
+ while ((method = mono_class_get_methods (klass, &iter))) {
+ guint32 flags, iflags;
+ flags = mono_method_get_flags (method, &iflags);
+ printf ("Method: %s, flags 0x%x, iflags 0x%x\n",
+ mono_method_get_name (method), flags, iflags);
+ }
+}
+
+void
+output_properties (MonoClass* klass) {
+ MonoProperty *prop;
+ gpointer iter = NULL;
+
+ while ((prop = mono_class_get_properties (klass, &iter))) {
+ printf ("Property: %s, flags 0x%x\n", mono_property_get_name (prop),
+ mono_property_get_flags (prop));
+ }
+}
+
+void
+output_events (MonoClass* klass) {
+ MonoEvent *event;
+ gpointer iter = NULL;
+
+ while ((event = mono_class_get_events (klass, &iter))) {
+ printf ("Event: %s, flags: 0x%x\n", mono_event_get_name (event),
+ mono_event_get_flags (event));
+ }
+}
+
+int
+main (int argc, char* argv[]) {
+ MonoDomain *domain;
+ MonoClass *klass;
+ MonoImage *image;
+ const char *ns, *name;
+
+ if (argc < 3){
+ fprintf (stderr, "Please provide namespace and name of a type in mscorlib.\n");
+ return 1;
+ }
+ ns = argv [1];
+ name = argv [2];
+ /*
+ * mono_jit_init() creates a domain: each assembly is
+ * loaded and run in a MonoDomain.
+ */
+ domain = mono_jit_init (argv [0]);
+ if (argc > 3) {
+ MonoImageOpenStatus status;
+ image = mono_image_open (argv [3], &status);
+ if (!image) {
+ fprintf (stderr, "Can't load assembly \"%s\".\n", argv [3]);
+ return 1;
+ }
+ } else {
+ /* we default to using mscorlib */
+ image = mono_get_corlib ();
+ }
+ klass = mono_class_from_name (image, ns, name);
+ if (!klass) {
+ fprintf (stderr, "Class \"%s.%s\" not found.\n", ns, name);
+ return 1;
+ }
+ output_desc (klass);
+ output_ifaces (klass);
+ output_nested (klass);
+ output_fields (klass);
+ output_methods (klass);
+ output_properties (klass);
+ output_events (klass);
+ mono_jit_cleanup (domain);
+ return 0;
+}
+