元数据文件“Domain.dll”找不到使用CSharpCodeProvider引用项目时 [英] Metadata file 'Domain.dll' could not be found when using CSharpCodeProvider referencing project

查看:296
本文介绍了元数据文件“Domain.dll”找不到使用CSharpCodeProvider引用项目时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个组件,其中一个被称为域,并包含一个Book类和作者类的解决方案。

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

我要动态地创建一个类从图书类继承。这里是我的代码:

I want to dynamically create a class which inherits from the Book class. Here's 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'm getting the following error:

元数据文件'Domain.dll找不到

Metadata file 'Domain.dll' could not be found

域名。.DLL在我启动了项目,因此在运行时的bin文件夹中存在引用

Domain.dll is referenced in my start up project so it exists in the bin folder at run time.

有趣的Assembly.Load(Domain.dll);抛出一个FileNotFoundException异常

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

我怎样才能解决这个问题?

How can I resolve this issue?

推荐答案

我建议明确指定 Domain.dll 的位置如下:

I would suggest explicitly specify the location of Domain.dll as following:

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

这篇关于元数据文件“Domain.dll”找不到使用CSharpCodeProvider引用项目时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆