c#動(dòng)態(tài)編譯執(zhí)行對(duì)象方法示例 運(yùn)用映射機(jī)制創(chuàng)建對(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;三大命名空間
#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)文章
ajaxFileUpload插件,C#返回Json數(shù)據(jù)報(bào)錯(cuò)問題的解決方案
這篇文章主要介紹了ajaxFileUpload插件,C#返回Json數(shù)據(jù)報(bào)錯(cuò)的解決方案,需要的朋友可以參考下2017-12-12C#中Timer實(shí)現(xiàn)Tick使用精度的問題
這篇文章主要介紹了C#中Timer實(shí)現(xiàn)Tick使用精度的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08C#實(shí)現(xiàn)連接電子秤串口自動(dòng)稱重
這篇文章介紹了C#實(shí)現(xiàn)連接電子秤串口自動(dòng)稱重的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04WindowsForm實(shí)現(xiàn)TextBox占位符Placeholder提示功能
這篇文章主要介紹了WindowsForm實(shí)現(xiàn)TextBox占位符Placeholder提示,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07在Framework 4.0中:找出新增的方法與新增的類(一)
經(jīng)??吹接型瑢W(xué)在討論Framework 4 的新特性,新方法,于是想寫個(gè)程序找出framework4.0中新增的方法和類2013-05-05C#運(yùn)用FileInfo類實(shí)現(xiàn)拷貝文件的方法
這篇文章主要介紹了C#運(yùn)用FileInfo類實(shí)現(xiàn)拷貝文件的方法,需要的朋友可以參考下2014-07-07C#中參數(shù)個(gè)數(shù)可變的方法實(shí)例分析
這篇文章主要介紹了C#中參數(shù)個(gè)數(shù)可變的方法,以一個(gè)簡(jiǎn)單實(shí)例分析了C#中參數(shù)個(gè)數(shù)可變的方法,主要是使用params關(guān)鍵字來實(shí)現(xiàn)的,是C#編程中比較實(shí)用的技巧,需要的朋友可以參考下2014-11-11C#將Sql數(shù)據(jù)保存到Excel文件中的方法
這篇文章主要介紹了C#將Sql數(shù)據(jù)保存到Excel文件中的方法,文中的ExportExcel可起到將sql數(shù)據(jù)導(dǎo)出為Excel的作用,需要的朋友可以參考下2014-08-08Winform讓DataGridView左側(cè)顯示圖片
本文主要介紹在如何讓DataGridView左側(cè)顯示圖片,這里主要講解重寫DataGridView的OnRowPostPaint方法,需要的朋友可以參考下。2016-05-05