gpt4 book ai didi

c# - 如何删除 DataContractSerializer 冗长

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:16 24 4
gpt4 key购买 nike

我们正在尝试序列化一个对象树。虽然我们已经成功了。我希望找到一种方法来简化生成的 xml。

对象看起来像下面这样:

public class RuleSet<T>
{
public IEnumerable<IState<T>> States { get; set; }
public IEnumerable<ICondition<T>> Conditions { get; set; }
}

public class State<T> : IState<T>
{
public string Id { get; set; }
public List<ITransition<T>> Transitions { get; set; }
}

public class Transition<T> : ITransition<T>
{
public ICondition<T> Condition { get; set; }
public IState<T> Next { get; set; }
}

public class Condition<T> : ICondition<T>
{
public string Id { get; set; }
public string Name { get; set; }
}

目前我们正在使用一个非常简单的序列化代码:

public void blah()
{
var condition1 = new Condition<object>() {
Id = "C1", AttributeName = "Foo", ExpectedValue = "Bar"
};
var condition2 = new Condition<object>() {
Id = "C2", AttributeName = "Bar", ExpectedValue = "Foo"
};

var state1Transitions = new List<ITransition<object>>();
var state2Transitions = new List<ITransition<object>>();
var state3Transitions = new List<ITransition<object>>();

var state = new State<object> {
Id = "S1", Transitions = state1Transitions
};
var state2 = new State<object> {
Id = "S2", Transitions = state2Transitions
};
var state3 = new State<object> {
Id = "S3", Transitions = state3Transitions
};

state1Transitions.Add(new Transition<object> {
Condition = condition1, Next = state2
});
state1Transitions.Add(new Transition<object> {
Condition = condition2, Next = state3
});
state2Transitions.Add(new Transition<object> {
Condition = condition2, Next = state3
});
var ruleSet = new RuleSet<object> {
States = new List<IState<object>> {state, state2, state3},
Conditions = new List<ICondition<object>>{condition1, condition2}
};

var stream1 = new MemoryStream();

var serializer = new DataContractSerializer(typeof(RuleSet<object>),
new List<Type> {
typeof(State<object>),
typeof(Transition<object>),
typeof(AttributeEqualTo<object>)
});

serializer.WriteObject(stream1, ruleSet);
stream1.Position = 0;

var xml = new StreamReader(stream1).ReadToEnd();
Console.WriteLine(xml);
}

当生成 XML 时,每个级别的输出都是完整的,而不是仅包含对对象的引用。基本上每个Transition<T>我们得到每个状态和条件的完整对象定义,即使它们是在别处定义的。

有没有办法让这些只是引用?

最佳答案

这一切都取决于您如何创建 DataContractSerializer .

您需要调用 the overload of the constructor that allows you to indicate that you will preserve object references ,具有以下签名:

public DataContractSerializer(
Type type,
IEnumerable<Type> knownTypes,
int maxItemsInObjectGraph,
bool ignoreExtensionDataObject,
bool preserveObjectReferences,
IDataContractSurrogate dataContractSurrogate
)

您可以为大多数参数传递默认值。在您的情况下,对 DataContractSerializer 构造函数的调用将如下所示:

var serializer = new DataContractSerializer(typeof(RuleSet<object>),
new [] {
typeof(State<object>),
typeof(Transition<object>),
typeof(AttributeEqualTo<object>)
},
Int32.MaxValue,
false,
/* This is the important flag to set. */
true,
null
);

请注意,从 preserveObjectReferences 参数文档中,它使用非标准 XML(强调我的):

preserveObjectReferences

Type: System.Boolean

true to use non-standard XML constructs to preserve object reference data; otherwise, false.

如果您需要 .NET 之外的其他代码来解释它,那么理清这些引用可能会很困难(但不应该是不可能的)。

但是,它会阻止对象图 self 复制并减少 XML 的大小(考虑到您的引用的深度,可能会大大减少)。

关于c# - 如何删除 DataContractSerializer 冗长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14324394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com