小弟最近在研究利用Marshal实现序列化和反系列化,主要是使用Marshal类里的两个方法,StructureToPtr和PtrToStructure,这两个类的具体讲解和用法我就不多赘述 了,具体见[url=http://blog.csdn.net/wem520/article/details/9101549][/url],下面说我碰到的问题,我的需求是对一个类的对象通过StructureToPtr序列化成byte[]数组,然后通过TCP Socket传输到另外一个解决方案,再用PtrToStructure反序列化出来这个对象。现在的问题是类的字段类型问题,就是我一个属性是string类型,其余的都是int类型。测试结果发现如果在同一个API里,这个方法能完美实现,但是经过传输再反序列化后int类型可以反序列化出来而string字段就为空或者乱码。小弟百思不得其解,特来求高手帮忙分析解疑。具体代码如下:
API直接序列化反序列化
[code=csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.name = "joseph"