最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C#动态编译并执行字符串样例
时间:2017-07-07 编辑:简简单单 来源:一聚教程网
本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下
代码如下 | 复制代码 |
usingSystem; usingMicrosoft.CSharp; usingSystem.CodeDom.Compiler;
classProgram { publicstaticvoidMain() { // The C# code to execute stringcode ="using System; "+ "using System.IO; "+ "public class MyClass{ "+ " public static void PrintConsole(string message){ "+ " Console.WriteLine(message); "+ " } "+ "} ";
// Compiler and CompilerParameters CSharpCodeProvider codeProvider =newCSharpCodeProvider(); CompilerParameters compParameters =newCompilerParameters();
// Compile the code CompilerResults res = codeProvider.CompileAssemblyFromSource(compParameters, code);
// Create a new instance of the class 'MyClass' // 有命名空间的,需要命名空间.类名 objectmyClass = 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,newobject[] {"Hello World"});
Console.Read(); } } |
-
上一个: C语言开发之归并排序详解及实例
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10