在另一个 project/namespace 中使用现有源代码
Use existing source code in another project/namespace
我在一个解决方案中有两个库项目(除了其他项目),它们必然需要共享某些 classes 但由于自动更新的原因必须保持独立。
对于共享的 classes,理想情况下,我希望在两个库中使用相同的 class.cs
文件,这样我就不必始终检查对 class 通过两个库传播。
然而两个库的命名空间不同,因此每个库中包含class的文件需要不同的namespace classlib {}
声明。
我正在使用 git 存储库,如果有一种技术可以通过 branch/merge 操作来做到这一点。
目前使用 VS2013.
我怎样才能做到这一点?
示例:
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
}
}
根据您提供的信息,如果您的共享 类 确实是 "common",您应该创建一个您的两个主要库都可以引用的第三个库。例如:
主库 1
(引用commonLib)
主库2
(引用commonLib)
公共图书馆
(包括class.cs等常用代码)
I need to be able to update the library.dll
files separately
那么你应该为此任务使用子模块。
子模块是不同的 git 个相同根目录下的存储库。
这样您就可以在根存储库中的文件夹级别管理 2 个不同的项目
在公共命名空间而不是两个不同的命名空间中声明 SharedClass。您可以 link 将文件添加到项目中,而不是物理地包含它。
来自msdn:
You link to a file from your project in Visual Studio. In Solution Explorer, right-click your project, and then select Add Existing item Or, you can type 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 { ... }
或
namespace Khargoosh.MathLib { public class SharedClass { ... } }
namespace Khargoosh.MathLib.Library1 { ... }
namespace Khargoosh.MathLib.Library2 { ... }
另一种处理方式是使用 T4 template 和一些逻辑来动态创建文件。 *.tt 模板文件(不是 *.cs 文件!)的内容:
namespace library1
{
<#@ include file="MyCommonClass.cs"#>
}
而在另一个图书馆
namespace library2
{
<#@ include file="MyCommonClass.cs"#>
}
class 文件本身不会声明命名空间。
我在一个解决方案中有两个库项目(除了其他项目),它们必然需要共享某些 classes 但由于自动更新的原因必须保持独立。
对于共享的 classes,理想情况下,我希望在两个库中使用相同的 class.cs
文件,这样我就不必始终检查对 class 通过两个库传播。
然而两个库的命名空间不同,因此每个库中包含class的文件需要不同的namespace classlib {}
声明。
我正在使用 git 存储库,如果有一种技术可以通过 branch/merge 操作来做到这一点。
目前使用 VS2013.
我怎样才能做到这一点?
示例:
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
}
}
根据您提供的信息,如果您的共享 类 确实是 "common",您应该创建一个您的两个主要库都可以引用的第三个库。例如:
主库 1 (引用commonLib)
主库2 (引用commonLib)
公共图书馆 (包括class.cs等常用代码)
I need to be able to update the
library.dll
files separately
那么你应该为此任务使用子模块。
子模块是不同的 git 个相同根目录下的存储库。
这样您就可以在根存储库中的文件夹级别管理 2 个不同的项目
在公共命名空间而不是两个不同的命名空间中声明 SharedClass。您可以 link 将文件添加到项目中,而不是物理地包含它。
来自msdn:
You link to a file from your project in Visual Studio. In Solution Explorer, right-click your project, and then select Add Existing item Or, you can type 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 { ... }
或
namespace Khargoosh.MathLib { public class SharedClass { ... } }
namespace Khargoosh.MathLib.Library1 { ... }
namespace Khargoosh.MathLib.Library2 { ... }
另一种处理方式是使用 T4 template 和一些逻辑来动态创建文件。 *.tt 模板文件(不是 *.cs 文件!)的内容:
namespace library1
{
<#@ include file="MyCommonClass.cs"#>
}
而在另一个图书馆
namespace library2
{
<#@ include file="MyCommonClass.cs"#>
}
class 文件本身不会声明命名空间。