[英]Share code between projects in a solution in Visual Studio 2008, when building a common assembly is impossible
I create an add-on for the product Foo. 我为产品Foo创建了一个附加组件。 There are different versions of Foo, namely version 1, 2, 3 and 4.
Foo有不同的版本,即版本1、2、3和4。
These versions have a mostly compatible API, but not fully. 这些版本具有大多数兼容的API,但不完全兼容。
I currently have 5 projects: 我目前有5个项目:
The Foo*-projects contains the add-in for version 1-4 of Foo. Foo *项目包含Foo 1-4版本的加载项。
There are a lot of duplicated files in the Foo*-projects, as there are a lot of things in the API which are identical for all versions of Foo. Foo *项目中有很多重复的文件,因为API中有很多东西对于所有版本的Foo都是相同的。 It would be nice to separate out everything which is common for all Foo-versions.
最好将所有Foo版本通用的所有内容都分离出来。
Why not just create a common assembly for all versions of Foo called FooCommon? 为什么不为所有版本的Foo创建一个名为FooCommon的通用程序集呢?
If I would put all classes which are common for all versions of Foo into a new library project, I would still have to choose which version of Foo the new FooCommon should reference. 如果我将所有版本的Foo通用的所有类放到新的库项目中,我仍然必须选择新的FooCommon应该引用的Foo版本。 As said, they are not identical.
如前所述,它们并不相同。
Create an interface containing the common methods: 创建一个包含常用方法的接口:
public interface IFoo
{
void CommonMethod1();
void CommonMethod2();
}
Create an abstract base class from IFoo: 从IFoo创建一个抽象基类:
public abstract class FooBase : IFoo
{
// Implement the common calls here
public void CommonMethod1()
{
// concrete shared code goes here
}
public void CommonMethod2()
{
// concrete shared code goes here
}
}
Create your one-off code from the FooBase: 从FooBase创建一次性代码:
public class FooOne : FooBase
{
// concrete code specific to FooOne goes here
}
public class FooTwo : FooBase
{
// concrete code specific to FooTwo goes here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.