I have an application referencing let's say : DLL 1 and DLL 2.
Within the application the end user can write some code for some customized functions.
Inside my application I compile their code using :
// User's code
string code = @" ** User's code ** ";
// Create the provider
CSharpCodeProvider provider = new CSharpCodeProvider();
// Create the parameters
CompilerParameters parameters = new CompilerParameters();
// Compile the user's code
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
Before the compilation, I would like to add some references that are already used withing my application which are DLL 1 and DLL 2 (in order to let the user use some of my functions).
I tried to use GetExecutingAssembly, but it returns the main application assembly and I can't find how to get DLL 1 and DLL 2.
Assembly.GetExecutingAssembly
I don't know the path of the DLL's so the following instruction won't work :
parameters.ReferencedAssemblies.Add("C:\DLL1.dll");
How can I solve that?
You can get the assembly location from a type defined within that assembly. So in your CompilerParameters you can reference them like this:
new CompilerParameters
{
ReferencedAssemblies =
{
typeof(MyDll1.Type1).Assembly.Location,
typeof(MyDll2.Type2).Assembly.Location
//, etc
}
//, etc
}