C#動態(tài)編譯并執(zhí)行字符串樣例
更新時間:2020年11月17日 14:48:24 作者:王寶會
這篇文章主要為大家詳細(xì)介紹了C#動態(tài)編譯并執(zhí)行字符串樣例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#連接SQL?Sever數(shù)據(jù)庫與數(shù)據(jù)查詢實例之?dāng)?shù)據(jù)倉庫詳解
最近的工作遇到了連接查詢,特在此記錄,以免日后以往,下面這篇文章主要給大家介紹了關(guān)于C#連接SQL?Sever數(shù)據(jù)庫與數(shù)據(jù)查詢實例之?dāng)?shù)據(jù)倉庫的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

