0

I am executing C# code dynamically by using "CSharpCodeProvider" then compile the code and invoke the method. I need to include a certain DLL files to the compiler in order to call classes from that DLL. I am using the method:

CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add(the path of the DLL File);

But when invoking the method, I get an error :

Could not load file or assembly or one of its dependencies. The system cannot find the file specified.

But when I just include the DLL from Project>References>Add Reference , the code invoked withed any error. Can anyone tell me how to add the reference dynamically on run-time to avoid that error?

Here is the code :

CSharpCodeProvider Code = new CSharpCodeProvider();
ICodeCompiler icc = Code.CreateCompiler();
CompilerParameters cp = new CompilerParameters();

cp.ReferencedAssemblies.Add("system.dll");
cp.ReferencedAssemblies.Add("system.data.dll");
cp.ReferencedAssemblies.Add("system.windows.forms.dll");
cp.ReferencedAssemblies.Add(@"D:\AnalogClockControl.dll");
cp.CompilerOptions = "/t:library";
cp.GenerateInMemory = true;
StringBuilder sb = new StringBuilder("");
sb.Append("using System;\n");
sb.Append("using System.Data;\n");
sb.Append("using System.Windows.Forms;\n");
sb.Append("namespace CSCodeEvaler{ \n");
sb.Append("public class CSCodeEvaler{ \n");
sb.Append("public void test(){AnalogClockControl.AnalogClock _AnalogClock= new AnalogClockControl.AnalogClock();}\n");
sb.Append("} \n");
sb.Append("}\n");
CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
if (cr.Errors.Count > 0)
{
   return  ; //"ERROR: " + cr.Errors[0].ErrorText
}
System.Reflection.Assembly a = cr.CompiledAssembly;
object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");
Type t = o.GetType();
MethodInfo mi = t.GetMethod("test"); 
object j = mi.Invoke(o, new object[] {}); // Here where I get the exception
3
  • Maybe you're writing the wrong path. Could you show us your code? Commented Dec 23, 2012 at 4:18
  • My guess would be there's a dependency problem. Try checking the "Assembly loader exception" for more info. Commented Dec 23, 2012 at 4:38
  • Thanks guys for your response. Eve: the path is correct, otherwise the error won't be gone if I added the DLL manually. I will post the code in a minute .. dbaseman: Can you please tell me from where I can find the Assembly loader exception ? Commented Dec 23, 2012 at 5:00

1 Answer 1

1

Maybe you need to add a using statament for AnalogClockControl namespace

sb.Append("using AnalogClockControl;\n"); // whatever the namespace is

EDIT: never mind, that should not compile at all if it was required.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding this line wont do anything because when I am defining the instance, I am including the full class name AnalogClockControl.AnalogClock _AnalogClock= new AnalogClockControl.AnalogClock(); Anyway, I tried to add this line and I still have the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.