简体   繁体   中英

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).

When I paste the method in the DLL project, it doesn't recognize parts of the code and it's showing this error`:

"are you missing a using directive or an assembly reference?"

Because of that, the DLL can't be built. 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.

You can't add reference to EXE from class library (assuming EXE uses that class library) as it will introduce circular reference.

Usually you need to refactor all dependencies so class library either have them all or allow application to inject dependencies from EXE/other clients. 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):

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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