中国领先的工业平台

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

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

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

下载贤集网APP入驻自媒体

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

  1. using System;
  2. using Microsoft.CSharp;
  3. using System.CodeDom.Compiler;
  4. class Program
  5. {
  6. public static void Main()
  7. {
  8. // The C# code to execute
  9. string code = "using System; " +
  10. "using System.IO; " +
  11. "public class MyClass{ " +
  12. " public static void PrintConsole(string message){ " +
  13. " Console.WriteLine(message); " +
  14. " } " +
  15. "} ";
  16. // Compiler and CompilerParameters
  17. CSharpCodeProvider codeProvider = new CSharpCodeProvider();
  18. CompilerParameters compParameters = new CompilerParameters();
  19. // Compile the code
  20. CompilerResults res = codeProvider.CompileAssemblyFromSource(compParameters, code);
  21. // Create a new instance of the class 'MyClass'    // 有命名空间的,需要命名空间.类名
  22. object myClass = res.CompiledAssembly.CreateInstance("MyClass");
  23. // Call the method 'PrintConsole' with the parameter 'Hello World'
  24. // "Hello World" will be written in console
  25. myClass.GetType().GetMethod("PrintConsole").Invoke(myClass, new object[] {"Hello World" });
  26. Console.Read();
  27. }
  28. }
复制代码
最新回复

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

发布回复

为您推荐

热门交流