欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#動(dòng)態(tài)編譯執(zhí)行對(duì)象方法示例 運(yùn)用映射機(jī)制創(chuàng)建對(duì)象

 更新時(shí)間:2014年01月16日 11:14:10   作者:  
本示例核心技術(shù)是運(yùn)用.NET動(dòng)態(tài)編譯技術(shù)+.NET映射技術(shù),把一個(gè)代碼塊中的代碼,動(dòng)態(tài)編譯成程序集后,在運(yùn)用映射機(jī)制,創(chuàng)建對(duì)象示例,調(diào)用對(duì)象方法

C#是一種編譯型的語言,程序執(zhí)行,首先要經(jīng)過編譯器編譯,如何讓C#像一種腳本一樣,在要執(zhí)行的時(shí)候,進(jìn)行編譯,這里,我們可以用Microsoft.CSharp空間下的CSharpCodeProvider提供類,來達(dá)到動(dòng)態(tài)編譯的效果。在這里,我新建一個(gè)控制臺(tái)程序,在Program.cs類里引用using System.CodeDom.Compiler;
using System.Reflection;using Microsoft.CSharp;三大命名空間

復(fù)制代碼 代碼如下:

#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>實(shí)例化C#代碼服務(wù)提供對(duì)象
            CSharpCodeProvider provider = new CSharpCodeProvider();
            //2>聲明編譯器參數(shù)
            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = false;
            parameters.GenerateInMemory = true;
            try
            {
                //3>動(dòng)態(tài)編譯
                CompilerResults result = provider.CompileAssemblyFromSource(parameters, BuildCSharpCode());
                if (result.Errors.Count > 0)
                {
                    Console.Write("編譯出錯(cuò)!");
                }
                //4>如果編譯沒有出錯(cuò),此刻已經(jīng)生成動(dòng)態(tài)程序集LCQ.LCQClass
                //5>開始玩C#映射
                Assembly assembly = result.CompiledAssembly;
                object obj = assembly.CreateInstance("LCQ.LCQClass");
                Type type = assembly.GetType("LCQ.LCQClass");
                //6>獲取對(duì)象方法
                MethodInfo method = type.GetMethod("Sum");
                object[] objParameters = new object[2] { 1, 5 };
                int iResult = Convert.ToInt32(method.Invoke(obj, objParameters));//喚醒對(duì)象,執(zhí)行行為
                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
    }
}

相關(guān)文章

最新評(píng)論