[英]How to use a C# class in many projects inside the same solution
I'm learning C# and I don't know exactly how to make a class visible to all projects inside the Solution.我正在学习 C#,但我不知道如何使 class 对解决方案中的所有项目可见。 Basically I have many projects, all Windows Forms, all of them have some similarity, so they can share a lot of classes.基本上我有很多项目,都是Windows Forms,都有一些相似之处,所以可以分享很多类。 I think a good approach is put all the shared classes in the Solution folder and make all the apps get the classes from there.我认为一个好的方法是将所有共享类放在解决方案文件夹中,并让所有应用程序从那里获取类。 Is this a good approach?这是一个好方法吗? How to do it?怎么做? Another way to do it, I think, is to put all the shared classes in one app and the others get that references.我认为另一种方法是将所有共享类放在一个应用程序中,而其他人则获得该引用。
If that is not a good approach, which one is?如果这不是一个好方法,那是哪一个? How to do it?怎么做?
I'm using Visual Studio 2019. This is how my Solution looks like, but I can't share the classes between the projects:我正在使用 Visual Studio 2019。这就是我的解决方案的样子,但我无法在项目之间共享类:
Create a Class Library project within your solution and put your shared classes in there (right click on your solution in the Solution Explorer, Add, New Project).在您的解决方案中创建一个 Class 库项目并将您的共享类放在那里(在解决方案资源管理器中右键单击您的解决方案,添加,新建项目)。 Then for each of the projects that you want to access the classes from, right click on Dependencies, Add Project Reference and tick the Class Library project you created.然后对于您要从中访问类的每个项目,右键单击依赖项,添加项目引用并勾选您创建的 Class 库项目。
You create a class library project and in there you would want to add folders where you can name them according to what the classes would do so it is easier to maintain.您创建了一个 class 库项目,并且您希望在其中添加文件夹,您可以根据类的用途命名它们,这样更易于维护。 Then you would add your classes in the corresponding folder and in the project that you want to use the class you can add a reference to the project and the class would be accessible as long as it was a public class.然后,您将在相应的文件夹中添加您的类,并在您想要使用 class 的项目中添加对项目的引用,只要 class 是公共的 ZA2F2ED4F8EBC2CBB4C21A29DC40AB61D,就可以访问 class。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.