uni*_*uni 38 c# xml-serialization xmlserializer
我有一个调用记录器,用于记录所有方法调用以及与使用XmlSerializer的方法相关的参数.它适用于大多数调用,但它会为具有IEnumerable
类型参数的所有方法抛出异常.
例如,void MethodWithPlace( Place value )
将序列化,但void MethodWithPlace( IEnumerable<Place> value )
不会.
例外是
System.NotSupportedException:无法序列化接口System.Collections.Generic.IEnumerable`1 [[Place,Test,Version = 0.0.0.0,Culture = neutral]].
我应该怎么做才能使用这些方法IEnumerable
作为其参数之一?
Her*_*eld 31
序列化IEnumerable属性的方法是使用代理属性
[XmlRoot]
public class Entity {
[XmlIgnore]
public IEnumerable<Foo> Foo { get; set; }
[XmlElement, Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public List<Foo> FooSurrogate { get { return Foo.ToList(); } set { Foo = value; } }
}
Run Code Online (Sandbox Code Playgroud)
这很丑陋,但它完成了工作.更好的解决方案是编写代理类(即EntitySurrogate).
Kyl*_*e W 11
基本上,XmlSerializer无法序列化接口.然后,解决方案是给它一个序列化的具体实例.根据您的调用记录器的工作方式,我会考虑使用
var serializer = new XmlSerializer(value.GetType());
Run Code Online (Sandbox Code Playgroud)
我不认为你能够序列化它.尝试将IEnumerable转换为List,然后您就可以序列化了.
归档时间: |
|
查看次数: |
33752 次 |
最近记录: |