Specifying out params for Type.GetMethod

前端 未结 2 1651
太阳男子
太阳男子 2021-01-07 18:16

I\'m using reflection to get at the TryParse method info (upvote for the first person to guess why ;).

If I call:

typeof(Int32).GetMethod(\"Parse\",
         


        
相关标签:
2条回答
  • 2021-01-07 18:39

    Like @Jab's but a little shorter:

    var tryParseMethod = typeof(int).GetMethod(nameof(int.TryParse),
                                               new[]
                                               {
                                                   typeof(string),
                                                   typeof(int).MakeByRefType()
                                               });
    
    // use it
    var parameters = new object[] { "1", null };
    var success = (bool)tryParseMethod.Invoke(null, parameters);
    var result = (int)parameters[1];
    
    0 讨论(0)
  • 2021-01-07 18:56

    Try this

    typeof(Int32).GetMethod("TryParse",
      BindingFlags.Static | BindingFlags.Public,
      null,
      new Type[] { typeof(string), typeof(Int32).MakeByRefType() },
      null);
    
    0 讨论(0)
提交回复
热议问题