在.NET Core中可以获得私有构造函数吗?

4
在.NET Framework中存在这样的方法:
public ConstructorInfo GetConstructor(
    BindingFlags bindingAttr,
    Binder binder,
    CallingConventions callConvention,
    Type[] types,
    ParameterModifier[] modifiers
)

但在.NET Core(1.1)中,我只找到了一个扩展,它没有给我私有构造函数:

public static ConstructorInfo GetConstructor(this Type type, Type[] types)

所以我想知道在.NET Core中是否有办法访问和获取私有构造函数。


1
你可能需要使用 GetTypeInfo().DeclaredConstructors 和 LINQ 过滤器来查找所需的构造函数。 - Ivan Stoev
2
是的,这就是正确的方法,在这个较旧的 .NET Core 版本中使用 TypeInfo 类。自 .NET Core 2.0 以来,该经典 API 调用已被实现。 - thehennyy
是的,它可以工作。谢谢。 - qwermike
@IvanStoev 你可能想把它扩展成一个答案。 - Patrick Hofman
@PatrickHofman 我没有 .net core 的经验,所以这只是一个猜测(因此是一条评论)。也许你或其他人可以提供一个完整的答案,并附上适当的参考资料。 - Ivan Stoev
1个回答

8
请使用 TypeInfo.DeclaredConstructors
根据文档,它适用于.NET Core 1.1(以及其他版本)。
您需要使用 GetTypeInfo().DeclaredConstructors 和 LINQ 过滤器来查找所需的构造函数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接