我们正在使用 unity3d(monotouch) 引擎 (C#) 开发跨平台在线游戏,并且我们使用 protobuf-net(感谢 Marc Gravell)作为协议。众所周知,反射在 iOS 上不起作用。Apple 不允许动态生成代码。我们查看了 Marc 的博客(http://marcgravell.blogspot.com/),发现早期的 protobuf-net v2(虽然还没有完成)可以避免反射问题。
我们做了一些测试,并尝试将序列化代码预编译为 dll 文件,但是,当我们在 ios 上运行它时,我们收到以下消息。
>
System.IO.FileNotFoundException has been thrown
> “Could not load file or assembly “DataBuilder , Version=0.0.0.0 ,
> Culture = neutrual , PublicKeyToken =
> null ”” or one of its dependencies.”
这是我们编译的dll代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;
using ProtoBuf.Meta;
using Data;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var bb = TypeModel.Create();
bb.Add(typeof(Customer), true);
bb.Compile("DataBuilder", "databuilder.dll");
}
}
}
这是我们的测试代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data;
using System.IO;
using ProtoBuf;
using ProtoBuf.Meta;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var creater = new DataBuilder(); // that's it
var dd = new Data.Customer();
dd.Name = "Hellow World";
dd.CustomerId = "1232";
MemoryStream mm = new MemoryStream();
creater.Serialize(mm, dd);
mm.Seek(0, SeekOrigin.Begin);
Data.Customer c = (Data.Customer)creater.Deserialize(mm, null, typeof(Data.Customer));
Console.WriteLine(c.Name);
}
}
}
我们在 Windows 环境下编译了 dll(compact framework .net 3.5),也许它在 monotouch 上不起作用。