【问题标题】:How to get number of arguments in a constructor如何在构造函数中获取参数的数量
【发布时间】:2011-06-20 04:35:20
【问题描述】:

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

提前谢谢...

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

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

    ConstructorInfo ctor = /* ... */
    int numberOfArguments = ctor.GetParameters().Length;
    

    【讨论】:

      【解决方案2】:
      Type t = typeof(...);
      
      var constructors = t.GetConstructors();
      foreach (var con in constructors)
      {
          Console.WriteLine(con.GetParameters().Length);
      }
      

      【讨论】:

        猜你喜欢
        • 2021-10-09
        • 1970-01-01
        • 1970-01-01
        • 2016-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-18
        • 1970-01-01
        相关资源
        最近更新 更多