如何用反射调用一个具有ref参数的方法

CodeTalk 2005-12-08 11:44:11
在用反射的方式(后绑定)访问Com对象的时候,涉及这类问题。比如:
public class MyClass1
{
public int MyMethod( int i, ref long k)
{
k = 2;
return 0;
}

public static void Main()
{
try
{
Type objType;
object objBinding;

objType = typeof(MyClass1);
objBinding = Activator.CreateInstance(objType);

Type[] paramTypes = new Type[] {Type.GetType("System.Int32"), Type.GetType("System.Int32&")};

MethodInfo m = objType.GetMethod("MyMethod",paramTypes);
object[] args = new object[2];
args[0] = 1;
args[1] = //-------写到这里就不知道怎么写了。

int res=m.Invoke(objBinding,args);

}
catch(TargetInvocationException ee)
{
Console.WriteLine("\terr:{0}",ee.Message);
}
}

}
...全文
176 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
CodeTalk 2005-12-08
  • 打赏
  • 举报
回复
MyMethod方法的第二个参数类型是System.Int32& ,这是值类型的引用。好像对于一般object的ref参数是很容易的,但是对于值类型有点难。 请高手指教!

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧