我正在尝试寻找具有特定签名的构造函数。此构造函数在当前类型中不存在,但在其父类型中存在。举例说明:
public class Base
{
public Base()
{
}
public Base(string a1, string a2, string a3)
{
...
}
}
public class Child : Base
{
}
问题是,我似乎无法通过 .GetConstructor
找到带有字符串参数的 .ctor
,甚至尝试这样:
typeof(Child).GetConstructor(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(string), typeof(string) }, null);
将 typeof(Child)
替换为 typeof(Base)
,当然可以。
在查找父构造函数方面我是否遗漏了什么?
最佳答案
构造函数不是继承的,因此即使使用 FlattenHierarchy
也无法通过子项找到它们。
您必须遍历子项才能找到它。
关于C# GetConstructor() 不返回父构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12473692/