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 ";
}