2011-08-25 119 views
2

我有以下的方法,通过胃肠 delared:Get方法失败

public static void Get(int pname, out int @params) 

我试图让使用下列方式反映它:

MethodInfo mGetMethod = typeof(Gl).GetMethod("Get", 
              BindingFlags.Public|BindingFlags.Static, 
              null, 
              new Type[] 
              { 
               typeof(Int32), 
               typeof(Int32) 
              }, 
              null); 

但是我有没有成功。为什么?

是否因为关键字?

+0

你使用.NET 4吗?如果是这样,您可以使用动态关键字来创建反射对象,然后直接调用该方法。这样你就不需要MethodInfo查找。缺点是,如果你弄错了,你会得到一个运行时异常 – ghostJago

回答

7

使用typeof(Int32).MakeByRefType()你的第二个参数。即:

MethodInfo mGetMethod = typeof(Gl).GetMethod("Get", bindingFlags.Public|BindingFlags.Static, null, new Type[] { typeof(Int32), typeof(Int32).MakeByRefType() }, null); 
+0

它已经工作了,但是如何在方法调用中处理它呢? – Luca

+0

找到它了:http://stackoverflow.com/questions/2438065/c-reflection-how-can-i-invoke-a-method-with-an-out-parameter – Luca

0

如何尝试这样的事情,而不是:

MethodInfo method = this.GetType().GetMethod("Get"); 
if (method != null) 
{ 
    method.Invoke(this, new object[] { "Arg1", "Arg2", "Arg3" }); 
} 
1

如果需要指定该方法的具体超载话,肯定有什么@Isaac Overacker说去。否则,只是不指定参数:

MethodInfo mGetMethod = typeof(Gl).GetMethod("Get", BindingFlags.Public | BindingFlags.Static); 
1

out关键字经过参考的参数,这可能是你的问题。您需要将其标记为引用类型,因为C#允许您使用byValue和byReference参数来重载方法。