编译DLL字符串[]导致DLL损坏

本文关键字:DLL 导致 损坏 字符串 编译 | 更新日期: 2023-09-27 18:02:22

下面是我的代码:

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; 

It works as expected…

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

我环境:

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

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

编辑

根据Flamy的建议,我将字符串[]改为列表

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

遗憾的是错误仍然存在。

另一份报告

我还决定使用dll反编译器来看看发生了什么。我无法反编译dll,直到"string[] items"变量被删除。

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

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

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

现在我需要做的就是用我的大型项目创建dll !祈祷

感谢大家的帮助!

编译DLL字符串[]导致DLL损坏

你的参考文献是否正确?%项目dir % '图书馆' ScriptAssemblies ' Assembly-CSharp.dll

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

使用二进制序列化或使用二进制格式的序列化方法(本例中为protobuff)序列化字符串数组总是会导致许多问题。原因是String本身是一个字符数组,它没有定义的大小,这意味着它不知道一个字符串在哪里结束,下一个字符串从哪里开始…通常我们逐个序列化字符串数组,而不是整个数组。所以我建议你使用一个属性(希望protobuff在属性上接受属性!)或创建一个字符串列表代替(虽然我还没有测试它应该工作!)