我正在使用 .NET 智能卡,它具有与 .NET 远程处理相同的概念。
所以我的智能卡(作为服务器)有这个服务:
public class MyService : MarshalByRefObject
{
string a = "abC";
public byte[] MySampleMethod()
{
MyService obj = new MyService();
return help.ObjectToByteArray( obj);
}}}
这是 ObjectToByteArray(obj)
public static byte[] ObjectToByteArray(MyService obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(0);
bf.Serialize(ms, obj);
return ms.ToArray();
}
至于客户:
public static void Main()
{
// create and register communication channel
APDUClientChannel channel = new APDUClientChannel();
ChannelServices.RegisterChannel(channel);
// get the referenc to remote object
MyService service = (MyService)Activator.GetObject(typeof(MyService), URL);
// invoke the remote method
byte[] result = service.MySampleMethod();
MyService obj = ByteArrayToObject(result);
Console.WriteLine(result[0]);
Console.ReadLine();
// unregister the communication channel
ChannelServices.UnregisterChannel(channel);
}
字节数组对象
public static MyService ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream(0);
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
//memStream.Position = 0;
MyService obj = (MyService)binForm.Deserialize(memStream);
return obj;
}
问题是当我想反序列化对象时。
我测试这个字符串 "ABCDE" ,在卡片中序列化它,结果十六进制是:
1C-5D-D2-00-27-11-02-00-00-00-05-00-00-00-05-00-00-00-01-41-00-42-00-43-00- 44-00-45-00
当我在我的电脑上序列化它时的结果是:
00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-00-06-01-00-00-00-05-41-42- 43-44-45-0B。
所以在我的 PC 应用程序上,反序列化第二个效果很好,但是当我反序列化第一个字符串(来自智能卡)时,我得到:
“输入流不是有效的二进制格式。起始内容(以字节为单位)为:1C-5D-D2-00-27-11-02-00-00-00-05-00-00-00-05- 00-00 ...”