如何获取构造函数中的参数数

本文关键字:参数 数数 构造函数 何获取 获取 | 更新日期: 2023-09-27 17:59:42

在我的.net windows应用程序(c#)中,我想知道特定类的每个构造函数中的参数数量。我通过使用反射获得所有构造函数。有可能得到number of arguments of each constructors吗?

提前感谢。。。

如何获取构造函数中的参数数

询问其参数(通过GetParameters()),然后询问数组的长度。

ConstructorInfo ctor = /* ... */
int numberOfArguments = ctor.GetParameters().Length;
Type t = typeof(...);
var constructors = t.GetConstructors();
foreach (var con in constructors)
{
    Console.WriteLine(con.GetParameters().Length);
}