views:

47

answers:

2

The web service has SoapExtension, which contains an error handler and serializing error in a format xml.

<? Xml version = "1.0" encoding = "utf-8" standalone = "yes"?>
<Exception Type="System.NullReferenceException"> Exception text. </ Exception>

How to make error handler, which call error of "Type"? E.g.:

Type _type = Type.GetType(doc.DocumentElement.Attributes["Type"].Value);

It must to call NullReferenceException.

+3  A: 

You need to provide the fully-qualified name, i.e. "System.NullReferenceException".

In this particular case, that's enough - because NullReferenceException is in mscorlib. However, if you need other exceptions - such as ConstraintException, which lives in the System.Data assembly - you'd need to provide the full assembly name too. Type.GetType(string) only looks in the currently executing assembly and mscorlib for type names which don't specify the assembly.

EDIT: Here's a short but complete program which works:

using System;
using System.Reflection;

class Test
{
    static void Main()
    {
        string typeName = "System.NullReferenceException";
        string message = "This is a message";
        Type type = Type.GetType(typeName);
        ConstructorInfo ctor = type.GetConstructor(new[] { typeof(string) });
        object exception = ctor.Invoke(new object[] { message });
        Console.WriteLine(exception);
    }
}
Jon Skeet
OK I correct xml: <Exception Type="System.NullReferenceException">Exception message.</Exception> How to call it?
Dublicator
@Dublicator: Well now you can use Type.GetType as per your existing code. You'll then probably want to find a constructor taking a string parameter (use `Type.GetConstructor`) and invoke it.
Jon Skeet
I use such code:ConstructorInfo ctor = _type.GetConstructor(new[] {typeof (string)});object instance = ctor.Invoke(new object[] { _message });But it's not work.
Dublicator
@Dublicator: "It's not work" isn't exactly a detailed description of the problem. Please edit your question with a short but complete program which demonstrates the problem. I'll do the same in my answer with a short but complete program which *does* work.
Jon Skeet
Thanks for help. See comment below. I forgot about parameter innerException.
Dublicator
@Dublicator: But you don't *have* an inner exception, do you?
Jon Skeet
A: 

I use such code:

public static Exception Invoke(string soapMessage) {
    //...            
    string _message = doc.DocumentElement.InnerText;
    Type_type = Type.GetType(doc.DocumentElement.Attributes["Type"].Value);
    ConstructorInfo constructor = _type.GetConstructor(new[] {typeof (string), _type.GetType()});
    try {
        Exception ctor = (Exception)constructor.Invoke(new object[] { _message, _type });
    } catch (Exception ex) {
        return ex;
    }
    return null;
}
Dublicator
That doesn't look right to me - why are you passing in the type as an extra constructor argument? And why are you *returning* an exception *thrown* by invoking the constructor? See my answer for a *real* working answer. The reason this *appears* to work is that you're actually *causing* a NullReferenceException by asking for a constructor which doesn't exist, and then calling Invoke on that null reference. It won't work for other exception types.
Jon Skeet
You right, it's not work correctly. I use your method.
Dublicator