Mihai Cozma
Mihai Cozma

Reputation: 385

Referencing dynamically loaded assemblies in dynamically compiled code

I have a situation here in a .net project in which I'm compiling C# scripts into in-memory assemblies using CodeDomProvider.

In the CompilerParameters class that I use for compiling, I reference some assemblies by title (System.dll and one that is part of the project) and everything works so far. However some scripts are using some code (through an interface and polymorphism) that is found in an assembly that is precompiled but loaded dynamically from disk (like a plug-in). In this situation it won't work and the issue is that I don't know how to reference the dynamically loaded assembly when compiling the script dynamically.

If I put my script in a class library and from that class library I reference the plug-in assembly, it all works fine, but if I add the name of the plug-in assembly (the dll file) to the referenced assemblies in CompilerParameters and compile the script dynamically afterwards, the script is executed but the code that is located in the plug-in dll is not.

I hope it is clear what I am trying to do here, please help if you know anything about this because I'm struggling for some time now and nothing that I have tried works.

Thanks!

Edit: Here is the code for loading the plug-in dlls:

        DirectoryInfo di = new DirectoryInfo(@".\Plugins");
        FileInfo[] files = di.GetFiles("*.dll");
        foreach (FileInfo fi in files)
        {
            try
            {
                //load all dll files from the app pack directory
                Assembly asm = Assembly.LoadFrom(fi.FullName);
                Assemblies.Add(asm);
                foreach (Type type in asm.GetTypes())
                {
                    try
                    {
                        object instance = null;
                        instance = Activator.CreateInstance(type);

After this I store the instance in a dictionary for further usage. On the other side, Here is how I try to pass the references to the script that will be compiled:

        CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
        List<string> referenceAssemblies = new List<string>();
        referenceAssemblies.Add("System.dll");
        referenceAssemblies.Add("VFS.dll");
        foreach (Assembly asm in PluginManager.Instance.Assemblies)
        {
            referenceAssemblies.Add(asm.Location);
        }
        CompilerParameters compilerParameters = new CompilerParameters(referenceAssemblies.ToArray());

This works if I don't use any class that is part of a plug-in.

Upvotes: 3

Views: 2278

Answers (1)

Mihai Cozma
Mihai Cozma

Reputation: 385

I have discovered the problem and it is in a different area, this part works fine.

@Insipid - Thanks for your inspiring answer, because nothing was actually happening, and that was what made me look into another place and see the issue.

The code I pasted above works just fine and can be used if anyone would want to implement such mechanisms in the future.

Upvotes: 0

Related Questions