我有一些自定义属性,这个类是许多其他clases 基类的基类这是我的课:需要运行一个受保护的方法时,我的类实例化
[CustomAttribute1("First")]
[CustomAttribute2("Other")]
public class Foo
{
protected void checkAttributes()
{
// Need to run this code when the class and derived classes are instantiated
}
}
然后,如果我创建一个实例
Foo MyClass = new Foo();
或建立一个dereived类:
[CustomAttribute1("FirstDerived")]
public CustClass : Foo
{
public CustClass(int MyVar)
{
//Something here
}
public void OtherMethod()
{
}
}
CustClass MyClass = new CustClass(5);
我需要个e方法checkAttributes() allways运行。
这是可能吗?
还有另一个aproach?
注:我需要舒尔checkAttributes()运行,即使在派生类的构造函数被重新定义:
但如果在派生类中有一个像美孚(INT MYVAR)另一个构造什么发生? –
@JuanPabloGomez任何类型的**构造函数**必须**只调用一个其他构造函数('System.Object'是唯一的异常),无论是在相同类型还是在父类型上。如果省略链式构造函数调用,则使用父级无参数构造函数。 (如果父类型不包含无参数构造函数,并且在派生类型的构造函数中未明确指定链接构造函数,则这是编译时错误。)[请参见演示。](http://ideone.com/ OgXXeA) – cdhowie
这意味着空的构造函数总是在我的类型和派生类型上运行?我的问题是派生类,他们可以实现它拥有构造函数。 –