我正在尝试创建一个抽象类,它封装了我希望所有表继承的基本功能。我有几百个表/视图,并希望在所有类的编码方式中强制执行一些统一性。我知道C#不允许使用抽象枚举,但有没有更好的方法来实现我想要实现的目标?
public abstract class basetbl
{
public abstract enum efields;
public virtual void doSort(params efields[] sortfields)
{
// some generic sort algorithm
}
}
public class sometbl : basetbl
{
public override enum efields
{
field1 = 0,
field2 = 1,
field3 = 2
}
public override void doSort(params efields[] sortfields)
{
// or some other code if the base algorithm is insufficient
}
}
public class testenum
{
...
public void dosort()
{
sometbl stbl = new sometbl();
// get some data
stbl.doSort(stbl.efields.field2, stbl.efields.field1);
// do some stuff
stbl.doSort(stbl.efields.field3);
}
...
}
答案 0 :(得分:3)
也许Generics可以帮助你。 通过将基类声明为泛型,可以帮助开发人员考虑声明内部枚举。
public class basetbl<TEnum>
{
public virtual void doSort(params TEnum[] sortfields)
{
// some generic sort algorithm
}
}
public class sometbl : basetbl<sometbl.fields>
{
public enum fields
{
field1 = 0,
field2 = 1,
field3 = 2
}
public override void doSort(params fields[] sortfields)
{
// or some other code if the base algorithm is insufficient
}
}
public class testenum
{
...
public void dosort()
{
sometbl stbl = new sometbl();
// get some data
stbl.doSort(sometbl.fields.field2, sometbl.fields.field1);
// do some stuff
stbl.doSort(sometbl.fields.field3);
}
...
}