public TypeInfo(EventSource source, SchemaNaming naming, Type t) { this.toWMI = (ConvertFuncToWMI)InstrumentedAssembly.mapTypeToToWMIFunc[t]; this.toNET = (ConvertFuncToNET)InstrumentedAssembly.mapTypeToToNETFunc[t]; this.t = t; isSTA = Thread.CurrentThread.ApartmentState == ApartmentState.STA; ManagementClass eventClass = new ManagementClass(naming.NamespaceName + ":" + ManagedNameAttribute.GetMemberName(t)); PropertyInfo prop = typeof(ManagementBaseObject).GetProperty("WmiObject", BindingFlags.Instance | BindingFlags.NonPublic); ManagementObject evt = eventClass.CreateInstance(); obj = (IWbemClassObject_DoNotMarshal)prop.GetValue(evt, null); this.source = source; SetBatchSize(batchSize); IWbemClassObject_DoNotMarshal obj2; obj.Clone_(out obj2); xoa1[0] = (IWbemObjectAccess)obj2; oa1test = new ClassObjectArray(xoa1); writeDWORD1 = new WriteDWORD(xoa1[0].WriteDWORD_); writeQWORD1 = new WriteQWORD(xoa1[0].WriteQWORD_); #if xxx string code = CodeSpit.Spit(t, xoa1[0]); CSharpCodeProvider provider = new CSharpCodeProvider(); ICodeCompiler compiler = provider.CreateCompiler(); CompilerParameters parameters = new CompilerParameters(); parameters.GenerateInMemory = true; parameters.ReferencedAssemblies.Add(t.Assembly.Location); parameters.ReferencedAssemblies.Add(typeof(WriteDWORD).Assembly.Location); parameters.ReferencedAssemblies.Add(typeof(Event).Assembly.Location); CompilerResults results = compiler.CompileAssemblyFromSource(parameters, code); foreach (CompilerError err in results.Errors) { Console.WriteLine(err.ToString()); } Type dynType = results.CompiledAssembly.GetType("Hack"); MethodInfo doit = dynType.GetMethod("Func"); managedToIWbem = (ManagedToIWbem)Delegate.CreateDelegate(typeof(ManagedToIWbem), doit); #endif }
public static Hashtable mapTypeToToNETFunc = new Hashtable(); //TODO LOCK THIS public InstrumentedAssembly(Assembly assembly, SchemaNaming naming) { this.naming = naming; CSharpCodeProvider provider = new CSharpCodeProvider(); ICodeCompiler compiler = provider.CreateCompiler(); CompilerParameters parameters = new CompilerParameters(); parameters.GenerateInMemory = true; parameters.ReferencedAssemblies.Add(assembly.Location); parameters.ReferencedAssemblies.Add(typeof(BaseEvent).Assembly.Location); parameters.ReferencedAssemblies.Add(typeof(System.ComponentModel.Component).Assembly.Location); CompilerResults results = compiler.CompileAssemblyFromSource(parameters, naming.Code); foreach (CompilerError err in results.Errors) { Console.WriteLine(err.ToString()); } Type dynType = results.CompiledAssembly.GetType("WMINET_Converter"); Type[] types = (Type[])dynType.GetField("netTypes").GetValue(null); MethodInfo[] toWMIs = (MethodInfo[])dynType.GetField("toWMIMethods").GetValue(null); MethodInfo[] toNETs = (MethodInfo[])dynType.GetField("toNETMethods").GetValue(null); Object theOne = dynType.GetField("theOne").GetValue(null); for (int i = 0; i < types.Length; i++) { ConvertFuncToWMI toWMI = (ConvertFuncToWMI)Delegate.CreateDelegate(typeof(ConvertFuncToWMI), theOne, toWMIs[i].Name); ConvertFuncToNET toNET = (ConvertFuncToNET)Delegate.CreateDelegate(typeof(ConvertFuncToNET), theOne, toNETs[i].Name); mapTypeToToWMIFunc.Add(types[i], toWMI); mapTypeToToNETFunc.Add(types[i], toNET); } // TODO: Is STA/MTA all we have to worry about? if (Thread.CurrentThread.ApartmentState == ApartmentState.STA) { // We are on an STA thread. Create the event source on an MTA Thread thread = new Thread(new ThreadStart(InitEventSource)); thread.ApartmentState = ApartmentState.MTA; thread.Start(); thread.Join(); } else { InitEventSource(); } }