简体   繁体   English

为移入类库的代码添加引用

[英]Adding references for code moved into a class library

From a solution, which I made in Visual Studio 2013, I want to transfer a method to a DLL (class library project). 从我在Visual Studio 2013中制作的解决方案中,我想将方法​​转移到DLL(类库项目)中。

When I paste the method in the DLL project, it doesn't recognize parts of the code and it's showing this error`: 当我将方法粘贴到DLL项目中时,它无法识别部分代码,并显示此错误`:

"are you missing a using directive or an assembly reference?" “您是否缺少using指令或程序集引用?”

Because of that, the DLL can't be built. 因此,无法构建DLL。 I am assuming, that a certain reference to the solution is required, but I am not sure how to proceed. 我假设需要对该解决方案进行一定的引用,但是我不确定如何进行。

I know how to add a reference from a solution to a DLL, but I'm not sure how it's done the other way around or even if it's possible. 我知道如何从解决方案中添加对DLL的引用,但是我不确定它是如何做到的,即使可能也是如此。

You can't add reference to EXE from class library (assuming EXE uses that class library) as it will introduce circular reference. 您不能从类库添加对EXE的引用(假设EXE使用该类库),因为它将引入循环引用。

Usually you need to refactor all dependencies so class library either have them all or allow application to inject dependencies from EXE/other clients. 通常,您需要重构所有依赖项,因此类库要么全部拥有它们,要么允许应用程序从EXE /其他客户端注入依赖项。 In later case class library needs to define base classes/interfaces to allow such injection. 在以后的情况下,类库需要定义基类/接口以允许这种注入。

Yes, you need to restore the same references that the original project uses, if they are used in the code you want to move. 是的,如果要移动的代码中使用了引用,则需要还原原始项目使用的引用。

If you need to do this by hand (ie without tools like ReSharper): 如果您需要手动执行此操作(即没有诸如ReSharper之类的工具):

  1. Move the code to the new assembly. 将代码移动到新的程序集。
  2. For each namespace or type giving the error, find it in the Object Browser. 对于给出错误的每个名称空间或类型,请在对象浏览器中找到它。
  3. Locate the assembly containing that namespace and type, and add a reference to that assembly in your new project. 找到包含该名称空间和类型的程序集,然后在新项目中添加对该程序集的引用。

You may also have to add a Project Reference to the original project. 您可能还必须将项目参考添加到原始项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM