c# - Protobuf-net 编译为 DLL 字符串 [] 导致损坏的 dll

标签 c# serialization dll unity3d protobuf-net

这是我的代码:

using ProtoBuf;

[ProtoContract]
[ProtoInclude(500, typeof(SampleClassDrv))]
public class SampleClass
{
    [ProtoMember(1)] public int theInt;
    [ProtoMember(2)] public string[] items;
    public SampleClass(){}
    public SampleClass(int c) {this.theInt = c;}
}

[ProtoContract]
public class SampleClassDrv : SampleClass
{
    [ProtoMember(1)] public int theOtherInt;
    public SampleClassDrv(){}
    public SampleClassDrv(int b):base(1){this.theOtherInt=b;}
}

为了编译我的 DLL,我运行以下代码:

RuntimeTypeModel rModel = TypeModel.Create();
rModel.AllowParseableTypes = true;
rModel.AutoAddMissingTypes = true;

rModel.Add(typeof(SampleClass), true);
rModel.Add(typeof(SampleClassDrv), true);
rModel.Compile("MySerializer", "MySerializer.dll");

最后我应该能够像这样通过 dll 中的 RuntimeTypeModel 进行初始化:

MySerializer serializer = new MySerializer();
serializer.Serialize(stream, object);

但是Unity抛出如下异常

Internal compiler error. See the console log for more information.
[...]
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

有趣的是,如果我返回并删除该行

[ProtoMember(2)] public string[] items; 

它按预期工作......

还值得注意的是,如果在添加类之后使用 RunTimeModel 而不是尝试使用 dll,则 RunTimeModel 会按预期工作。

我的环境:

  • Unity3D 4.3.4
  • Protobuf-net r668
  • 在 Full/unity 中使用 protbuf-net.dll

如果有人能指出我方法中的错误,我将不胜感激。

编辑

根据 Flamy 的建议,我将 string[] 的形式更改为 List

[ProtoMember(2)] public List<string> items;

很遗憾,错误仍然存​​在。

另一个注意事项

我还决定使用一个 dll 反编译器来查看发生了什么。在删除“string[] items”变量之前,我无法反编译 dll。

已解决

我认为它与使用 Unity3D 编译 DLL 的一些问题有关。

当我使用上面显示的代码在 Visual Studios 中创建项目时,一切似乎都按预期工作。这是一种解脱,因为如果 protobuf 无法序列化 string[],这似乎将是一个大问题。

我按照 BCCode 提供的文章设置了 visual studio 项目并编译了 DLL。

现在我需要做的就是用我的大型项目创建 dll! 双手合十

感谢大家的帮助!

最佳答案

您的引用资料是否正确? %project dir%\Library\ScriptAssemblies\Assembly-CSharp.dll

另请看这里: http://purdyjotut.blogspot.com/2013/10/using-protobuf-in-unity3d.html?m=1

关于c# - Protobuf-net 编译为 DLL 字符串 [] 导致损坏的 dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23533913/

相关文章:

c++ - 将 boost 序列化集成到现有代码中

java - 用 jackson 序列化日期列表

c# - 无法加载文件或程序集 'Leadtools.Codecs',如果有人熟悉 leadtools

c++ - 使用绝对路径加载带有 LoadLibrary() 和 GetModuleHandle() 的系统 DLL 是否能更好地防止 DLL 劫持?

c++ - DLL监控

c# 并从内存中删除敏感数据和所有垃圾收集副本

c# - Excel 无法识别来自 closedXML/c# 的公式

c++ - 当对字段使用不同的值时,Protobuf 序列化长度不同

C# 智能感知库

c# - 当我们只知道窗口句柄时如何获取窗口图像