Hi I got compiling C# at runtime for my modding framework. Each class is represented as a string in a string array that is loaded from a text asset. I need to only allow certain types to be compiled. I got a CModdable class and I want to prevent compiling scripts that don’t inherit this or parent does not inherit it and so on. So basically I need to sandbox loading in code or people can’t change code that there are not suppose to.
static Assembly CompileAssembly(string[] s)
{
var provider = new CSharpCodeProvider();
var param = new CompilerParameters();
//Add All of the Assembly refrences
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
param.ReferencedAssemblies.Add(assembly.Location);
//Generate a dll in memory
param.GenerateExecutable = false;
param.GenerateInMemory = true;
//Compile
var result = provider.CompileAssemblyFromSource(param,s);
return result.CompiledAssembly;
}