• About use Runtime Method Invoke and Runtime Source Code Compliation


    1.

    using System.Reflection;
    using System.Security.Permissions; 
    namespace SystemReflectionEx
    {

    Class Test

    {
    string ModuleName = "TestAssembly.dll";
    string TypeName = "TestClass";
    string MethodName = "TestMethod";

    Module [] myModules = myAssembly.GetModules();
    foreach (Module Mo in myModules) 
        {
         if (Mo.Name == ModuleName) 
             {
             Type[] myTypes = Mo.GetTypes();
             foreach (Type Ty in myTypes)
                 {
                if (Ty.Name == TypeName) 
                    {
                    MethodInfo[] myMethodInfo = Ty.GetMethods(flags);
                    foreach(MethodInfo Mi in myMethodInfo)
                        {
                        if (Mi.Name == MethodName) 
                            {
                            Object obj = Activator.CreateInstance(Ty);
                            Object response = Mi.Invoke(obj, null);
                            }
                        }
                    }
                }
            }
        }
    }
    }

    2.
    using System;
    using System.IO;
    using System.Globalization;
    using System.CodeDom.Compiler;
    using System.Text;
    using Microsoft.CSharp;
    using Microsoft.VisualBasic;
    
    namespace CodeProviders
    {
    	class CompileSample
    	{
            [STAThread]
            static void Main(string[] args)
            {
                if (args.Length > 0)
                {
                    //  First parameter is the source file name.
                    if (File.Exists(args[0]))
                    {
                        CompileExecutable(args[0]);
                    }
                    else 
                    {
                        Console.WriteLine("Input source file not found - {0}",
                            args[0]);
                    }
                }
                else 
                {
                    Console.WriteLine("Input source file not specified on command line!");
                }
            }
    
            public static bool CompileExecutable(String sourceName)
            {
                FileInfo sourceFile = new FileInfo(sourceName);
                CodeDomProvider provider = null;
                bool compileOk = false;
    
                // Select the code provider based on the input file extension.
                if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
                {
                    provider = CodeDomProvider.CreateProvider("CSharp");
                }
                else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
                {
                    provider = CodeDomProvider.CreateProvider("VisualBasic");
                }
                else 
                {
                    Console.WriteLine("Source file must have a .cs or .vb extension");
                }
    
                if (provider != null)
                {
    
                    // Format the executable file name.
                    // Build the output assembly path using the current directory
                    // and <source>_cs.exe or <source>_vb.exe.
    
                    String exeName = String.Format(@"{0}{1}.exe", 
                        System.Environment.CurrentDirectory, 
                        sourceFile.Name.Replace(".", "_"));
    
                    CompilerParameters cp = new CompilerParameters();
    
                    // Generate an executable instead of 
                    // a class library.
                    cp.GenerateExecutable = true;
    
                    // Specify the assembly file name to generate.
                    cp.OutputAssembly = exeName;
    
                    // Save the assembly as a physical file.
                    cp.GenerateInMemory = false;
    
                    // Set whether to treat all warnings as errors.
                    cp.TreatWarningsAsErrors = false;
    
                    // Invoke compilation of the source file.
                    CompilerResults cr = provider.CompileAssemblyFromFile(cp, 
                        sourceName);
    
                    if(cr.Errors.Count > 0)
                    {
                        // Display compilation errors.
                        Console.WriteLine("Errors building {0} into {1}",  
                            sourceName, cr.PathToAssembly);
                        foreach(CompilerError ce in cr.Errors)
                        {
                            Console.WriteLine("  {0}", ce.ToString());
                            Console.WriteLine();
                        }
                    }
                    else
                    {
                        // Display a successful compilation message.
                        Console.WriteLine("Source {0} built into {1} successfully.",
                            sourceName, cr.PathToAssembly);
                    }
    
                    // Return the results of the compilation.
                    if (cr.Errors.Count > 0)
                    {
                        compileOk = false;
                    }
                    else 
                    {
                        compileOk = true;
                    }
                }
                return compileOk;
            }
    	}
    }
    Love it, and you live without it
  • 相关阅读:
    微信小程序实现运动步数排行(可删除)
    一个文艺的在线生成漂亮的二维码工具网站
    微信小程序常见的UI框架/组件库总结
    小程序踩坑记- tabBar.list[3].selectedIconPath 大小超过 40kb
    推荐一款便捷的在线图片处理工具
    如何在本地运行查看github上的开源项目
    微信小程序实现运动步数排行(可删除)
    从零开始学ios开发(三):第一个有交互的app
    从零开始学ios开发(二):Hello World!来啦!
    从零开始学ios开发(一):准备起航
  • 原文地址:https://www.cnblogs.com/tomclock/p/6906773.html
Copyright © 2020-2023  润新知