我在低级名称空间中有一个枚举。 我想在“继承”低级枚举的中级命名空间中提供一个类或枚举。

namespace low
{
public enum base
{
x, y, z
}
}
namespace mid
{
public enum consume : low.base
{
}
}

我希望这是可能的,或者希望可以使用某种类来代替枚举消耗,这将为枚举提供抽象层,但仍允许该类的实例访问枚举。

有什么想法吗?

编辑:我没有将其切换为类中的const的原因之一是我必须使用的服务需要低级枚举。 我得到了WSDL和XSD,它们将结构定义为枚举。 该服务无法更改。

#1楼

我知道这个答案有点晚了,但这就是我最终要做的事情:

public class BaseAnimal : IEquatable
{
public string Name { private set; get; }
public int Value { private set; get; }
public BaseAnimal(int value, String name)
{
this.Name = name;
this.Value = value;
}
public override String ToString()
{
return Name;
}
public bool Equals(BaseAnimal other)
{
return other.Name == this.Name && other.Value == this.Value;
}
}
public class AnimalType : BaseAnimal
{
public static readonly BaseAnimal Invertebrate = new BaseAnimal(1, "Invertebrate");
public static readonly BaseAnimal Amphibians = new BaseAnimal(2, "Amphibians");
// etc
}
public class DogType : AnimalType
{
public static readonly BaseAnimal Golden_Retriever = new BaseAnimal(3, "Golden_Retriever");
public static readonly BaseAnimal Great_Dane = new BaseAnimal(4, "Great_Dane");
// etc
}

然后,我可以执行以下操作:

public void SomeMethod()
{
var a = AnimalType.Amphibians;
var b = AnimalType.Amphibians;
if (a == b)
{
// should be equal
}
// call method as
Foo(a);
// using ifs
if (a == AnimalType.Amphibians)
{
}
else if (a == AnimalType.Invertebrate)
{
}
else if (a == DogType.Golden_Retriever)
{
}
// etc
}
public void Foo(BaseAnimal typeOfAnimal)
{
}

#2楼

上面使用具有int常量的类的解决方案缺乏类型安全性。 即,您可以发明实际上在类中未定义的新值。 此外,例如不可能编写将这些类之一作为输入的方法。

你需要写

public void DoSomethingMeaningFull(int consumeValue) ...

但是,当没有枚举可用时,有一个基于Java的老式解决方案。 这提供了几乎类似于枚举的行为。 唯一的警告是这些常量不能在开关语句中使用。

public class MyBaseEnum
{
public static readonly MyBaseEnum A = new MyBaseEnum( 1 );
public static readonly MyBaseEnum B = new MyBaseEnum( 2 );
public static readonly MyBaseEnum C = new MyBaseEnum( 3 );
public int InternalValue { get; protected set; }
protected MyBaseEnum( int internalValue )
{
this.InternalValue = internalValue;
}
}
public class MyEnum : MyBaseEnum
{
public static readonly MyEnum D = new MyEnum( 4 );
public static readonly MyEnum E = new MyEnum( 5 );
protected MyEnum( int internalValue ) : base( internalValue )
{
// Nothing
}
}
[TestMethod]
public void EnumTest()
{
this.DoSomethingMeaningful( MyEnum.A );
}
private void DoSomethingMeaningful( MyBaseEnum enumValue )
{
// ...
if( enumValue == MyEnum.A ) { /* ... */ }
else if (enumValue == MyEnum.B) { /* ... */ }
// ...
}

#3楼

另一个可能的解决方案:

public enum @base
{
x,
y,
z
}
public enum consume
{
x = @base.x,
y = @base.y,
z = @base.z,
a,b,c
}
// TODO: Add a unit-test to check that if @base and consume are aligned

高温超导

#4楼

这就是我所做的。 我所做的不同之处是在“ consuming” enum上使用了相同的名称和new关键字。 由于enum的名称相同,因此您可以不加思索地使用它,它是正确的。 另外,您还将获得智能感知。 您只需在设置时手动小心,将值从基础复制过来并保持同步即可。 您可以在代码注释中提供帮助。 这是为什么在数据库中存储enum值时,我总是存储字符串而不是值的另一个原因。 因为如果您使用的是自动分配的递增整数值,则这些整数值会随着时间变化。

// Base Class for balls
public class BaseBall
{
// keep synced with subclasses!
public enum Sizes
{
Small,
Medium,
Large
}
}
public class VolleyBall : BaseBall
{
// keep synced with base class!
public new enum Sizes
{
Small = BaseBall.Sizes.Small,
Medium = BaseBall.Sizes.Medium,
Large = BaseBall.Sizes.Large,
SmallMedium,
MediumLarge,
Ginormous
}
}

#5楼

这是不可能的(就像已经提到的@JaredPar一样)。 试图使逻辑变通解决此问题是一种不好的做法。 如果您有一个具有enum的base class ,则应在此列出所有可能的enum-values ,并且该类的实现应使用它知道的值。

例如,假设您有一个基类BaseCatalog ,并且它有一个enum ProductFormats ( Digital , Physical )。 然后你就可以有一个MusicCatalog或BookCatalog可能同时包含Digital和Physical产品,但如果类是ClothingCatalog ,它应该只包含Physical产品。