中国领先的工业平台

返回贤集网 返回微头条
贤集网技术微头条APP获取

C#动态编译并执行字符串样例

 山东大明消毒科技有限公司

下载贤集网APP入驻自媒体

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下using System;using Microsoft.CSharp;using System.CodeDom.Compiler;class Program{public static void Main(){// The C# code to executestring code = "using System; " +"using System.IO; " +"public class MyClass{ " +" public static void PrintConsole(string message){ " +" Console.WriteLine(message); " +" } " +"} ";// Compiler and CompilerParametersCSharpCodeProvider codeProvider = new CSharpCodeProvider();CompilerParameters compParameters = new CompilerParameters();// Compile the codeCompilerResults 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 consolemyClass.GetType().GetMethod("PrintConsole").Invoke(myClass, new object[] {"Hello World" });Console.Read();}}复制代码					
最新回复

还没有人回复哦,抢沙发吧~

发布回复

为您推荐

热门交流