Web服务具有SoapExtension,其中包含一个错误处理程序和以xml格式序列化错误。

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


如何制作错误处理程序,调用“类型”错误?例如。:

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


它必须调用NullReferenceException。

最佳答案

您需要提供标准名称,即“ System.NullReferenceException”。

在这种情况下,这就足够了-因为NullReferenceException在mscorlib中。但是,如果您需要其他异常(例如,位于System.Data程序集中的ConstraintException),则也需要提供完整的程序集名称。 Type.GetType(string)仅在当前执行的程序集和mscorlib中查找未指定程序集的类型名称。

编辑:这是一个简短但完整的程序,可以正常工作:

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

10-01 23:33
查看更多