我正在将 protobuf-net v2 beta r431 用于 C# .net 4.0 应用程序。在我的应用程序中Dictionary<int, IMyClass>
,我需要序列化一个。一个类MyClass
实现IMyClass
接口。根据 protobuf 的文档,我编写的代码如下:
[ProtoContract]
[ProtoInclude(1, typeof(MyClass))]
public interface IMyClass
{
int GetId();
string GetName();
}
[ProtoContract]
[Serializable]
public class MyClass : IMyClass
{
[ProtoMember(1)]
private int m_id = 0;
[ProtoMember(2)]
private string m_name = string.Empty;
public MyClass(int id, string name)
{
m_id = id;
m_name = name;
}
public MyClass()
{
}
#region IMyClass Members
public int GetId()
{
return m_id;
}
public string GetName()
{
return m_name;
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
但是,根据我的应用程序的设计,接口是在更高级别(在与类不同的项目中)定义的,并且无法在编译时确定实现此接口的类。因此,它给出了 [ProtoInclude(1, typeof(MyClass))] 的编译时错误。我尝试使用 [ProtoInclude(int tag, string KownTypeName)] 如下:
[ProtoContract]
[ProtoInclude(1, "MyClass")]
public interface IMyClass
{
int GetId();
string GetName();
}
Run Code Online (Sandbox Code Playgroud)
但是,这在行引发了“对象引用未设置为对象的实例”异常
Serializer.Serialize(stream, myDict);
Run Code Online (Sandbox Code Playgroud)
其中 Dictionary myDict = new Dictionary(int, IMyClass)(); 请让我知道在这种情况下如何使用 ProtoInclude 以便在字典/列表中序列化接口类型。
由于它不知道从哪里获取您的MyClass
,您可能应该为您的类使用Type.AssemblyQualifiedName值。
下面是一些示例代码:
namespace Alpha
{
[ProtoContract]
[ProtoInclude(1, "Bravo.Implementation, BravoAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")]
//[ProtoInclude(1, "Bravo.Implementation")] // this likely only works because they're in the same file
public class PublicInterface
{
}
}
namespace Bravo
{
public class Implementation : Alpha.PublicInterface
{
}
public class Tests
{
[Test]
public void X()
{
// no real tests; just testing that it runs without exceptions
Console.WriteLine(typeof(Implementation).AssemblyQualifiedName);
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, new Implementation());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
奥斯汀是正确的(我相信):使用程序集限定名称(作为字符串)应该可以解决这个问题。
在 v2 中,存在一个附加选项:您可以在运行时而不是通过属性执行映射:
RuntimeTypeModel.Default[typeof(PulicInterface)]
.AddSubType(1, typeof(Implementation));
Run Code Online (Sandbox Code Playgroud)
如果您的“应用程序”层知道这两种类型,则这可以通过静态代码来完成,或者可以通过一些自定义配置/反射过程来完成。