协议缓冲区反序列化和动态加载的 DLL [英] Protocol buffer deserialization and a dynamically loaded DLL

查看:65
本文介绍了协议缓冲区反序列化和动态加载的 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 protobuf-net 进行协议缓冲.我有一个动态加载的 dll.我可以创建包含在 dll 中的数据类的实例,并且可以使用和修改创建的数据对象.但是,当我尝试序列化/反序列化数据对象时,出现以下崩溃:

I am using protobuf-net for my protocol buffering. I have a dll I am loading dynamically. I can create an instance of a data class contained within the dll, and I can use and modify the created data object. However, When I attempt to serialize/deserialize the data object I get the following crash:

{无法识别 ProtoIncludeAttribute 的已知类型:MyDataDLL.MyDataClass、MyDataDLL、Version=0.0.0.0、Culture=neutral、PublicKeyToken=null"}

{"Unable to identify known-type for ProtoIncludeAttribute: MyDataDLL.MyDataClass, MyDataDLL, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"}

有时会崩溃,说 MyDataClass 不是 MyBaseClass 的子类,或者类似的东西.MyDataClass 肯定继承自 MyBaseClass,而 MyBaseClass 有一个用于 MyDataClass 的 ProtoInclude 标记.

and sometimes a crash saying that MyDataClass is not a subclass of MyBaseClass, or something like that. MyDataClass definitely inherits from MyBaseClass, and MyBaseClass has a ProtoInclude tag for MyDataClass .

Assembly theAssembly = Assembly.LoadFrom("MyDataDLL.dll");

Type theType = theAssembly.GetType("MyDataDLL.MyDataClass");

object theData = Activator.CreateInstance(theType);

using (FileStream theStream = File.Open(fileName, FileMode.OpenOrCreate))
{
    MethodInfo method = typeof(ProtoBuf.Serializer).GetMethod("Deserialize").MakeGenericMethod(theType);
    theData = method.Invoke(null, new object[] { theStream });                      
}

崩溃发生在method.invoke"

The crash happens on "method.invoke"

如果我在项目中引用 dll 并以这种方式使用它,我不会崩溃.所以我知道它是一个有效的 dll.

If I reference the dll in the project and use it that way, I don't get the crash. So I know it is a working dll.

更新:是的,MyDataClass 和 MyBaseClass 在同一个程序集中.

Update: Yes, MyDataClass and MyBaseClass are in the same assembly.

以下是我的代码与您的测试类的不同之处的列表,尽管它可能并不广泛:MyDataClass 是 7 个 ProtoIncludes 列表中的第 4 个.

Here's a list of what my code does differently then your test class, though it's probably not extensive: MyDataClass is the 4th in a list of 7 ProtoIncludes.

MyBaseClass 包含所有数据字段,MyDataClass 包含操作这些数据字段的函数的逻辑.所以在 MyDataClass 中没有 ProtoMember 调用.

MyBaseClass contains all the data fields, MyDataClass contains the logic for functions that manipulate these data fields. So there are no ProtoMember calls in MyDataClass.

MyBaseClass 实现了 IExtensible 接口并具有以下功能来处理附加数据:

MyBaseClass implements the IExtensible interface and has the following function to handle additional data:

private ProtoBuf.IExtension extensionObject;
ProtoBuf.IExtension ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)            {
    return ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing);
}

它可能不会影响任何东西,但我的 ProtoContracts 使用 Name 参数.

Its probably not affecting anything, but my ProtoContracts use the Name parameter.

推荐答案

好的,我会调查的.我已经在此处记录了此信息.应该很有趣;-p出于兴趣,MyDataClassMyBaseClass 是否在同一个程序集中?

OK, I will investigate. I've logged this here. Should be fun ;-p Out of interest, are MyDataClass and MyBaseClass in the same assembly?

顺便说一下;在下一滴中,我打算包括接受 Type(而不是泛型)的 Serialize 等 - 这将使进行中"的 RPC 堆栈更简单,并且会有所帮助您的使用情况也是如此.

By the way; in the next drop, I intend including Serialize etc that accept a Type (rather than generics) - this will make the "in progress" RPC stack much simpler, and will help your usage too.

更新;我添加了一个 单元测试 和(在单独的 dll 中,通过 Assembly.LoadFrom 加载 测试类).单元测试通过.请您澄清一下您的代码有何不同之处(我需要一些我可以重现的东西来修复它).

Update; I've added a unit-test and (in a separate dll, loaded via Assembly.LoadFrom test classes). The unit-test passed. Please can you clarify what your code does differently (I need something I can reproduce in order to fix it).

这篇关于协议缓冲区反序列化和动态加载的 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆