MemberInfo调用out对象[] [英] MemberInfo Invoke with out object[]

查看:50
本文介绍了MemberInfo调用out对象[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在编写类似''异常处理程序包装器'的内容。

用于一组不同的方法。

案件是我有ca. 40个方法形成Web服务,

不同的返回值(和类型),以及超出参数。我想要做的就是支持每个方法调用异常(http 404,

soap异常和其他类型的异常)并用try& <包装它br />
catch(很多捕获;-)


所以我想到

私有对象SafeInvoker(代表){

尝试{

重新调用代理人

}

catch(A){}

catch(B){}

catch(C){}


等等。

}


但代表们应该为我的每个webservice metod

签名(很多组合)介绍。所以我把它留给:


私有对象SafeInvoker(System.Reflection.MethodInfo mi,object []

inObjects){

try {

返回mi.Invoke(this,inObjects);

}

捕获..


这似乎是非常聪明的解决方案,但是......我需要处理

[OUT]参数,问题是 - 是MethodInfo。 Invoke()能够

处理这样的参数(你知道

SafeInvoker签名中的object [] outObjects,以及mi.Invoke(this,outObjects); make

" out参数''outObjects''必须在控制之前分配给

离开当前方法错误。)


提前致谢...


如果您有其他想法 - 随意携带这些; - )

-

最好的问候

stic

Hi,

I''m in a middle of writing something like ''exception handler wraper''
for a set of different methodes.
The case is that I have ca. 40 methods form web servicem, with
different return values (and types), and with out parmeters. What I
want to do is to support each method call with exception (http 404,
soap exception, and other types of exceptions) and wrap it with try &
catch (a lots of catch ;-)

So I think about:
private object SafeInvoker(delegate) {
try{
retrun invoking delegate
}
catch (A){}
catch (B) {}
catch (C) {}

and so one.
}

but the delegates should be introduced for each of my webservice metod
signature (a lot of combinations.) So I leave it for:

private object SafeInvoker( System.Reflection.MethodInfo mi, object[]
inObjects ) {
try {
return mi.Invoke(this,inObjects);
}
catches..

And this seems to be quite smart solution, but then... I NEED TO handle
[OUT] parameters, and the question is - is MethodInfo.Invoke() able to
handle such a parameters (you know out object[] outObjects in the
SafeInvoker signature, and the mi.Invoke(this, outObjects); make the
"The out parameter ''outObjects'' must be assigned to before control
leaves the current method" error.)

Thanks in advance...

If you have another ideas - feel free to bring these ;-)

--
best regards
stic

推荐答案




stic写道:


stic wrote:


我正在写一些像' '异常处理程序包装器''
用于一组不同的方法。
案例是我有ca. 40个方法构成Web服务,具有不同的返回值(和类型),并且具有out parmeter。我想要做的是支持每个方法调用异常(http 404,
soap异常和其他类型的异常)并用try&
catch(很多赶上;-)

所以我想到
私人对象SafeInvoker(委托){
尝试{
重新调用委托
}
catch(A){}
catch(B){}
catch(C){}

等等。
}

但代表们应该为我的每个webservice metod
签名(很多组合)介绍。所以我把它留给:

私人对象SafeInvoker( System.Reflection.MethodInfo mi,object []
inObjects){
尝试{
返回mi.Invoke(this,inObjects);
}
捕捉..

这似乎是一个非常聪明的解决方案,但是......我需要处理
[OUT]参数,问题是 - 是MethodInfo.Invoke()能够
处理这样的参数(你知道
SafeInvoker signatur中的object [] outObjects e,和mi.Invoke(this,outObjects);在控制之前必须将out参数''outObjects'分配给当前方法错误。)

提前致谢...

如果您有其他想法 - 随意携带这些;-)

- <最好的问候
stic
Hi,

I''m in a middle of writing something like ''exception handler wraper''
for a set of different methodes.
The case is that I have ca. 40 methods form web servicem, with
different return values (and types), and with out parmeters. What I
want to do is to support each method call with exception (http 404,
soap exception, and other types of exceptions) and wrap it with try &
catch (a lots of catch ;-)

So I think about:
private object SafeInvoker(delegate) {
try{
retrun invoking delegate
}
catch (A){}
catch (B) {}
catch (C) {}

and so one.
}

but the delegates should be introduced for each of my webservice metod
signature (a lot of combinations.) So I leave it for:

private object SafeInvoker( System.Reflection.MethodInfo mi, object[]
inObjects ) {
try {
return mi.Invoke(this,inObjects);
}
catches..

And this seems to be quite smart solution, but then... I NEED TO handle
[OUT] parameters, and the question is - is MethodInfo.Invoke() able to
handle such a parameters (you know out object[] outObjects in the
SafeInvoker signature, and the mi.Invoke(this, outObjects); make the
"The out parameter ''outObjects'' must be assigned to before control
leaves the current method" error.)

Thanks in advance...

If you have another ideas - feel free to bring these ;-)

--
best regards
stic






stic,


在实践中这似乎是一个好主意,你将有一个时间创造不是一个,但每个功能的两个版本(一个

,公共可访问) ,以及实际执行逻辑的那个。)

另外,创建用

逻辑动态调用方法的代码会给你带来如此多的开销我真的不值得,

在我看来。


相反,我会在你的每个方法中放置一个try / catch块,并且

有一个通用的catch块,它调用你的异常处理代码。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" stic" < ST ************* @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
stic,

While in practice it seems like a good idea, you are going to have a
hell of a time creating not one, but TWO versions of each function (the one
that is publically acessible, and the one that actually performs the logic).
Additionally, creating the code to dynamically call the method with the
logic is going to give you so much overhead that it''s really not worth it,
in my opinion.

Rather, I would place a try/catch block in each of your methods, and
have one generic catch block, which calls your exception handling code.

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

"stic" <st*************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...


我正在编写类似异常处理程序包装器的内容,用于一组不同的方法。
案例是我有ca. 40个方法构成Web服务,具有不同的返回值(和类型),并且具有out parmeter。我想要做的是支持每个方法调用异常(http 404,
soap异常和其他类型的异常)并用try&
catch(很多赶上;-)

所以我想到
私人对象SafeInvoker(委托){
尝试{
重新调用委托
}
catch(A){}
catch(B){}
catch(C){}

等等。
}

但代表们应该为我的每个webservice metod
签名(很多组合)介绍。所以我把它留给:

私人对象SafeInvoker( System.Reflection.MethodInfo mi,object []
inObjects){
尝试{
返回mi.Invoke(this,inObjects);
}
捕捉..

这似乎是一个非常聪明的解决方案,但是......我需要处理
[OUT]参数,问题是 - 是MethodInfo.Invoke()能够
处理这样的参数(你知道
SafeInvoker签名中的对象[] outObjects,和mi.Invoke(this,outObjects);在控制之前必须将out参数''outObjects'分配给当前方法错误。)

提前致谢...

如果您有其他想法 - 随意携带这些;-)

- <最好的问候
stic
Hi,

I''m in a middle of writing something like ''exception handler wraper''
for a set of different methodes.
The case is that I have ca. 40 methods form web servicem, with
different return values (and types), and with out parmeters. What I
want to do is to support each method call with exception (http 404,
soap exception, and other types of exceptions) and wrap it with try &
catch (a lots of catch ;-)

So I think about:
private object SafeInvoker(delegate) {
try{
retrun invoking delegate
}
catch (A){}
catch (B) {}
catch (C) {}

and so one.
}

but the delegates should be introduced for each of my webservice metod
signature (a lot of combinations.) So I leave it for:

private object SafeInvoker( System.Reflection.MethodInfo mi, object[]
inObjects ) {
try {
return mi.Invoke(this,inObjects);
}
catches..

And this seems to be quite smart solution, but then... I NEED TO handle
[OUT] parameters, and the question is - is MethodInfo.Invoke() able to
handle such a parameters (you know out object[] outObjects in the
SafeInvoker signature, and the mi.Invoke(this, outObjects); make the
"The out parameter ''outObjects'' must be assigned to before control
leaves the current method" error.)

Thanks in advance...

If you have another ideas - feel free to bring these ;-)

--
best regards
stic



Nicholas,


你的真理和智慧声明,但.. ;-)


我正在使用网络服务(我没有提及,因为我想要

)清晰的图片)。然后,我所拥有的是一套网络服务

方法,你看,


public Some Some SomeServiceServiceVeryImportantMethod(){

string methodName =

System.Reflection.MethodInfo.GetCurrentMethod()。Na me;

MethodInfo mi = ws.GetType()。GetMethod(methodName); //其中ws是

我的Web服务

//我检查这个web服务是否真的有这个方法

if(mi == null )$

NullReferenceException nex =

new NullReferenceException(Web服务没有

定义方法:" + methodName);

抛出nex;

}

返回this.SafeInvoker(mi,null);

}


和SafeInvoker只取mi和inObjects(object [])...并且

包裹调用执行mi.Invoke(ws,inObjects)。

这很有效,很简单,也很快。更重要的是我

可以添加尽可能多的异常类型,我只用一个

方法。


我做的第二件事,是带有一般System.Exception的方法

ex

以某种方式将它发送到我的异常类型

if(ex是MyExceptionA)doAThings;

if(ex是MyExceptionB)doBThings


抛出新的异常("",ex) ; - 我也传递了消息;-)


这适用于[out]方法 - 但必须简单或更智能

实现此目的! !!


-

最好的问候

stic

Nicholas,

There is truth and wisdom in your statement, but .. ;-)

I''m doing this with web service (what I didn''t mention because I want
to have clear picture). And then, what I have is a set of web service
methods, you see

public whatever SomeWebServiceVeryImportantMethod() {
string methodName =
System.Reflection.MethodInfo.GetCurrentMethod().Na me;
MethodInfo mi = ws.GetType().GetMethod(methodName); //where ws is
my Web Service
//I check if this web service really has this method
if ( mi == null ) {
NullReferenceException nex =
new NullReferenceException("Web service does not
define method: "+methodName);
throw nex;
}
return this.SafeInvoker(mi, null);
}

and the SafeInvoker just take the mi and inObjects (object[])... And
wrapped call is executed mi.Invoke(ws, inObjects).
This is working, is simple, and quite fast. What is more important I
could add as much exception types as I need, and I do it only in one
method.

The second thing I do, Is a method with takes general System.Exception
ex
dispatch it, to my exception types, in a way of
if (ex is MyExceptionA) doAThings;
if(ex is MyExceptionB) doBThings

throw new Exception("",ex); - I pass the message also ;-)

And this works for [out] methods - but THERE MUST BE SIMPLER or SMARTER
way to achieve this !!!

--
best regards
stic

这篇关于MemberInfo调用out对象[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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