I am trying to read and compile an external file (it's in a string constant for now)
I can use basic primitives in the external code but I can't seem to figure out how to pass it a class type without it getting angry -heres what I have (excluding the using lines)
class myClass
{
public int x;
public myClass(int n)
{
x = n;
}
}
class Program
{
static void Main(string[] args)
{
string source =
@"
namespace ConsoleApplication1
{
public class Bar
{
public int getNumber(myClass c)
{
return c.x;
}
}
}";
Dictionary<string, string> providerOptions = new Dictionary<string, string>
{
{"CompilerVersion", "v3.5"}
};
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerParameters compilerParams = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false
};
CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);
if (results.Errors.Count != 0)
throw new Exception("Failed");
object o = results.CompiledAssembly.CreateInstance("ConsoleApplication1.Bar");
MethodInfo mi = o.GetType().GetMethod("getNumber");
object[] param = new object[1];
myClass c = new myClass(5);
param[0] = c;
int myInt = (int)mi.Invoke(o, param);
Console.Write(myInt);
Console.ReadLine();
}
}
Help would be appreciated