当前位置: 移动技术网 > IT编程>开发语言>c# > c#动态编译执行对象方法示例 运用映射机制创建对象

c#动态编译执行对象方法示例 运用映射机制创建对象

2019年07月18日  | 移动技术网IT编程  | 我要评论
c#是一种编译型的语言,程序执行,首先要经过编译器编译,如何让c#像一种脚本一样,在要执行的时候,进行编译,这里,我们可以用microsoft.csharp空间下的csha

c#是一种编译型的语言,程序执行,首先要经过编译器编译,如何让c#像一种脚本一样,在要执行的时候,进行编译,这里,我们可以用microsoft.csharp空间下的csharpcodeprovider提供类,来达到动态编译的效果。在这里,我新建一个控制台程序,在program.cs类里引用using system.codedom.compiler;
using system.reflection;using microsoft.csharp;三大命名空间

复制代码 代码如下:

#region using directiry
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.io;
using system.codedom;
using system.codedom.compiler;
using system.reflection;
using microsoft.csharp;
#endregion
/*==============================================================================
 *
 * author:lichaoqiang@163.com
 * link:http://my.oschina.net/lichaoqiang
 *
 *
 * ============================================================================*/
namespace codedom
{
    class program
    {
        #region 主程序入口
        /// <summary>
        ///主程序入口
        /// </summary>
        /// <param name="args"></param>
        static void main(string[] args)
        {
            //1>实例化c#代码服务提供对象
            csharpcodeprovider provider = new csharpcodeprovider();
            //2>声明编译器参数
            compilerparameters parameters = new compilerparameters();
            parameters.generateexecutable = false;
            parameters.generateinmemory = true;
            try
            {
                //3>动态编译
                compilerresults result = provider.compileassemblyfromsource(parameters, buildcsharpcode());
                if (result.errors.count > 0)
                {
                    console.write("编译出错!");
                }
                //4>如果编译没有出错,此刻已经生成动态程序集lcq.lcqclass
                //5>开始玩c#映射
                assembly assembly = result.compiledassembly;
                object obj = assembly.createinstance("lcq.lcqclass");
                type type = assembly.gettype("lcq.lcqclass");
                //6>获取对象方法
                methodinfo method = type.getmethod("sum");
                object[] objparameters = new object[2] { 1, 5 };
                int iresult = convert.toint32(method.invoke(obj, objparameters));//唤醒对象,执行行为
                console.write(iresult);
                console.read();
            }
            catch (system.notimplementedexception ex)
            {
                console.write(ex.message);
            }
            catch (system.argumentexception ex)
            {
                console.write(ex.message);
            }
            catch (exception ex)
            {
                console.write(ex.message);
            }
        }
        #endregion

        #region 生成代码块
        /// <summary>
        /// 生成代码块
        /// </summary>
        /// <returns></returns>
        private static string buildcsharpcode()
        {
            string filename = appdomain.currentdomain.basedirectory.replace("debug", string.empty).replace("release", string.empty) + "codefile.cs";
            string strcodedom = file.readalltext(filename);
            return strcodedom;
        }
        #endregion
    }
}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网