MethodInfo.Invoke与OUT参数 [英] MethodInfo.Invoke with out Parameter

查看:141
本文介绍了MethodInfo.Invoke与OUT参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为例code我尝试做一定会做得比我更好的英语:

 公共BOOL IsNumericValueInBounds(字符串值,类型numericType)
{
  双D = double.NaN;  。布尔界外球=(布尔)numericType.GetMethod(的TryParse)调用(空,新的对象[] {值,D});  返回界外球;
}

可惜的TryParse方法需要的输出参数所以这是行不通的。
任何想法如何解决这个问题?

(PS:这would'nt对鸭打字很好的例子 - ?因为我知道每个numericType有一个的TryParse还是我错了)


解决方案

 公共静态布尔的TryParse(字符串文本,出INT数){} ....MethodInfo的方法= GetTryParseMethodInfo();
[对象]参数=新的对​​象[] {12345,空}
对象result = method.Invoke(NULL,参数);
布尔blResult =(布尔)结果;
如果(blResult){
    INT parsedNumber =参数[1];
}

an example code what I try to do will surely do better than my english:

public bool IsNumericValueInBounds (string value, Type numericType)
{
  double d = double.NaN;     

  bool inBounds = (bool)numericType.GetMethod ("TryParse").Invoke (null, new object[] { value, d });

  return inBounds;
}

Unfortunately the TryParse method needs an out parameter so this doesn't work. any ideas how to solve this?

(ps.: would'nt this be a nice example for duck typing? - because i know every numericType has an "TryParse" or I am mistaken?)

解决方案

public static bool TryParse( string text, out int number ) { .... }

MethodInfo method = GetTryParseMethodInfo();
object[] parameters = new object[]{ "12345", null }
object result = method.Invoke( null, parameters );
bool blResult = (bool)result;
if ( blResult ) {
    int parsedNumber = parameters[1];
}

这篇关于MethodInfo.Invoke与OUT参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆