【发布时间】:2014-06-25 09:09:24
【问题描述】:
这是我的代码:
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;
它按预期工作......
还值得注意的是,如果在添加类而不是尝试使用 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! 手指交叉
感谢大家的帮助!
【问题讨论】:
-
这可能与您的问题无关,但据我了解,
theOtherInt将在基类中影响theInt。如果这是实际类的代表,您可能需要更改派生类的成员索引,以使它们不会与基类的成员索引发生冲突。 -
我找不到任何明确说明这一点的地方,但它确实是有道理的。也许@MarcGravell 可以验证。
-
我的印象是索引只需要每个类库都是唯一的,但如果你是正确的,那是我的代码中的一个大错误,我将更新我的实际代码以反映你的建议注意安全。谢谢!
标签: c# serialization dll unity3d protobuf-net