[Mono-list] Mono Embedding C-API : Get a MonoClass* without knowing the namespace.

candl kredaxx at gmail.com
Fri Mar 7 21:21:32 UTC 2014

Hi,Suppose I have this sample C# code:
using System;namespace sampleApp {  class Bar {    public static void Work()
{       Console.WriteLine("Bar.Work called");    }  }  class Program {   
public static void Main(string[] args) {       
Console.WriteLine("Program.Main called");    }   }}
On C side I want to launch mono, obtain Bar class pointer and call the
Bar.Work static method on it. This is easily achievable as shown below: 
#include <mono/jit/jit.h>#include <mono/metadata/mono-config.h>#include
<mono/metadata/metadata.h>#include <mono/metadata/appdomain.h>#include
<mono/metadata/class.h> #include <mono/metadata/assembly.h>#include
<mono/metadata/image.h>#include <mono/metadata/object.h>#include
<mono/metadata/debug-helpers.h>int main(int argc, char **argv) {       
mono_config_parse(NULL); 	MonoDomain *domain = mono_jit_init_version("app",
"v4.0.30319");	MonoAssembly *assembly = mono_domain_assembly_open(domain,
"sampleApp.exe");  	MonoImage *image = mono_assembly_get_image(assembly);         
mono_jit_exec(domain, assembly, argc, argv);        MonoClass *klass =
mono_class_from_name(image, "sampleApp", "Bar");        MonoMethod *method =
mono_class_get_method_from_name(klass, "Work", 0);                void
*params[1] = { NULL };        mono_runtime_invoke(method, NULL, params,
NULL);         mono_jit_cleanup(domain, assembly);        return 0;}
The crucial part is obtaining the /MonoClass*/. /mono_class_from_name()/
function takes a namespace as a parameter. This cannot be omitted. Since I
know the namespace of the class this is not a big deal. But what if I don't
know the classes' namespace? 
When I launch my C program I want to scan the opened assembly for a class
Bar which has a Work static method in it. Ultimately I want to call the Work
method from C code.
I was unable to find any functions which would let me get a list types of
the assembly, list of namespaces or anything like that.
I've used http://www.mono-project.com/Embedding_Mono as reference, together
with embedding samples in mono's repository but I was unable to find
There's the reflection API available from C code which could potentially
achieve what I want, but I have no idea how to use it, though :(.Would
appreciate any help. Thanks.

View this message in context: http://mono.1490590.n4.nabble.com/Mono-Embedding-C-API-Get-a-MonoClass-without-knowing-the-namespace-tp4662178.html
Sent from the Mono - General mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20140307/74b3a313/attachment-0001.html>

More information about the Mono-list mailing list