如果我有一个复杂的对象,我可以继承它并remove or ignore某些属性?
如果你不在乎why我想这样做,请随意提交答案。
如果您关心的话,可以阅读这个问题 https://stackoverflow.com/questions/8607862/jquery-deferred-ajax-500-server-errors。总而言之,我有一个对象,它有一个属性,即not可序列化,并且我想序列化整个对象。有问题的对象是System.Exception
为了解决这个问题,我想简单地创建自己的JsonException
对象,继承基础的所有可爱的属性(System.Exception
)除了移除(或清空)丑小鸭(Exception.TargetSite
).
就像是:
public class MyException : Exception
{
// Note this is not actually possible
// just demonstrating what I thought to do in theory
public override System.Reflection.MethodBase TargetSite
{
get
{
return null;
}
}
public MyException(string message)
: base()
{
}
}
另请记住,我一直使用 .NET 2.0,并且真的不想使用自定义序列化器(例如Json.NET http://json.codeplex.com/)如果我不需要的话。
See here http://msdn.microsoft.com/en-us/library/ms173163.aspx在“定义异常类”段落中了解如何创建自定义可序列化异常。
文章的相关部分:
[Serializable()]
public class InvalidDepartmentException : System.Exception
{
public InvalidDepartmentException() : base() { }
public InvalidDepartmentException(string message) : base(message) { }
public InvalidDepartmentException(string message, System.Exception inner) : base(message, inner) { }
// A constructor is needed for serialization when an
// exception propagates from a remoting server to the client.
protected InvalidDepartmentException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) { }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)