C# GetConstructor() 不返回父构造函数

标签 c# reflection constructor

我正在尝试寻找具有特定签名的构造函数。此构造函数在当前类型中不存在,但在其父类型中存在。举例说明:

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/

相关文章:

c# - 使用 ephtmltopdf 将 html 转换为 pdf

c# - 如何在编译时注入(inject) C# 代码?

c# - 对象传递回调用者/创建者后,(许多)一类属性丢失了吗?

C++ 在构造函数中通过引用传递对象和复制构造函数混淆

java - 短对象创建

c# - LINQ GroupBy 仅在键不为空时选择

c# - 创建表单并在后台线程上使用 Form.ShowDialog

c# - 在Controller Web API中创建自定义错误401响应

reflection - 在 Go 中获取接口(interface)的 reflect.Type 的更好方法

reflection - 在 F# 中,如何访问函数的属性?