简体   繁体   中英

C# VS2012 references, adding a Class Library?

Why is it that if you have a Class Library in your solution, the only way to reference it so that the VS2012 will recognize the classes within it, is by going into references and choosing the class from the projects tab. Why can you not instead compile the Class Library separately and use the generated .dll and add that through the browse tab, note: that I am still assuming that the Class Library still exists as a separate project in your solution.

You can. It doesn't really make a lot of sense to reference the compiled version instead of the project, but you can. When you add a reference to a project, click on "Browse" on the left side of the dialog and/or the "Browse" button on the bottom of the dialog. This will allow you to browse to a DLL file and add it as a reference.

You can compile the project and reference the compiled dll as you said

however : Referencing a projects dll will reference a specific configuration so you may be referencing the debug compiled dll but you would not want that when you release your solution using the release configuration. Referencing the dll means that you need to include the bin folder in source control which again is not best practice as it should be created when a solution is compiled.

Referencing the project is best practice and means that when you build a different configuration all projects are compiled to that configuration.

There is not only Projects and Dll way , there is one more that is called "Dynamic Loading" using System.Reflections . You can pick up desired dll's in runtime too and use its class and memebers

  1. Benefits of adding project as reference is that now matter what your changes are on successful compilation of the referencing project or entire solution the class called are automatically referenced.

  2. Referencing dll - If you are thinking of some common methods in all your projects and no change is required in your existing utility dll then compiling to dll and referencing it as dll is the best bet. Also note, If you are using dll as reference , everytime you need to compile and refer whenever you change code in your class library project.

  3. If using Reflections in net, It will give you power to use the dll in you way, what dll to load and what to not, but it will be much more tedious than the above two one.

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