简体   繁体   English

在一个解决方案中处理多个项目中的通用类

[英]Handling common classes in multiple projects in a single solution

I currently have two projects say project A and project B in my solution. 我目前在解决方案中有两个项目,分别是项目A和项目B。 Now both projects A and B are using similar classes and a copy of each class is found in each project. 现在,项目A和B都使用类似的类,每个项目中都会找到每个类的副本。 I wanted to know if there is a better way to accomplish this task. 我想知道是否有更好的方法来完成此任务。 I want to have a single class somewhere and have both projects access common classes from there. 我希望在某个地方有一个单独的类,并让两个项目从那里访问公共类。 What is the recommneded way to do this. 建议这样做的方法是什么。

  1. Create a third project C in the same solution. 在同一解决方案中创建第三个项目C.
  2. Add your shared classes to C. 将您的共享类添加到C.
  3. Reference C in the projects A and B. (to do so : in the Solution Explorer , right click on References in project A and B, click Add Reference , click on tab Projects , then select Project C, click OK ) 参考下,在项目A和B(这样做的:在Solution Explorer中,右键单击项目A和B 引用 ,单击添加引用 ,单击选项卡的项目 ,然后选择项目C,单击OK)
  4. Don't forget to add the correct using namespace to use the class in the project A and B. 不要忘记添加正确的using命名空间以使用项目A和B中的类。

Or just let your shared classes in project A, or B and add reference to this project in the other project (It depends on the content but doing this way will create a kind of assymetry between the projects A and B). 或者只是让您的共享类在项目A或B中,并在另一个项目中添加对该项目的引用(这取决于内容,但是这样做会在项目A和B之间产生某种不对称性)。

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

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