在C#中是否有可继承的静态泛型类的替代方案?
我希望有一系列静态类,每个静态类都包含一个字典,并在字典上执行两个方法。除了一个方法中的参数类型和另一个方法中的返回类型外,该类的所有版本中的方法都是相同的 理想情况下,我希望将行为(方法)放在一个“基”泛型静态类中,然后创建几个类型化的静态子体,每个子体都包含自己的静态字典。显然,我不能这样做,因为一个静态类不能从另一个静态类继承 我想知道是否有其他方法来实现我的目标。我知道我可以使用非静态类来完成这项工作,但在我看来,这更适合于静态任务 这是我想要的东西的草图:在C#中是否有可继承的静态泛型类的替代方案?,c#,generics,inheritance,static,C#,Generics,Inheritance,Static,我希望有一系列静态类,每个静态类都包含一个字典,并在字典上执行两个方法。除了一个方法中的参数类型和另一个方法中的返回类型外,该类的所有版本中的方法都是相同的 理想情况下,我希望将行为(方法)放在一个“基”泛型静态类中,然后创建几个类型化的静态子体,每个子体都包含自己的静态字典。显然,我不能这样做,因为一个静态类不能从另一个静态类继承 我想知道是否有其他方法来实现我的目标。我知道我可以使用非静态类来完成这项工作,但在我看来,这更适合于静态任务 这是我想要的东西的草图: public static
public static class BaseClass<T>
{
private static Dictionary<string, T> PrivateDict {get; set;}
private static String ToString(T argument)
{
return Somestring;
}
private static T FromString(string argument)
{
return Some-T-thing;
}
}
// This static class supplies the static dictionary but gets the static methods of BaseClass.
// Other static classes might exit that use, for instance, Dictionary<string, XElement>
public static class GenderClass : BaseClass<int>
{
private static Dictionary<string, int> PrivateDict = new Dictionary<string, int>
{
{"Male", 1},
{"Boy", 1},
{"M", 1},
{"Female", 2},
{"Girl", 2},
{"F", 2}
}
}
公共静态类基类
{
私有静态字典PrivateDict{get;set;}
私有静态字符串ToString(T参数)
{
返回sometstring;
}
私有静态T FromString(字符串参数)
{
返回一些T-thing;
}
}
//此静态类提供静态字典,但获取基类的静态方法。
//其他静态类可能退出该使用,例如Dictionary
公共静态类GenderClass:基类
{
private static Dictionary PrivateDict=新字典
{
{“男性”,1},
{“男孩”,1},
{“M”,1},
{“女性”,2},
{“女孩”,2},
{“F”,2}
}
}
关于某个东西是否应该是静态的决定与您使用它的方式有关。如果你必须有一些静态的东西,我个人会这样做:
public interface foo
{
string ToString(someBase input);
someBase FromString(string input);
}
public static class Bar
{
public static string ToString(someBase input)
{
if (input.GetType() == typeof(Duck))
{
foo duckie = new DuckHandler();
return duckie.ToString(input);
}
else if (input.GetType() == typeof(DeadParrot)) { /* ..snip.. */ }
}
public static someBase FromString(string input, type Type) {
if (type == typeof(Duckie))
{
foo duckie = new DuckHandler(input);
return duckie;
}
else if (type == typeof(OKLumberjack)) { /* ..snip.. */ }
}
}
- 有一个逻辑接口。然后根据需要创建尽可能多的类来实现它,每个类专门化逻辑。如果存在任何共享逻辑,则可以创建一个非静态的基类抽象类,并根据需要创建尽可能多的子类
- 在静态类的给定方法中,根据获得的参数选择要使用的逻辑实现类。然后实例化所选类的对象,调用相关方法,并返回其结果
public interface foo
{
string ToString(someBase input);
someBase FromString(string input);
}
public static class Bar
{
public static string ToString(someBase input)
{
if (input.GetType() == typeof(Duck))
{
foo duckie = new DuckHandler();
return duckie.ToString(input);
}
else if (input.GetType() == typeof(DeadParrot)) { /* ..snip.. */ }
}
public static someBase FromString(string input, type Type) {
if (type == typeof(Duckie))
{
foo duckie = new DuckHandler(input);
return duckie;
}
else if (type == typeof(OKLumberjack)) { /* ..snip.. */ }
}
}
您可以看到这一点。我建议使用该类型作为“继承”的路径。我引用这句话是因为它实际上只是一个旁路,但它仍然应该以你想要的方式工作 我在LinqPad中对此进行了测试,结果正确,没有抛出异常。请注意,这些字段是出于测试目的而公开的 首先,设置一个可以用来传递字典的接口
public interface IPublicDictionary<T>
{
Dictionary<string, T> PublicDictionary { get; }
}
为什么不创建一个非静态类,其中基类有一个静态成员,子类有一个静态成员?我最近问了一个类似的问题:这可能是相关的。在这个解决方案中,每次我想将字符串转换为性别时,我都会实例化一个GenderClass实例,反之亦然。这段代码将用于ETL工作中,可能涉及数十万个调用,因此我担心开销。
public static class BaseClass
{
public static String ToString<F,T>(F argument) where T : IPublicDictionary<F>, new()
{
IPublicDictionary<F> t = new T();
return t.PublicDictionary.First(d => Comparer<F>.Equals((F)d.Value, argument)).Key;
}
public static F FromString<T,F>(string argument) where T : IPublicDictionary<F>, new()
{
IPublicDictionary<F> t = new T();
return t.PublicDictionary[argument];
}
}
var s = BaseClass.FromString<GenderClass,int>("F");
Console.WriteLine(s);
var t = BaseClass.ToString<int,GenderClass>(2);
Console.WriteLine(t);
2
Female