Protocol buffer deserialization and a dynamically

2019-05-21 00:36发布

问题:

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:

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

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 });                      
}

The crash happens on "method.invoke"

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.


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

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 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 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);
}

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

回答1:

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

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.


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).