C# MethodInfo.Invoke 没有参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/569249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MethodInfo.Invoke with out Parameter
提问by Bluenuance
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?
不幸的是,TryParse 方法需要一个 out 参数,所以这不起作用。任何想法如何解决这个问题?
(ps.: would'nt this be a nice example for duck typing? - because i know every numericType has an "TryParse" or I am mistaken?)
(ps。:这不是鸭子打字的一个很好的例子吗? - 因为我知道每个 numericType 都有一个“TryParse”或者我错了?)
采纳答案by TcKs
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 = (int)parameters[1];
}