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
{
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
);
37 public static AssemblyBuilder ProxyAssembly
{
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();
51 if (args
.Name
.StartsWith("Mozilla.XPCOM.Interfaces.")) {
52 InterfaceGenerator gen
= new InterfaceGenerator(args
.Name
);
53 return gen
.Generate();
60 public static void Init()
62 if (serviceManager
!= IntPtr
.Zero
)
65 int res
= StartXPCOM(out serviceManager
);
67 throw new Exception(String
.Format("StartXPCOM failed: {0:X2}",
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) }
,
89 return ctor
.Invoke(new object[] { GetTestImpl() }
);