这是我的代码:
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/