简体   繁体   English

我可以在一个解决方案中的其他项目中使用主项目中的类和控件吗?

[英]Can I use classes and controls from main project in other projects within one solution?

How should I divide source files into projects (within one solution) to 我应如何将源文件分为多个项目(在一种解决方案内)以

  1. be able to use common classes in more relatively independent apps, 能够在相对独立的应用中使用通用类,
  2. avoid lots of dlls needed (preferably all in one file for each application), 避免需要大量的dll(最好是每个应用程序都集中在一个文件中),
  3. keep it fast? 保持快?

There are working (data processing) classes, User controls, some utility classes and Forms of the application. 有工作中的(数据处理)类,用户控件,一些实用程序类和应用程序形式。

You can make a separate assembly by creating a class library, and use that library within other projects within your solution. 您可以通过创建类库来制作单独的程序集 ,并在解决方案中的其他项目中使用该库。 Just put your reusable classes within a class library project, and add a project reference in your applications to that library. 只需将可重用的类放在类库项目中,然后在应用程序中将项目引用添加到该库。

Each time you separate out code into a separate (reusable) assembly, it does add one extra DLL (the class library project) as a requirement at runtime, but this is very minimal. 每次将代码分离到一个单独的(可重用的)程序集中时,它确实会在运行时添加一个额外的DLL(类库项目),但这是非常小的。

There are no real (significant) changes to performance when doing this. 执行此操作时,性能没有任何实际(重大)变化。 It is a very common practice. 这是非常普遍的做法。

You should make Class Library project(s) for each logical unit of classes, then add references to the libraries in each project that uses them. 您应该为类的每个逻辑单元创建类库项目,然后在使用它们的每个项目中添加对库的引用。

For example, you could have a Common library that contains basic classes used by everything else, and perhaps a Controls library that contains user controls. 例如,您可能有一个Common库,其中包含其他所有对象使用的基本类;也许还有一个Controls库,其中包含用户控件。

Each logical unit of classes can go in a namespace within the same library or in a separate library; 类的每个逻辑单元都可以进入相同库或单独库中的名称空间; you need to decide which. 您需要决定哪个。

It would be a good idea to drop the second requirement of avoiding lots of DLL's. 放弃避免大量DLL的第二个要求是一个好主意。 If you put your common code into a single "common" DLL then you need to recompile every time any class is added or modified. 如果将通用代码放入单个“通用” DLL中,则每次添加或修改任何类时都需要重新编译。 This could then give you a terrible versioning problem that is worse than managing lots of DLL's. 然后,这可能会给您带来可怕的版本控制问题,该问题比管理许多DLL还要糟糕。

You should group your common code, by the functionality they provide, into separate DLL's. 您应该按照通用代码提供的功能将它们分为单独的DLL。 So one for data access, one for user controls, one for each type of utility function, etc. Then if you have web service that accesses data you won't need to recompile the service when you add a new user control to a single DLL. 因此,一个用于数据访问,一个用于用户控件,一种用于每种类型的实用程序功能,等等。然后,如果您具有访问数据的Web服务,则在将新的用户控件添加到单个DLL时就不需要重新编译该服务。 。 Only those apps that depend on the change will need to be recompiled. 只有那些依赖更改的应用程序才需要重新编译。

您可以将公共类放入一个程序集中(例如CommonUtils),然后在内部使用名称空间进行分组,以指示如何拆分它们

暂无
暂无

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

相关问题 如何从我的解决方案中的其他项目中使用我的Web Api项目? - How can I use my Web Api project from other projects inside my solution? 将 Web 应用项目添加到我的解决方案中,并且解决方案中的其他项目已经使用 .net 框架,我可以将 .net 核心用于新项目吗? - adding a web app project to my solution, and the other projects in the solution already use .net framework, can I use .net core for the new project? 我有两个项目的解决方案,如何在Winforms项目中使用库类型项目中的方法? - I have a solution with two projects how can i use methods from the library type project in the winforms project? 项目中的依赖注入可以被解决方案中的其他项目使用 - Dependency injection in a project that can be used by other projects in a solution 在后台任务中使用主项目中的类 - Use classes from main project in background task 如何从解决方案中的类访问ASP.NET页面上的控件? - How can I access the controls on my ASP.NET page from a class within the solution? WCF使用解决方案中其他项目的服务 - WCF use service from other project in solution 我可以在定义的解决方案中使用roslyn分析仪吗? - Can I use a roslyn analyzer from within the solution it is defined in? 我可以在解决方案中的图书馆项目中使用ApiControllers吗? - Can i use ApiControllers from a library project in my solution? 如何使用Resharper在解决方案中的其他项目中找到C#组装项目参考? - How to find C# Assembly Project References in other Projects within Solution using Resharper?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM