//public StandAloneExe(MethodInfo targetMethodInfo) //{ // // } public static string createMainPointingToMethodInfo(MethodInfo targetMethodInfo) { var exeName = targetMethodInfo.Name; var cecilAssemblyBuilder = new CecilAssemblyBuilder(exeName, AssemblyKind.Console); TypeDefinition tdType = cecilAssemblyBuilder.addType("O2StandAloneExe", "Program"); MethodDefinition mdMain = cecilAssemblyBuilder.addMainMethod(tdType); cecilAssemblyBuilder.codeBlock_CallToMethod(mdMain, targetMethodInfo); var exeFileCreated = cecilAssemblyBuilder.Save(DI.config.O2TempDir); DI.log.info("Exe file created: {0}", exeFileCreated); return exeFileCreated; }
//public StandAloneExe(MethodInfo targetMethodInfo) //{ // // } public static string createMainPointingToMethodInfo(MethodInfo targetMethodInfo) { var exeName = targetMethodInfo.Name; var cecilAssemblyBuilder = new CecilAssemblyBuilder(exeName, ModuleKind.Console); TypeDefinition tdType = cecilAssemblyBuilder.addType("O2StandAloneExe", "Program"); MethodDefinition mdMain = cecilAssemblyBuilder.addMainMethod(tdType); cecilAssemblyBuilder.codeBlock_CallToMethod(mdMain, targetMethodInfo); var exeFileCreated = cecilAssemblyBuilder.Save(PublicDI.config.O2TempDir); PublicDI.log.info("Exe file created: {0}", exeFileCreated); return(exeFileCreated); }
public static string CreateAssemblyFromType(TypeDefinition typeToExtract, string targetFolder) { try { string fileName = typeToExtract.Name.Replace('<', '_').Replace('>', '_'); var cecilNewAssembly = new CecilAssemblyBuilder(fileName); cecilNewAssembly.addDummyType(); // todo: checkout why It looks like I need this in order for the type cloning to work cecilNewAssembly.addType(typeToExtract); return cecilNewAssembly.Save(targetFolder); } catch (Exception ex) { DI.log.ex(ex, "in CecilUtils.CreateAssemblyFromType"); return null; } }
public static string CreateAssemblyFromType(TypeDefinition typeToExtract, string targetFolder) { try { string fileName = typeToExtract.Name.Replace('<', '_').Replace('>', '_'); var cecilNewAssembly = new CecilAssemblyBuilder(fileName); cecilNewAssembly.addDummyType(); // todo: checkout why It looks like I need this in order for the type cloning to work cecilNewAssembly.addType(typeToExtract); return(cecilNewAssembly.Save(targetFolder)); } catch (Exception ex) { PublicDI.log.ex(ex, "in CecilUtils.CreateAssemblyFromType"); return(null); } }
public string save(string targetFolder) { return(cecilAssemblyBuilder.Save(targetFolder)); }
public void test_ExtractMethodFromAssembly() { AssemblyDefinition sourceCecilAssembly = CecilUtils.getAssembly(Assembly.GetExecutingAssembly().Location);//DI.config.hardCodedPathToO2UnitTestsDll); Assert.IsNotNull(sourceCecilAssembly, "sourceAsembly was null"); // create assembly using Cecil var cecilNewAssembly = new CecilAssemblyBuilder("ExtractedType"); cecilNewAssembly.addDummyType(); // todo: checkout why It looks like I need this in order for the type cloning to work // extract types from loaded CecilAssembly and add them to the new CecilAssembly foreach (string typeToExtract in typesToExtract) { TypeDefinition cecilTypeToExtract = CecilUtils.getType(sourceCecilAssembly, typeToExtract); Assert.IsNotNull(cecilTypeToExtract, "typeToExtract was null"); cecilNewAssembly.addType(cecilTypeToExtract); } // save cecilNewAssembly string cecilAssemblyFile = cecilNewAssembly.Save(DI.config.O2TempDir); Assert.IsTrue(File.Exists(cecilAssemblyFile), "file didn't exist"); // create assembly try to open it using Reflector Assembly reflectionAssembly = DI.reflection.getAssembly(cecilAssemblyFile); Assert.IsNotNull(reflectionAssembly, "assembly was null"); List<Type> reflectionTypes = DI.reflection.getTypes(reflectionAssembly); Assert.IsNotNull(reflectionTypes, "reflectionTypes was null"); Assert.That(reflectionTypes.Count() > 0, "reflectionTypes.Count == 0"); }
public void test_CloningAssembly() { AssemblyDefinition sourceAssembly = CecilUtils.getAssembly(currentAssembly); Assert.IsNotNull(sourceAssembly, "sourceAsembly was null"); var newAssembly = new CecilAssemblyBuilder("Cloned Assembly"); // foreach (var type in CecilUtils.getTypes(sourceAssembly)) // newAssembly.addType(type); string file = newAssembly.Save(DI.config.O2TempDir); Assert.IsTrue(File.Exists(file), "file didn't exist"); }