【发布时间】:2026-01-30 07:15:02
【问题描述】:
我有一个命名空间,它会在我的项目(每一代产品)中经常更改,并且希望保留对包含在我定义基类的文件中的特定命名空间的引用。在其他文件中,我定义了通常可以通过继承获取对命名空间的引用的子类,但我遇到了一种情况,因为对象非常具体,我更愿意链接到基类中的命名空间而不是每个对象。
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 的调用无法比较?
【问题讨论】:
-
为什么不在你的所有代码中使用抽象基类,只将你实例化的特定类换成变量?交换实现是抽象类和接口的发明。
-
听起来需要用到接口。
标签: c# namespaces