简体   繁体   English

在另一个项目/命名空间中使用现有源代码

[英]Use existing source code in another project/namespace

I have two library projects within a single solution (in addition to other projects) which necessarily need to share certain classes but must remain separate for automatic-update reasons. 我在一个解决方案中有两个库项目(除了其他项目),它们必然需要共享某些类,但由于自动更新的原因必须保持独立。

For the classes that are shared, I would ideally like to use the same class.cs file in both libraries so that I don't have to consistently check that changes to the class are propagated through both libraries. 对于共享的类,我理想地希望在两个库中使用相同的class.cs文件,这样我就不必一致地检查对类的更改是否通过两个库传播。

However the namespaces of the two libraries are different, and so the class-containing file in each library requires a different namespace classlib {} declaration. 但是 ,两个库的名称空间不同,因此每个库中包含类的文件需要不同的namespace classlib {}声明。

I am using a git repo, if there is a technique to do this through branch/merge operations. 我正在使用git repo,如果有技术通过分支/合并操作来完成此操作。
Presently using VS2013. 目前正在使用VS2013。

How can I achieve this? 我怎样才能做到这一点?


Example: 例:

library1.dll

namespace library1
{
    public class SharedClass
    {
        /// code must match SharedClass in libary2
    }
}

library2.dll

namespace libary2
{
    public class SharedClass
    {
        /// code must match SharedClass in library1
    }
}

Declare the SharedClass in a common namespace, instead of two different namespaces. 在公共名称空间中声明SharedClass,而不是两个不同的名称空间。 You could link the file into the projects instead of including it physically. 您可以将文件链接到项目中,而不是物理地包含它们。

From msdn : 来自msdn

You link to a file from your project in Visual Studio. 您链接到Visual Studio中项目的文件。 In Solution Explorer, right-click your project, and then select Add Existing item Or, you can type Shift+Alt+A. 在解决方案资源管理器中,右键单击项目,然后选择“添加现有项目”或者,可以键入Shift + Alt + A. In the Add Existing Item dialog box, select the file you want to add, and in the Add drop-down list, click Add As Link. 在“添加现有项”对话框中,选择要添加的文件,然后在“添加”下拉列表中,单击“添加为链接”。

namespace Khargoosh.MathLib.Common   { public class SharedClass { ... }  }
namespace Khargoosh.MathLib.Library1 { ... }
namespace Khargoosh.MathLib.Library2 { ... }

or 要么

namespace Khargoosh.MathLib          { public class SharedClass { ... }  }
namespace Khargoosh.MathLib.Library1 { ... }
namespace Khargoosh.MathLib.Library2 { ... }

A completely other way of handling that would be to use the T4 template with some logic to create the file dynamically. 一种完全不同的处理方式是使用T4模板和一些逻辑来动态创建文件。 Content of the *.tt template files (not *.cs files!): * .tt模板文件的内容(不是* .cs文件!):

namespace library1
{
<#@ include file="MyCommonClass.cs"#>
}

And in the other library 而在另一个图书馆

namespace library2
{
<#@ include file="MyCommonClass.cs"#>
}

The class file itself would not declare a namespace. 类文件本身不会声明命名空间。

Based on the info you've provided, if your shared classes are truly "common" you should create a 3rd library that both of your main libs can reference. 根据您提供的信息,如果您的共享类真正“普遍”,您应该创建一个第三个库,您的两个主要库可以引用。 for example: 例如:

MainLib1 ( reference commonLib ) MainLib1( 引用 commonLib)

MainLib2 ( reference commonLib ) MainLib2( 引用 commonLib)

commonLib (includes class.cs and other common code) commonLib(包括class.cs和其他常见的代码)

I need to be able to update the library.dll files separately 我需要能够单独更新library.dll文件

Then you should use submodules for this task. 然后,您应该使用子模块执行此任务。

Submodule are different git repositories under the same root. 子模块是同一根下的不同git存储库。
This way you can manage 2 different project at folder level inside the root repository 这样,您可以在根存储库中的文件夹级别管理2个不同的项目

在此输入图像描述

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

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