我有一个命名空间,它会在我的项目(每个产品生成)中经常更改,并且希望保留对我定义基类的文件中包含的特定命名空间的引用。在其他文件中,我定义了通常可以通过继承获取对命名空间的引用来获得的子类,但我遇到一个案例的问题,因为对象非常具体,我宁愿链接到基类中的命名空间而不是每个对象。
BaseClass.cs
namespace CommonNameSpace
{
using _productSpecificNameSpace;
_productSpecificNameSpace.thing.otherthing thingotherthingGood = _productSpecificNameSpace.thing.otherthing.Success;
_productSpecificNameSpace.thing.otherthing thingotherthingBad = _productSpecificNameSpace.thing.otherthing.SpecificFailure;
public class BaseClass
{
}
}
SubClass.cs
namespace CommonNameSpace
{
public class SubClass : BaseClass
{
var yeaOrNeigh = thingotherthingGood
}
}
如何在SubClass.cs中访问_productSpecificNameSpace而不必在每个子类中调用_productSpecificNameSpace,或者在BaseClass中命名我需要的每个可能的对象?有没有办法获得附加到类继承的命名空间的别名?
编辑:理想情况下,我希望能够访问可互换命名空间中的数据类型,因为它主要是一个枚举库。不会有一个接口用接口类型替换这些枚举的类型,这种接口类型与期望底层类型的API的调用无法比较吗?
答案 0 :(得分:0)
如果使用接口,则可以在运行时实例化您想要的生成,而不会影响任何现有代码:
interface IMyThing
{
void DoStuff()
}
abstract class BaseThing : IMyThing
{
public virtual void DoStuff()
}
Generation1:
class MyGen1 : BaseThing
{
public override void DoStuff()
{
// I'm Gen 1
}
}
第二代:
class MyGen2 : BaseThing
{
public override void DoStuff()
{
// I'm Gen 2
}
}