问题
I have a few classes that are abstracted in a way that I can use them in multiple projects. I'm always working on these classes, optimizing, adding, etc. So when I optimize something in one of these classes, I then need to copy that new version into every project I remember using it. This isn't a very good way of doing it, but is there a better way?
Thanks
回答1:
Instead of manually copying the updated classes to every project that uses them, create a Class Library project and reference the compiled file in every project that uses the classes. Organizing your classes like that will help you to follow the DRY ("Don't repeat yourself") principle.
If you need to reference files instead of compiled libraries, however, you can reference a file as a link so that multiple projects refer to the same file without copying it to each solution folder. To do that, right-click on your project, choose Add existing item..., browse to the .cs file, and choose Add as Link from the combobox in the right lower corner.
回答2:
Put these base classes in a single project and share this project between your different solutions as an referenced class library. This way you will not have to copy / paste anything between projects or solutions and everything should always be up to date.
You could even set-up a local NuGet feed so you can use NuGet to retrieve this shared project as a reference in a well structured and managed way.
回答3:
How about if you extract the classes into a separate project, and add a reference to this project in every project you are using?
回答4:
It is a bad idea to copy paste file throughout the application. To avoid these repetitions you can either:
- make a link, if the amount of file is really small . In the Solution browser of Visual Studio, right click, Add Existing file, chose your file and in the split button, choose Add as a link
- create a separate project and reference this project wherever is is necessary if the amount of files not tiny.
回答5:
Create a base-lib and build it to a "shared" location. Add a reference to it in you project. It will keep the other projects smaller and will be faster to build.
来源:https://stackoverflow.com/questions/8239451/updating-classes-used-in-multiple-projects