Exception capture when calling using methodinfo. Invoke

Source: Internet
Author: User

Usually we call a method. If you try, catch can catch the thrown exception.

However, you must use the methodinfo. Invoke method to call methods of an unfixed class.

However, this method does not return the thrown information in the original mode, but returns a targetinvocationexception.

 

To solve this problem, we can directly use the innerexception of the exception to obtain the original exception.

 

To resolve a medium exception:

If we throw a faultexception <classname> type exception,

You can use the following code to parse internal class objects.

 

Code
# Region errorserialize
/// <Summary>
///
/// </Summary>
/// <Param name = "ex"> </param>
/// <Param name = "pserializeerrorobject"> </param>
/// <Returns> "": not serialize object "1": serialize object </returns>
Public String serializeerrorobject (system. Exception ex, ref xmldocument pserializeerrorobjectdoc)
{
String vrtn = "";

Xmldocument vserializeerrorobjectdoc = new xmldocument ();

If (! Ex. GetType (). fullname. startswith ("system. servicemodel. faultexception '1 "))
{
Return vrtn;
}

Type vtype = ex. GetType ();

// Internal objects are stored in faultexception <classname>. Detail.
Propertyinfo vpropinfo = vtype. getproperty ("detail ");

If (vpropinfo = NULL)
{
Return vrtn;
}

Object vserializeerrorobject = vpropinfo. getvalue (ex, null );

If (vserializeerrorobject = NULL)
{
Return vrtn;
}

Type verrorobjecttype = vserializeerrorobject. GetType ();

// If this type does not exist in the current Assembly, the specified
// If the error type does not exist, it makes no sense to return the error message.
If (verrorobjecttype = NULL)
{
String vtypename = vserializeerrorobject. tostring ();

Verrorobjecttype = This. m_loadassembly.gettype (vtypename );
}

If (verrorobjecttype = NULL)
{
Return vrtn;
}

// String is a special object instead of a value type
If (vserializeerrorobject. GetType (). fullname. Equals ("system. String "))
{
Xmlnode vimportnode = (xmlnode) vserializeerrorobjectdoc. createelement ("errormsg ");

Vimportnode. innertext = vserializeerrorobject. tostring ();

Vserializeerrorobjectdoc. appendchild (vimportnode );

Pserializeerrorobjectdoc = vserializeerrorobjectdoc;

Vrtn = "1 ";
}
Else if (! Vpropinfo. propertytype. isvaluetype)
{
Vrtn = serializeclass (vserializeerrorobject, ref vserializeerrorobjectdoc );

Pserializeerrorobjectdoc = vserializeerrorobjectdoc;
}

Return vrtn;
}
# Endregion

/// <Summary>
/// Haven'targs
/// </Summary>
/// <Returns> valuetype: "" isref: "1" </returns>
Public String serializeclass (Object pinstanceobj, ref xmldocument pserializeobject)
{
If (pinstanceobj = NULL)
{
Return "";
}

Type vclasstype = pinstanceobj. GetType ();

If (vclasstype = NULL)
{
Return "";
}
Else if (vclasstype. isvaluetype |
Vclasstype. Equals (typeof (string )))
{
Return "";
}

Xmlserializer vserializer = new xmlserializer (vclasstype );

Memorystream vstream = new memorystream ();

Xmlwriter vwriter = xmlwriter. Create (vstream );

Vserializer. serialize (vwriter, pinstanceobj );

Xmldocument vserializeobjectdoc = new xmldocument ();

Vstream. Seek (0, seekorigin. Begin );

Vserializeobjectdoc. Load (vstream );

Vwriter. Close ();

Vstream. Close ();

Pserializeobject = vserializeobjectdoc;

Return "1 ";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.