I found example with runtime function creating.
public static MethodInfo CreateFunction(string function)
{
string code = @"
using System;
namespace UserFunctions
{
public class BinaryFunction
{
public static double Function(double x, double y)
{
return func_xy;
}
}
}
";
string finalCode = code.Replace("func_xy", function);
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerResults results = provider.CompileAssemblyFromSource(new CompilerParameters(), finalCode);
Type binaryFunction = results.CompiledAssembly.GetType("UserFunctions.BinaryFunction");
return binaryFunction.GetMethod("Function");
}
Main:
static void Main(string[] args)
{
MethodInfo function = CreateFunction("x + 2 * y");
object result = function.Invoke(null, new object[] { 2, 3 });
Console.WriteLine(result);
}
The main question is how to create property in runtime and pass value to it in Main
?
public static PropertyInfo CreateProperty()
{
string code=@"
private string name;
public string Name {
get{
return this.name;
}
set{
this.name=value;
}
}
";
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerResults results = provider.CompileAssemblyFromSource(new CompilerParameters(),code);
///
}
Aucun commentaire:
Enregistrer un commentaire