使用输出参数调用反射 [英] reflection invoke with output parameters

查看:53
本文介绍了使用输出参数调用反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我?我正在试图用反射调用一个成员,但是这个成员需要两个参数,第一个参数是一个字符串,第二个是一个

引用数据集(输出参数)。参数必须在一系列对象中传递




我尝试过这样的事情:


object [] parameters = new object [2];


parameters [0] =" text";


DataSet DS = new DadaSet();

parameters [1] = DS;


Myclass.InvokeMember(" MyMethod",BindingFlags.Invok eMethod,null,

obj,参数);


但它确实有效吗?因为数组的第二项(参数[1])

应该是对数据集的引用....我怎样才能解决这个问题?

谢谢

Hi, I?′m triying to invoke a member using reflection, but this member requiere
two parameters, the first patrameter is a string and the second is a
reference to a dataset (output parameter). The parameters has to be passed
in an array of objects.

I?′ve tried something like this:

object[] parameters = new object[2];

parameters [0] ="text";

DataSet DS=new DadaSet();
parameters [1]= DS;

Myclass.InvokeMember("MyMethod",BindingFlags.Invok eMethod,null,
obj,parameters );

But it does?′nt work because the second item of the array (parameters[1])
should be a refference to a dataset.... How can I solve this problem?
Thanks

推荐答案

嗨Gus,


试试这个样本:


使用System;

使用System.Collections;

使用System.Reflection;


公共类MyClass

{

公共类TestClass {

public void MyMethod(out int x){

x = 42;

}

} $ / $

public static void Main(){

TestClass testClass = new TestClass();

MethodInfo methodInfo = typeof(TestClass ).GetMethod(MyMethod,

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


object [] parameters = new object [1];

methodInfo.Invoke(testClass,parameters);

Console.WriteLine(parameters [0]);

Console.ReadLine();

}

}


Oliver Sturm

-

omnibus ex nihilo ducendis sufficit unum

插入空格以防止谷歌电子邮件销毁:

MSN oliver @ sturmnet.org Jabber sturm @ amessage .de

ICQ 27142619 http://www.sturmnet.org/博客
Hi Gus,

try this sample:

using System;
using System.Collections;
using System.Reflection;

public class MyClass
{
public class TestClass {
public void MyMethod(out int x) {
x = 42;
}
}

public static void Main() {
TestClass testClass = new TestClass();
MethodInfo methodInfo = typeof(TestClass).GetMethod("MyMethod",
new Type[] { Type.GetType("System.Int32&") });

object[] parameters = new object[1];
methodInfo.Invoke(testClass, parameters);
Console.WriteLine(parameters[0]);
Console.ReadLine();
}
}

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


Gus,


你将不得不使用InvokeMember的重载来获取一个
ParameterModifier实例。这将告诉反思注意

ref / out参数并修改对象数组(如果有ref / out

参数)。

你的电话应该是这样的。


//设置参数修饰符。

ParameterModifier [] mods = new ParameterModifier [2 ] {new ParameterModifier(),

new ParameterModifier()};


//设置允许修改的第二个参数。

mods [1] [0] = true;


//拨打电话。

Myclass.InvokeMember(" MyMethod",BindingFlags) .InvokeMethod,null,obj,

参数,mods,null,null);


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam .guard.caspershouse.com


" Gus" < Gu*@discussions.microsoft.com>在消息中写道

新闻:AC ********************************** @ microsof t.com ...
Gus,

You will have to use the overload of InvokeMember that takes an array of
ParameterModifier instances. This will tell reflection to pay attention to
ref/out parameters and modify the array of objects if there is a ref/out
parameter.

Your call should be something like this.

// Set up the parameter modifiers.
ParameterModifier[] mods = new ParameterModifier[2]{new ParameterModifier(),
new ParameterModifier()};

// Set the second parameter to be allowed to be modified.
mods[1][0] = true;

// Make the call.
Myclass.InvokeMember("MyMethod", BindingFlags.InvokeMethod, null, obj,
parameters, mods, null, null);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gus" <Gu*@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
我正在试图用反射来调用一个成员,但这个成员需要两个参数,第一个参数是一个字符串而且第二个是对数据集(输出参数)的引用。参数必须在一系列对象中传递

我尝试过这样的事情:

object [] parameters = new对象[2];

参数[0] =" text";

DataSet DS = new DadaSet();
参数[1] = DS;

Myclass.InvokeMember(" MyMethod",BindingFlags.Invok eMethod,null,
obj,参数);

但它不起作用,因为第二数组的项目(参数[1])
应该是对数据集的引用....如何解决这个问题?
谢谢
Hi, I′m triying to invoke a member using reflection, but this member
requiere
two parameters, the first patrameter is a string and the second is a
reference to a dataset (output parameter). The parameters has to be
passed
in an array of objects.

I′ve tried something like this:

object[] parameters = new object[2];

parameters [0] ="text";

DataSet DS=new DadaSet();
parameters [1]= DS;

Myclass.InvokeMember("MyMethod",BindingFlags.Invok eMethod,null,
obj,parameters );

But it does′nt work because the second item of the array (parameters[1])
should be a refference to a dataset.... How can I solve this problem?
Thanks



很棒,看起来比我的解决方案更清洁。

Oliver Sturm

-

omnibus ex nihilo ducendis sufficit unum

插入空格以防止谷歌电子邮件销毁:

MSN oliver @ sturmnet.org Jabber sturm @ amessage.de

ICQ 27142619 http://www.sturmnet.org/blog
Great, that looks cleaner than my solution.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


这篇关于使用输出参数调用反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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