是否应该支持序列化 PropertyInfo?如果没有,最好的解决方法是什么?我知道可以序列化类型(受支持),然后通过属性查找属性 - 想象一下查找可能很昂贵。
ps PropertyInfo 用于实现类似于 DataLoadOptions.LoadWith http://msdn.microsoft.com/en-us/library/bb548760.aspx的功能。
using System.IO;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ProtoBuf;
[ProtoContract]
public class A
{
[ProtoMember(1)]
public PropertyInfo Property { get; set; }
}
[TestClass]
public class UnitTest
{
[TestMethod]
public void TestMethod1()
{
var value = new A { Property = typeof(string).GetProperty("Length") };
using (var stream = new MemoryStream())
{
// Raises InvalidOperationException
// "No serializer defined for type: System.Reflection.PropertyInfo"
Serializer.Serialize(stream, value);
}
}
}
InvalidOperationException
引发时调用堆栈:
protobuf-net.dll!ProtoBuf.Meta.ValueMember.BuildSerializer() Line 316 C#
protobuf-net.dll!ProtoBuf.Meta.ValueMember.Serializer.get() Line 188 + 0x24 bytes C#
protobuf-net.dll!ProtoBuf.Meta.MetaType.BuildSerializer() Line 421 + 0x1f bytes C#
protobuf-net.dll!ProtoBuf.Meta.MetaType.Serializer.get() Line 344 + 0xe bytes C#
protobuf-net.dll!ProtoBuf.Meta.RuntimeTypeModel.Serialize(int key, object value, ProtoBuf.ProtoWriter dest) Line 592 + 0x44 bytes C#
protobuf-net.dll!ProtoBuf.Meta.TypeModel.SerializeCore(ProtoBuf.ProtoWriter writer, object value) Line 178 + 0x14 bytes C#
protobuf-net.dll!ProtoBuf.Meta.TypeModel.Serialize(System.IO.Stream dest, object value, ProtoBuf.SerializationContext context) Line 209 + 0xe bytes C#
protobuf-net.dll!ProtoBuf.Meta.TypeModel.Serialize(System.IO.Stream dest, object value) Line 193 + 0x10 bytes C#
protobuf-net.dll!ProtoBuf.Serializer.Serialize<TestProtoBufNet.A>(System.IO.Stream destination, TestProtoBufNet.A instance) Line 88 + 0x18 bytes C#
TestProtoBufNet.dll!TestProtoBufNet.UnitTest.TestMethod1() Line 24 + 0x10 bytes C#