if you want to create coding dynamically and compile dynamically you could do that like in my math program, to dynamically add some functions without need to recompile the project..
i have text file with body of program:
using System;
// file curve.txt
namespace UserFunction
{
public class BinaryFunction
{
public static double MyFunction(double x, double param)
{
return param == 0 ? MyFunction0(x, 0.3) : MyFunction1(x);
}
private static double MyFunction0(double x, double param)
{
return (1 - param) * x + param * (Math.Pow(x, 3));
}
private static double MyFunction1(double x)
{
return x * 2;
}
}
};
And in project C#, i dynamically compile the program and create dll. After i can call the method from dll created.
// in project C#
private Func<double, double, double> betterFunction;
private MethodInfo CreateAndCompileFunction(string filename)
{
FileInfo fi = new FileInfo(@".\folder\CurveAssembly.dll");
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.OutputAssembly = @".\folder\CurveAssembly.dll";
CompilerResults results = provider.CompileAssemblyFromFile(compilerparams, filename);
if (results.Errors.Count > 0)
{
// Display compilation errors.
Console.WriteLine("Errors building {0} into {1}",
fi.FullName, results.PathToAssembly);
foreach (CompilerError ce in results.Errors)
{
Console.WriteLine(" {0}", ce.ToString());
}
}
else
{
// Display a successful compilation message.
Console.WriteLine("Source {0} built into {1} successfully.",
fi.FullName, results.PathToAssembly);
}
Type binaryFunction = results.CompiledAssembly.GetType("UserFunction.BinaryFunction");
return binaryFunction.GetMethod("MyFunction");
}
public void compileFunction(string curvename)
{
MethodInfo function = CreateAndCompileFunction(@".\folder\curve.txt");
betterFunction = (Func<double, double, double>)Delegate.CreateDelegate(typeof(Func<double, double, double>), function);
}
//calling the method inside the dll (betterfunction)
public double MyFunction(double x, double param)
{
return betterFunction(x, param);
}