我可以动态地应用XmlIgnore到ArrayItem的属性?

编程入门 行业动态 更新时间:2024-10-20 13:35:30
本文介绍了我可以动态地应用XmlIgnore到ArrayItem的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想序列化列表< T>其中T:EntityObject,很想离开了所有的EntityKey和其他的EntityReference属性从列表中的项目。可以这样做动态可能使用XmlAttributeOverrides?

据我所看到的,XmlAttributeOverrides选项只有真正指向即名单,其中顶层的对象; T>而不是牛逼自己,这是不是对我很有帮助。 任何人都可以点我的方式来动态地忽略ArrayItems的属性?

下面是一个简单的例子,我一直在使用,不使用EntityObjects但应该说明我想要做的:

公共类车     {         公共字符串制作{获得;组; }         公共字符串型号{获得;组; }         公共双EngineSize {获得;组; }     }     [测试]     公共无效WouldLoveToDynamicallyLeaveOutMembersOfArrayItems()     {         VAR汽车=新的名单,其中,汽车及GT;                        {                          新汽车                              {                                  请=法拉利,                                  模型=F1,                                  EngineSize = 6000                              },                          新汽车                              {                                  请=威廉姆斯                                  模型=F1,                                  EngineSize = 5500                              }                      };         VAR attributeOverrides =新XmlAttributeOverrides();         attributeOverrides.Add(typeof运算(双人间),EngineSize,新XMLATTRIBUTES {XmlIgnore = TRUE});         VAR XS =新的XmlSerializer(cars.GetType(),attributeOverrides,新的[] {typeof运算(车)},新XmlRootAttribute(汽车总动员),);         VAR毫秒=新的MemoryStream();         xs.Serialize(MS,汽车);         ms.Position = 0;         VAR SR =新的StreamReader(MS);         VAR的结果= sr.ReadToEnd();         Assert.IsFalse(result.Contains(EngineSize));     }

解决方案

是的,你可以做到这一点 - 主要错误是你需要的,而不是typeof运算(双)typeof运算(车)。与此,请注意,XmlAttributeOverrides的的不的只是应用于顶层onject

不过,我不知道这是最简单的途径。首先,请注意您的的必须的存储和再利用使用XmlAttributeOverrides时,否则你会泄漏组件的串行器。

我的期望的,你想这样做的主要原因是因为你不想编辑自己生成的文件。然而,还有另一种方式;在一个单独的文件,在正确的命名空间,你可以使用:

部分类车{     公共BOOL ShouldSerializeEngineSize(){返回false; } }

在哪里ShouldSerialize *是识别和使用XmlSerializer的控制有条件的序列化模式。在部分类只是将两个独立的code文件合并成一个单一类型的一种方式(并为此设计的生成内容的情况)。

这样,你不需要惹XmlAttributeOverrides,并且可以使用更简单的新XmlSeralizer(类型)(其中有每型自动缓存)。

I am trying to serialize a List<T> where T: EntityObject and would love to leave out all the EntityKey and other EntityReference properties from the items in the list. Can this be done dynamically possibly using XmlAttributeOverrides?

As far as I can see, the XmlAttributeOverrides options only really point to the top level object ie the List<T> and not the T themselves, which is not very helpful to me. Could anyone point me to a way to dynamically ignore properties of ArrayItems?

Here is a simple example I have been using that does not use EntityObjects but it should illustrate what I would like to do:

public class Car { public String Make { get; set; } public String Model { get; set; } public Double EngineSize { get; set; } } [Test] public void WouldLoveToDynamicallyLeaveOutMembersOfArrayItems() { var cars = new List<Car> { new Car { Make = "Ferrari", Model = "F1", EngineSize = 6000 }, new Car { Make = "Williams", Model = "F1", EngineSize = 5500 } }; var attributeOverrides = new XmlAttributeOverrides(); attributeOverrides.Add(typeof(Double), "EngineSize", new XmlAttributes {XmlIgnore = true}); var xs = new XmlSerializer(cars.GetType(), attributeOverrides, new []{ typeof(Car) }, new XmlRootAttribute("cars"), ""); var ms = new MemoryStream(); xs.Serialize(ms, cars); ms.Position = 0; var sr = new StreamReader(ms); var result = sr.ReadToEnd(); Assert.IsFalse(result.Contains("EngineSize")); }

解决方案

Yes you can do that - the main error is you need typeof(Car) instead of typeof(double). With this, note that XmlAttributeOverrides does not just apply to the top-level onject.

However, I'm not sure that is the easiest route. Firstly, note that you must store and re-use the serialiser when using XmlAttributeOverrides otherwise you will leak assemblies.

I expect the main reason you want to do this is because you don't want to edit he generated file. However, there is another way; in a separate file, in the right namespace, you can use:

partial class Car { public bool ShouldSerializeEngineSize() { return false; } }

Where "ShouldSerialize*" is a pattern recognised and used by XmlSerializer to control conditional serialization. The "partial" class is simply a way of combining two separate code files into a single type (and was designed for this generated-content scenario).

This way, you dont need to mess with XmlAttributeOverrides, and you can use the simpler "new XmlSeralizer(Type)" (which has automatic caching per-type).

更多推荐

我可以动态地应用XmlIgnore到ArrayItem的属性?

本文发布于:2023-11-11 22:57:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   动态   XmlIgnore   ArrayItem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!