在C#中删除继承对象的属性

Ter*_*rry 2 c# oop serialization

如果我有一个复杂的对象,我可以继承它并删除忽略某些属性吗?

如果您不在乎我为什么要这样做,请随时提交答案.

如果你关心,你可以阅读这个问题.总结一下,我有一个对象具有一个不可序列化的属性,我想序列化整个对象.有问题的对象是System.Exception

为了解决这个问题,我想简单地创建自己的JsonException对象,继承base(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()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

另外请记住,我坚持使用.NET 2.0,如果我不需要,我真的不想使用自定义序列化程序(如Json.NET).

Seb*_*Piu 5

这里的"定义异常类"的段落,看看如何创建一个自定义序列化的例外.

文章的相关部分:

[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) { }
}
Run Code Online (Sandbox Code Playgroud)