Specifying out params for Type.GetMethod

前端 未结 2 1650
太阳男子
太阳男子 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条回答
  •  -上瘾入骨i
    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];
    

提交回复
热议问题