Import from 1.9a8 tarball
[mozilla-extra.git] / extensions / mono / src / components.cs
blob974f77c1a3092f4b5fc6f236925118a9bf199a54
1 using System;
2 using Mozilla.XPCOM;
3 using System.Reflection;
4 using System.Reflection.Emit;
5 using System.Runtime.InteropServices;
7 namespace Mozilla.XPCOM {
9 public class Components
11 [DllImport("xpcom-dotnet.so")]
12 static extern int StartXPCOM(out IntPtr srvmgr);
14 static IntPtr serviceManager = IntPtr.Zero;
16 public static IntPtr ServiceManager {
17 get {
18 return serviceManager;
22 static void RegisterAppDomainHooks()
24 AppDomain.CurrentDomain.TypeResolve +=
25 new ResolveEventHandler(TypeResolve);
26 AppDomain.CurrentDomain.AssemblyResolve +=
27 new ResolveEventHandler(AssemblyResolve);
28 Console.WriteLine("XPCOM AppDomain hooks registered.");
31 static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
33 Console.WriteLine("Resolving: {0}", args.Name);
34 return null;
37 public static AssemblyBuilder ProxyAssembly {
38 get {
39 return ProxyGenerator.ProxyAssembly;
43 static Assembly TypeResolve(object sender, ResolveEventArgs args)
45 if (args.Name.StartsWith("Mozilla.XPCOM.Proxies.")) {
46 ProxyGenerator gen = new ProxyGenerator(args.Name);
47 return gen.Generate();
50 #if NOTYET
51 if (args.Name.StartsWith("Mozilla.XPCOM.Interfaces.")) {
52 InterfaceGenerator gen = new InterfaceGenerator(args.Name);
53 return gen.Generate();
55 #endif
57 return null;
60 public static void Init()
62 if (serviceManager != IntPtr.Zero)
63 return;
65 int res = StartXPCOM(out serviceManager);
66 if (res != 0) {
67 throw new Exception(String.Format("StartXPCOM failed: {0:X2}",
68 res));
71 RegisterAppDomainHooks();
74 [DllImport("test.so", EntryPoint="GetImpl")]
75 public static extern IntPtr GetTestImpl();
77 public static object CreateInstance(string contractId, Type iface)
79 String typeName = iface.FullName;
80 String proxyName = typeName.Replace(".Interfaces.", ".Proxies.");
81 Console.WriteLine("Need proxy class {0} for {1}", proxyName, typeName);
82 Type proxyType = System.Type.GetType(proxyName);
83 if (contractId == "@off.net/test-component;1") {
84 ConstructorInfo ctor =
85 proxyType.GetConstructor(BindingFlags.NonPublic |
86 BindingFlags.Instance,
87 null, new Type[1] { typeof(IntPtr) },
88 null);
89 return ctor.Invoke(new object[] { GetTestImpl() });
91 return null;