C#動態(tài)編譯并執(zhí)行字符串樣例
更新時間:2020年11月17日 14:48:24 作者:王寶會
這篇文章主要為大家詳細介紹了C#動態(tài)編譯并執(zhí)行字符串樣例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下
using System;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
class Program
{
public static void Main()
{
// The C# code to execute
string code = "using System; " +
"using System.IO; " +
"public class MyClass{ " +
" public static void PrintConsole(string message){ " +
" Console.WriteLine(message); " +
" } " +
"} ";
// Compiler and CompilerParameters
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters compParameters = new CompilerParameters();
// Compile the code
CompilerResults res = codeProvider.CompileAssemblyFromSource(compParameters, code);
// Create a new instance of the class 'MyClass' // 有命名空間的,需要命名空間.類名
object myClass = res.CompiledAssembly.CreateInstance("MyClass");
// Call the method 'PrintConsole' with the parameter 'Hello World'
// "Hello World" will be written in console
myClass.GetType().GetMethod("PrintConsole").Invoke(myClass, new object[] {"Hello World" });
Console.Read();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#中TreeView實現(xiàn)適合兩級節(jié)點的選中節(jié)點方法
這篇文章主要介紹了C#中TreeView實現(xiàn)適合兩級節(jié)點的選中節(jié)點方法,實例分析了C#中TreeView節(jié)點操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
DataTables List互相轉(zhuǎn)換的實現(xiàn)類示例
這篇文章主要介紹了將DataTable轉(zhuǎn)換為List,將List轉(zhuǎn)換為DataTable的實現(xiàn)類實例方法,大家參考使用吧2013-11-11
C#三種判斷數(shù)據(jù)庫中取出的字段值是否為空(NULL) 的方法
最近操作數(shù)據(jù)庫,需要判斷返回的字段值是否為空,在網(wǎng)上收集了3種方法供大家參考2013-04-04
C#結(jié)合JavaScript對Web控件進行數(shù)據(jù)輸入驗證的實現(xiàn)方法
在 Web 應用的錄入界面,數(shù)據(jù)驗證是一項重要的實現(xiàn)功能,數(shù)據(jù)驗證是指確認 Web 控件輸入或選擇的數(shù)據(jù),本文我們將介紹如何通過C# 后端及JavaScript 前端對 Web 控件進行數(shù)據(jù)輸入有效性的驗證,感興趣的朋友可以參考一下2024-05-05

