0

I'm trying to dynamically compile a .cs file. This .cs file has some custom .dll references that I also need to reference dynamically. All works good, but when I try to get all the properties of the .cs file by reflection, I obtain

{"Could not load file or assembly 'CUSTOMDLL, Version=3.2.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"CUSTOMDLL, Version=3.2.0.0, Culture=neutral, PublicKeyToken=null"}

I tried to import those dll through

Assembly.LoadFile(dllPath)

but seems not working. My code is currently:

             Dictionary<string, string> providerOptions = new Dictionary<string, string>
            {
                {"CompilerVersion", "v4.0"}
            };
            CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);

            CompilerParameters compilerParams = new CompilerParameters
            {
                GenerateInMemory = true,
                GenerateExecutable = false,
                ReferencedAssemblies = { "System.dll",
                    "System.Data.dll",
                    "mscorlib.dll",
                }
            };

            string referencesPath = PATH CONTAINING ALL .DLL NEEDED TO COMPILE;
            foreach(string filePath in Directory.GetFiles(referencesPath))
            {
                compilerParams.ReferencedAssemblies.Add(filePath);
                Assembly.LoadFile(filePath);
            }

            CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, SOURCE CODE OF .CS CLASS);
            Assembly assembly = results.CompiledAssembly;

            assembly.GetTypes(); // This cannot resolve all types

There is any way to "import" those dll in order to get the GetTypes() working?

Ferra
  • 91
  • 9

0 Answers0