I'll try to define as mush as possible my problem and forget nothing.
For my project, which use a webRequest, I would like to compile dynamically my webRequest. For this I used the CodeDomProvider in a Private Assembly and a public MethodInfo who gives me back a " method" that I can use in my main program. So the main problem is that in my CompileCode, my MethodInfo method = type.getMethod(functionname); gives me a NullReferenceException error. I know it's because my type.getMethod(functionname) can't work on a type which is null. I tried to modify the fact that my object instance and Type type are not null, but I can't give them values because of their gender and I get stuck in the fact that they stay null and gives me no values...
I also saw that lot of people used Linq, but as I am compiling a whole .cs file, I can't write it all like this with the @"using System.Linq;";
So here are the partial code were the problem is :
Thank you
namespace testCodeCompiler
{
public class CodeCompiler
{
public CodeCompiler()
{
}
public MethodInfo CompileCode(string code, string namespacename, string classname,string functionname, bool isstatic, params object[] args)
{
Assembly asm = BuildAssembly(code);
object instance = null;
Type type = null;
if (isstatic)
{
type = asm.GetType(namespacename + "." + classname);
}
else
{
instance = asm.CreateInstance(namespacename + "." + classname);
type = instance.GetType();
}
MethodInfo method = type.GetMethod(functionname); // here is the error
return method;
}
private Assembly BuildAssembly(string code)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = true;
compilerparams.ReferencedAssemblies.Add("System.dll");
compilerparams.ReferencedAssemblies.Add("System.Xml.dll");
System.Reflection.Assembly currentAssembly = System.Reflection.Assembly.GetExecutingAssembly();
compilerparams.ReferencedAssemblies.Add(currentAssembly.Location);
CompilerResults results = provider.CompileAssemblyFromSource(compilerparams, code);
if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in results.Errors )
{
errors.AppendFormat("Line {0},{1}\t: {2}\n", error.Line, error.Column, error.ErrorText);
}
throw new Exception(errors.ToString());
}
else
{
return results.CompiledAssembly;
}
}
}
And a little part of the Main() :
static void Main(string[] args)
{
MethodInfo method;
[// Little bit of code ]
StreamReader sr = new StreamReader(@"c:\pathtothefileIwant\File.cs", System.Text.Encoding.Default);
string file = sr.ReadToEnd();
sr.Close();
CodeCompiler cc = new CodeCompiler();
object[] arguments = { popup }; // here are the args which are in another class
[...little bit of code...]
method = cc.CompileCode(file, "testNamespace", "Class1", "webRequest", true, arguments);
List<Test> li = (List<Test>)method.Invoke(null, arguments); // Here I invoke the method to put it in a List<Compte> I made before.
}