Reputation:
I have created a function to compile C# code in a string in memory and use it at runtime. It works very well.
Next I created a small class library (lets call it mynew.dll) and placed it at c:\mylibraries\mynew.dll.
In my code I can add a referenced assembly. I do it like this:
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};
parameters.ReferencedAssemblies.Add(@"c:\mylibraries\mynew.dll");
CompilerResults results = provider.CompileAssemblyFromSource(parameters, mycode);
Now, in my C# code-string (mycode) is a function called "Execute". When I declare the namespace and class from the mynew.dll in this function I get this error:
Could not load file or assembly 'MyNew, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
When I copy the dll in the bin/debug folder of the main application is suddenly works, but I don't want this. I want the code, that is compiled at runtime, to use it, not the main application.
This I did before posting here:
Is there a way to fix this?
Upvotes: 0
Views: 978
Reputation: 424
Use Directory.GetFiles to find your assembly in the file system, use System.Reflection.Assembly.LoadFile to load the assembly, and then find the types required. Use interface that will be implemented by your concrete classes to avoid having to know the concrete types.
This thread should help: Finding objects that implement interface from loaded assembly -how to compare types?
Upvotes: -1