110,568
社区成员
发帖
与我相关
我的任务
分享
struct Student
{
char* name;
float height;
School* pSchool;
}
struct School
{
char* name;
Student* pStudent;
}
[StructLayout(LayoutKind.Sequential,Pack=1,CharSet=CharSet.Ansi)]
class Student
{
string name;
float height;
IntPtr school;
}
unsafe class School
{
public School() { }
public School(IntPtr pSchool) { this.pSchool = pSchool; }
public IntPtr pSchool;//指向非托管内存的数据
//省略
}
unsafe class Student
{
public Student() { }
public Student(IntPtr pStudent) { this.pStudent = pStudent; }
public IntPtr pStudent;//指向非托管内存的数据
#region 指向非托管 数据结构 各成员的指针
private char* * pName
{
get { return (char**)((Int32)pStudent + 0); }
}
private float * pHeight
{
get { return (float*)((Int32)pStudent + 4); }
}
private void* * pSchool
{
get { return (void**)((Int32)pStudent + 8); }
}
#endregion
#region 获取 非托管内存 中各成员数据
public string Name
{
get { return Marshal.PtrToStringAnsi((IntPtr)(*pName)); }
set { *pName = (char*)Marshal.StringToHGlobalAnsi(value); }
}
public float Height
{
get { return *pHeight; }
set { *pHeight = value; }
}
public School MySchool
{
get
{
School reuslt = new School((IntPtr)(*pSchool));
return reuslt;
}
set
{
*pSchool = (void*)value.pSchool;
}
}
#endregion
}