Back to index

lightning-sunbird  0.9+nobinonly
wrapped-clr.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Reflection;
00003 using System.Runtime.InteropServices;
00004 using Mozilla.XPCOM;
00005 
00006 using MethodDescriptor = Mozilla.XPCOM.TypeInfo.MethodDescriptor;
00007 using ParamDescriptor = Mozilla.XPCOM.TypeInfo.ParamDescriptor;
00008 using ParamFlags = Mozilla.XPCOM.TypeInfo.ParamFlags;
00009 using TypeDescriptor = Mozilla.XPCOM.TypeInfo.TypeDescriptor;
00010 using TypeFlags = Mozilla.XPCOM.TypeInfo.TypeFlags;
00011 using TypeTag = Mozilla.XPCOM.TypeInfo.TypeTag;
00012 
00013 namespace Mozilla.XPCOM {
00014 internal class CLRWrapper
00015 {
00016     public static IntPtr Wrap(object o, ref Guid iid)
00017     {
00018         CLRWrapper wrapper = new CLRWrapper(o, ref iid);
00019         return wrapper.MakeXPCOMProxy();
00020     }
00021     
00022     int InvokeMethod(int index, IntPtr args)
00023     {
00024         Console.WriteLine("invoking method {0} on {1}", index,
00025                           wrappedObj);
00026         MethodDescriptor desc = TypeInfo.GetMethodData(wrappedAsIID, index);
00027         Type ifaceType = TypeInfo.TypeForIID(wrappedAsIID);
00028         // Console.WriteLine("ifaceType: {0}", ifaceType);
00029         MethodInfo meth = ifaceType.GetMethod(desc.name);
00030         // Console.WriteLine("meth: {0}", meth);
00031         // This might just throw on you, if it's not a void-taking method
00032         meth.Invoke(wrappedObj, new object[0]);
00033         return 0;
00034     }
00035     
00036     CLRWrapper(object o, ref Guid iid)
00037     {
00038         wrappedObj = o;
00039         wrappedAsIID = iid;
00040     }
00041     
00042     object wrappedObj;
00043     Guid wrappedAsIID;
00044     
00045     delegate int MethodInvoker(int index, IntPtr args);
00046     
00047     [DllImport("xpcom-dotnet.so")]
00048         static extern IntPtr WrapCLRObject(MethodInvoker del, ref Guid iid);
00049     
00050     IntPtr MakeXPCOMProxy()
00051     {
00052         return WrapCLRObject(new MethodInvoker(this.InvokeMethod),
00053                              ref wrappedAsIID);
00054     }
00055 }
00056 
00057 } // namespace Mozilla.XPCOM