【发布时间】:2012-07-24 20:45:14
【问题描述】:
我在紧凑框架 (3.5) 上反序列化数千个对象,而且速度很慢。用设备 20 多秒完成。我发现它是通过反射完成的,而不是像非紧凑型对应物那样编译和运行。 所以我想,我可以先预编译生成一个类型模型dll吗?
所以我做了以下事情:
- 将所有 Contract 类提取到智能设备 dll(它引用 Protobuf-net CF3.5 Dll)
-
创建一个桌面 3.5 控制台应用程序,引用 Protobuf-net "Desktop" Dll 和上面创建的 Contract Dll。
class Program { static void Main(string[] args) { var bb = TypeModel.Create(); foreach (var t in Assembly.GetAssembly(typeof(My.ContractX)).GetTypes()) { var contract = t.GetCustomAttributes(typeof (ProtoBuf.ProtoContractAttribute), false); if (contract.Length > 0) { bb.Add(t, true); } } bb.Compile("My.TypeModel", "My.Serialization.dll"); } } - 回到设备项目,引用 Contract DLL、生成的 My.Serialization.dll 和 Protobuf-net CF3.5 Dll。
- 不使用默认模型,而是将其修改为使用“new TypeModel()”构造的模型进行反序列化
它实际上编译正确。我在Reflector中查看了生成的dll,和预期的一样。
除了在运行时,它会抛出 MissingMethodException。然而,缺少的正是缺少的东西,因为紧凑的框架没有报告这一点。
我打赌是因为生成的 My.Serialization.dll 实际上是指“桌面”dll,但缺少某些方法。
回到我的问题,我怎样才能实现类型模型预生成以在紧凑框架中使用?或者我可以通过做其他事情来提高性能吗?
【问题讨论】:
标签: compact-framework deserialization protocol-buffers protobuf-net