0

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:

  • Search Stackoverflow
  • Used Google
  • Tried to compile the dll to the disk and not in memory. This works... Problem now is that the dll is locked in IIS and I cannot delete it without restarting the IIS server; no option since the code can change any moment and I don't want to restart the server when people are on the website.

Is there a way to fix this?

  • 1
    "The code can change at any moment" means an in-memory assembly won't work for you either, unless you fancy memory leaks and a lot of juggling with loading the "right" assembly. Once loaded, assemblies cannot be unloaded (regardless of their source); only unloading the AppDomain will do that. Is there any particular reason you want to compile assemblies and not, say, generate code dynamically using expression trees? – Jeroen Mostert Dec 16 '16 at 11:17
  • In the dll is a lot of logic. Think here about database handling or a library for e-mail handling. The compiled code is not always the same and will sometimes need the database handling and some other times the e-mail handling. Maybe it is a little bit hard to explain. I just want to reference dll's at runtime that are not in the bin directory of the main application –  Dec 16 '16 at 11:22
  • @Aly El-Haddad This is example data. Of course I have my strings escaped where needed. But I changed it for you –  Dec 16 '16 at 11:26
  • @dropshot32: alright, but then why not simply compile those assemblies outside your application and load them from a well-known directory as plugins with `Assembly.LoadFile` and the full name? Or, if your site is ASP.NET, drop the source as .cs files in App_Code and have the ASP.NET engine take care of compilation? (Yes, it can do that, even though these days precompiled code is a lot more popular.) – Jeroen Mostert Dec 16 '16 at 11:29

1 Answers1

-1

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?

Community
  • 1
  • 1
J. Tuc
  • 424
  • 2
  • 10