C#動(dòng)態(tài)編譯并執(zhí)行字符串樣例
本文實(shí)例為大家分享了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(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#操作mysql數(shù)據(jù)庫(kù)的代碼實(shí)例
這篇文章為大家提供了一個(gè)C#操作mysql數(shù)據(jù)庫(kù)的實(shí)例,大家可以參考使用2013-11-11C#實(shí)現(xiàn)word和pdf格式互轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)C#實(shí)現(xiàn)word和pdf格式互轉(zhuǎn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10Unity UGUI的Scrollbar滾動(dòng)條組件使用詳解
這篇文章主要介紹了Unity UGUI的Scrollbar(滾動(dòng)條)組件的介紹及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07C#實(shí)現(xiàn)打開(kāi)指定目錄和指定文件的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)打開(kāi)指定目錄、打開(kāi)指定目錄且選中指定文件、打開(kāi)指定文件,感興趣的小伙伴可以嘗試一下2022-06-06Unity實(shí)現(xiàn)簡(jiǎn)單手勢(shì)識(shí)別
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單手勢(shì)識(shí)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07C#連接SQL?Sever數(shù)據(jù)庫(kù)與數(shù)據(jù)查詢實(shí)例之?dāng)?shù)據(jù)倉(cāng)庫(kù)詳解
最近的工作遇到了連接查詢,特在此記錄,以免日后以往,下面這篇文章主要給大家介紹了關(guān)于C#連接SQL?Sever數(shù)據(jù)庫(kù)與數(shù)據(jù)查詢實(shí)例之?dāng)?shù)據(jù)倉(cāng)庫(kù)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06