0

我想将树序列化为人类可读的文本格式(所需的存储空间越少越好)。我想在不实现 XmlSerializer 类所要求的无参数构造函数和公共设置器的情况下实现这一点。实现这一目标的标准方法是什么?

我的模型看起来像这样:

public interface ISpecification<T>
{ //some stuff here }


// The tree will have some nodes like this
public abstract class CompositeSpecificationBase<T>
{
    public ISpecification<T> Left { get; private set; }

    public ISpecification<T> Right { get; private set; }

    protected CompositeSpecificationBase(ISpecification<T> left, ISpecification<T> right)
    {
        Left = left;
        Right = right;
    }
}

public class AndSpecification<T> : CompositeSpecificationBase<T>, ISpecification<T>
{
    public AndSpecification(ISpecification<T> left, ISpecification<T> right)
        : base(left, right)
    {} 
    // some stuff here
}

// At least one more implementation of CompositeSpecificationBase...

// There will be many leaf types like this
public class ExampleSpec : ISpecification<SomeEntity>
{
    public int SomeProperty {get; private set;}

    public ExampleSpec (int someProperty)
    { 
        SomeProperty = someProperty;
    }
}
4

1 回答 1

2

我建议使用 Json,例如使用 Json.NET 序列化程序:http: //json.codeplex.com/

于 2012-08-14T14:03:19.050 回答