SoFunction
Updated on 2025-03-07

C# dynamically compiles and executes string samples

This article shares the specific code for Android nine-grid picture display for your reference. The specific content is as follows

using System;
using ;
using ;
 
class Program
{
 public static void Main()
 {
  // The C# code to execute
  string code = "using System; " +
      "using ; " +
      "public class MyClass{ " +
      " public static void PrintConsole(string message){ " +
      "  (message); " +
      " } " +
      "} ";
 
  // Compiler and CompilerParameters
  CSharpCodeProvider codeProvider = new CSharpCodeProvider();
  CompilerParameters compParameters = new CompilerParameters();
 
  // Compile the code
  CompilerResults res = (compParameters, code);
 
  // Create a new instance of the class 'MyClass'// If there is a namespace, a namespace is required.  object myClass = ("MyClass");
 
  // Call the method 'PrintConsole' with the parameter 'Hello World'
  // "Hello World" will be written in console
  ().GetMethod("PrintConsole").Invoke(myClass, new object[] {"Hello World" });
 
  ();
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.