Metadata file "Domain.dll" not found when using CSharpCodeProvider referencing project

I have a solution with two assemblies, one of which is called Domain and contains the Book class and the Author class.

I want to dynamically create a class that inherits from the Book class. Here is my code:

public Book CreateBookProxy(Book book) { CSharpCodeProvider cscp = new CSharpCodeProvider(new Dictionary<String, String> { { "CompilerVersion", "v3.5" } }); var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll", "Domain.dll" }, "Proxies.dll", false); parameters.GenerateExecutable = false; var compileUnit = new CodeCompileUnit(); var ns = new CodeNamespace("Proxies"); compileUnit.Namespaces.Add(ns); ns.Imports.Add(new CodeNamespaceImport("System")); ns.Imports.Add(new CodeNamespaceImport("Domain")); var classType = new CodeTypeDeclaration("BookProxy"); classType.Attributes = MemberAttributes.Public; classType.BaseTypes.Add(new CodeTypeReference(typeof(Book))); ns.Types.Add(classType); var results = cscp.CompileAssemblyFromDom(parameters, compileUnit); List<string> errors = new List<string>(); errors.AddRange(results.Errors.Cast<CompilerError>().Select(e => e.ErrorText)); return Activator.CreateInstance(Type.GetType("Proxies.BookProxy, Proxies")) as Book; } 

However, I get the following error:

Could not find metadata file 'Domain.dll'

Domain.dll is referenced in my startup project, so it exists in the bin folder at runtime.

Interesting Assembly.Load ("Domain.dll"); throws a FileNotFoundException

How can I solve this problem?

+5
source share
1 answer

I would suggest explicitly specifying the location of Domain.dll as follows:

 parameters.ReferencedAssemblies.Add(typeof(<TYPE FROM DOMAIN.DLL>).Assembly.Location); 
+10
source

Source: https://habr.com/ru/post/914663/


All Articles