[Mono-list] Using Exceptions with external Plugins
Arild Fines
arild.fines@broadpark.no
Tue, 29 Jun 2004 16:06:45 +0200
mono-list-admin@lists.ximian.com wrote:
> Cool! Thanks!
>
> Now, how can I get sure that the InnerException is a
> XmlRpcFaultException or a XmlRpcMissedSomething?, I'm trying with this:
>
> } catch(TargetInvocationException e) {
> if(e.InnerException.GetType().ToString() ==
> "CookComputing.XmlRpc.XmlRpcFaultException") {
> Console.WriteLine("yes");
> }
>
> But I don't think it's the correct way/code, any suggestions?
You might want to take advantage of the fact that Type.InvokeMember actually
performs virtual dispatch based on the runtime type of the object *AND* all
of the parameters, IE, it performs double/triple/ntuple dispatch.
That means the following will work the way you want it to:
catch( TargetInvocationException e )
{
Type t = this.GetType();
t.InvokeMember( "HandleException", BindingFlags.InvokeMethod |
BindingFlags.Instance | BindingFlags.NonPublic, null,
this, new object[]{ e } );
}
// ...
private void HandleException( CookComputing.XmlRpc.XmlRpcFaultException e )
{
//...
}
private void HandleException( FooException e )
{
//...
}
private void HandleException( Exception e )
{
// called if there are no other matching signatures
}
Of course, it isn't particularly fast...
--
Arild
AnkhSVN: http://ankhsvn.tigris.org
Blog: http://ankhsvn.com/blog
IRC: irc://irc.freenode.net/ankhsvn
"Weaseling out of things is good. It's what separates us from the other
animals....except weasels." -- Homer Simpson