简体   繁体   English

如何在同一解决方案内的多个项目中使用 C# class

[英]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.

相关问题 C#解决方案 - 有多少个项目? - C# Solution - How many projects? 如何确保ac#解决方案中的所有项目都使用相同版本的第三方库? - How to ensure that all projects in a c# solution use same versions of third party library? C#依赖注入在同一解决方案上有3个项目 - C# Dependency Injection with 3 projects on same Solution 如何在具有多个项目的解决方案中使用GitHub存储库 - How to use GitHub repo with a solution with many projects 具有许多项目的Visual Studio C#解决方案(依赖项) - Visual Studio C# Solution with many Projects (Dependencies) 我可以使用 c# 在 Visual Studio 中的同一解决方案中使用两个项目吗 - Can I use two projects in same solution in Visual Studio using c# C#如何创建具有多个相同命名项目的解决方案? - C# How to create a solution with more than one same named projects? 我有一个 C# 解决方案,其中同一个文件属于多个项目。 如何? - I have a C# solution where the same file belongs to multiple projects. How? 是否可以在C#项目中使用XDT? - Is it possible to use XDT inside C# projects? C#:自定义颜色类可供解决方案中的所有项目访问 - C#: Custom Colors class accessible to all projects in solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM