0

我正在使用由 wsdl.exe 生成的示例 SOAP 代码。对象lastError声明如下:

private Exception lastError;

Visual Studio 在此行上生成错误

String msg = lastError.Message;

'Exception' does not contain a definition for 'Message' and no extension method 'Message' accepting a first argument of type 'Exception' could be found (are you missing a using directive or an assembly reference?)

Exceptionwsdl.exe 生成的类如下所示:

/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(NestedException))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(PersistenceException))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(BbSecurityException))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://gradebook.ws.blackboard")]
public partial class Exception {

    private object exception1Field;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Exception", IsNullable=true)]
    public object Exception1 {
        get {
            return this.exception1Field;
        }
        set {
            this.exception1Field = value;
        }
    }
}
4

1 回答 1

0

partial类不会通过共享名称自动扩展任何内置或非部分类。如果您希望上述Exception类扩展System.Exception,那么最简单的方法是添加另一个分部类并显式扩展它:

(在其他文件中)

public partial class Exception : System.Exception
{

}

您应该知道Exception虽然有一个类命名的问题。从技术上讲,您不应该真正捕获 generic Exception,但是如果您的命名空间中有类似的东西,您可能不会捕获您认为的异常:

public void SomeMethod()
{
    try
    {
        DoSomethingThatExcepts();
    }
    catch (Exception e)
    {
        //You are actually catching the defined Exception, not System.Exception
    }
}

因此,在您使用System.Exception的任何地方,您都可能必须完全限定名称。

于 2015-12-15T20:56:35.390 回答