C#动态编译并执行字符串样例
时间:2020-12-30 13:04:04|栏目:.NET代码|点击: 次
本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下
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();
}
}
上一篇:ASP.NET开发者使用jQuery应该了解的几件事情
栏 目:.NET代码
下一篇:c#源码的执行过程详解
本文标题:C#动态编译并执行字符串样例
本文地址:http://www.codeinn.net/misctech/38227.html






