【发布时间】:2011-11-15 20:32:02
【问题描述】:
考虑以下示例:
interface IPropertyCollection
{
public MethodWrapper GetPropertySetterByName(string name);
//<<-- I want the implementation from A and B merged into here somehow
}
class A : IPropertyCollection
{
static PropertyMap properties = new PropertyMap(typeof(A));
public MethodWrapper GetPropertySetterByName(string name)
{
return properties.SomeFunc(name);
}
}
class B : IPropertyCollection
{
static PropertyMap properties = new PropertyMap(typeof(B));
public MethodWrapper GetPropertySetterByName(string name)
{
return properties.SomeFunc(name);
}
}
我希望每个类中都有一个静态成员,仅跟踪该类中的内容,并且我希望它对每个类的行为完全相同,但内容不同。每个静态成员应该只跟踪一个类。我希望能够通过拥有任何 IPropertyCollection 的实例来访问类的静态成员。
类似这样的:
IPropertyCollection a = new A();
IPropertyCollection b = new B();
a.GetPropertySetterByName("asdfsj"); //Should end up in static A.properties
b.GetPropertySetterByName("asdfsj"); //Should end up in static B.properties
现在这将适用于我的示例代码,但我不想在 A 和 B 以及 50 个其他类中重复所有这些行。
【问题讨论】:
-
“静态实例方法”是矛盾的。它要么是静态的,要么是实例的。你真正的意思是你想要一个依赖子类初始化的静态成员,对吧?
-
在我看来是抽象类的好人选。
标签: c# inheritance static