上下文 Xml 序列化
有什么方法可以控制 ex 的 xml 序列化吗?检查根/父对象中的属性。
我有:
class ClassA
{
public int SomeAIntProp {get; set; }
ClassB OtherClass;
}
class ClassB
{
public int SomeBIntProp {get; set; }
}
这就是我想要的:
public bool ShouldSerializeSomeBIntProp(context)
{
return (context is ClassA) && ((ClassA)context).SomeAIntProp >10;
}
}
Is there any way to control xml serialization for ex. checking properties in root/parent object.
I have:
class ClassA
{
public int SomeAIntProp {get; set; }
ClassB OtherClass;
}
class ClassB
{
public int SomeBIntProp {get; set; }
}
This is what I want:
public bool ShouldSerializeSomeBIntProp(context)
{
return (context is ClassA) && ((ClassA)context).SomeAIntProp >10;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不在任何标准/内置序列化器中,不。所有逻辑都必须与正在序列化的实例相关(当然,您可以遍历任何已存在的导航属性 -
.Parent
等 - 尽管需要标记[XmlIgnore]< /代码>)。唯一的其他选择是实现 IXmlSerialized 并手动完成所有操作,但我强烈建议不要这样做。
Not in any of the standard/inbuilt serializers, no. All of the logic must be relative to the instance being serialised (you can of course walk any navigation properties that already exist -
.Parent
etc - although that would need to be marked[XmlIgnore]
). The only other option is to implementIXmlSerializable
and do it all manually, but I strongly advise against that.不要在序列化中执行此操作,而只需向类中添加一个函数,该函数首先验证要保存的内容,然后调用具体的序列化函数。
如果您有多个对象必须合并并保存在单个文件中,那么还有一些针对参与序列化的不同类型对象的交叉验证,您可以或实现 SerializationAdapter (您的自定义类必须组合、验证和序列化感兴趣的对象),或使用责任链,如果在您的情况下可能的话,您有一组函数抛出,您的对象必须传递并验证这些函数,以及一些最终函数,用于验证您需要的所有对象上的所有函数的最终结果。或多或少就像您的对象是患者并且验证功能是医生,最后您收到一张收据,该收据基于每一次分析,报告您的最终结果。
希望这有帮助。
Do not do it in
Serialization
, but simply add to your class a function that first validate the content to be saved and after calls concrete serialization function.If you have more then one object that have to be combined and saved in single file, so also some cross validation against different type of objects that participate in serialization, you can or implement SerializationAdapter ( that your custom class has to combine, validate and serialize objects of interest), or use Chain of Responsibility, if it's possible in your case, where you have a set of functions throw which your objects have to pass and be validated, and some final function that validates final result of all functions on all objects you need. More or less like if your object(s) are patient(s) and validation functions are doctors, and finally you got a receipt, that based on every single analyse, reports you final result.
Hope this helps.