예제 #1
0
        //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 CreateTestExe createBasicHelloWorldExe(bool bWithPressEnter)
 {
     cecilAssemblyBuilder = new CecilAssemblyBuilder(exeName, ModuleKind.Console);
     TypeDefinition tdType = cecilAssemblyBuilder.addType("BasicTest", "Program");
     MethodDefinition mdMain = cecilAssemblyBuilder.addMainMethod(tdType);
     cecilAssemblyBuilder.codeBlock_ConsoleWriteLine(mdMain,
                                                     String.Format(
                                                         "Hello World " + Environment.NewLine +
                                                         Environment.NewLine +
                                                         "(Created at {0})" + Environment.NewLine +
                                                         Environment.NewLine +
                                                         "(by {1})", DateTime.Now, Environment.UserName));
     if (bWithPressEnter)
         cecilAssemblyBuilder.codeBlock_PressEnter(mdMain);
     return this;
 }
예제 #4
0
        public CreateTestExe createBasicHelloWorldExe(bool bWithPressEnter)
        {
            cecilAssemblyBuilder = new CecilAssemblyBuilder(exeName, ModuleKind.Console);
            TypeDefinition   tdType = cecilAssemblyBuilder.addType("BasicTest", "Program");
            MethodDefinition mdMain = cecilAssemblyBuilder.addMainMethod(tdType);

            cecilAssemblyBuilder.codeBlock_ConsoleWriteLine(mdMain,
                                                            String.Format(
                                                                "Hello World " + Environment.NewLine +
                                                                Environment.NewLine +
                                                                "(Created at {0})" + Environment.NewLine +
                                                                Environment.NewLine +
                                                                "(by {1})", DateTime.Now, Environment.UserName));
            if (bWithPressEnter)
            {
                cecilAssemblyBuilder.codeBlock_PressEnter(mdMain);
            }
            return(this);
        }
예제 #5
0
        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;
            }
        }
예제 #6
0
        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);
            }
        }
예제 #7
0
        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");
        }