我有一个带有静态字段和静态函数的类,例如这样重新定义C#中派生/嵌套类中的静态字段
public class A {
protected static string[] _eventField = new[] { "SomeValue" };
public static TOut DoSomethingThatDependsOnEventField(TIn input){
//output depends on input and the static _eventField
}
public class Nested1: A {
protected new static string[] _eventField = new[] { "SomethingDifferent" };
}
public class Nested2 : A {
protected new static string[] _eventField = new[] { "SomethingElse" };
}
}
的输出和输入类型的静态方法是在这里并不重要,唯一相关的是,输出 - 尽管依靠输入,当然 - 依赖于静态字段的内容。该方法的实现在派生类中根本不会改变,我只想改变那个非常静态的字段。但每当我喜欢
var res1 = A.Nested1.DoSomethingThatDependsOnEventField(...);
或 VAR RES2 = A.Nested2.DoSomethingThatDependsOnEventField(...)的调用;
来自基类A的不正确静态字段是从方法内引用的。 也就是说,通过protected new static string[] _eventField = ...
通过protected new static string[] _eventField = ...
打算“隐藏”/“重新定义”静态字段不起作用 - 这是为什么?
为什么要使用嵌套静态类? – Romoku
@Romoku为什么不呢?这只是为了方便起见,当类没有嵌套时,你是否期望有不同的行为? (它没有表现出任何不同的afaik)。 – MrCC
可能有人的“重复”标记者请评论*为什么*他们认为这是一个问题的重复[248263](http://stackoverflow.com/questions/248263/why-cant-i-declare-c-尖锐的方法虚和静态)?我并没有问及“虚拟化”静态方法,我所要求的只是改变这种静态方法所运行的数据。 – MrCC