![](/img/trans.png)
[英]Extending an entity in entity framework through partial class, stackoverflow exception
[英]Extending Exception partial class
我正在使用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?)
:
wsdl.exe生成的Exception
类如下所示:
/// <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;
}
}
}
partial
类不会凭借共享名自动扩展任何内置或非局部类。 如果要上述Exception
类扩展System.Exception
,那么最简单的方法是添加另一个局部类并显式扩展它:
(在其他文件中)
public partial class Exception : System.Exception
{
}
但是,您应该注意具有名为Exception
的类的问题。 从技术上讲,您实际上并不应该捕获通用的Exception
,但是,如果您的命名空间中有类似的东西,那么您可能不会捕获您认为自己是的异常:
public void SomeMethod()
{
try
{
DoSomethingThatExcepts();
}
catch (Exception e)
{
//You are actually catching the defined Exception, not System.Exception
}
}
因此,在任何使用System.Exception
地方,都可能必须完全限定该名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.