Fixed字段如何应用反射

jeedispeed 2010-05-01 12:09:29


unsafe struct _s{
fixed int a[10];
};

_s s_obj;
Type t = typeof(_s);
foreach(FieldInfo f in t.GetFields()){
object o = f.GetVale(s_obj);
}


类似这样的类 FieldInfo.GetValue(s_obj)如何获取值?

首先字段a的类型在C#中是int*,object如何拆箱为一个int*呢?
...全文
187 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingyuebuyu 2010-05-02
  • 打赏
  • 举报
回复
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", CharSet = CharSet.Ansi)]
public extern static long CopyMemory(IntPtr dest, IntPtr source, int size);

private void Form1_Load(object sender, EventArgs e)
{
_s s_obj=new _s();
//首先初始化s_obj中的数组a
int[] i = new int[] { 1, 2, 3,4,5,6,7,8,9,10 };
int k = Marshal.SizeOf(s_obj);
IntPtr ii = Marshal.AllocHGlobal(4 * 10);
Marshal.Copy(i, 0, ii, i.Length);
s_obj=(_s) Marshal.PtrToStructure(ii, typeof(_s));
Marshal.FreeHGlobal(ii);

Type t = typeof(_s);
FieldInfo[] fd = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);

foreach (FieldInfo f in fd)
{
unsafe
{
//反射得到对象
object o =f.GetValue(s_obj);

//固定住对象,获取对象的地址
GCHandle gc = GCHandle.Alloc(o,GCHandleType.Pinned);

IntPtr addr = gc.AddrOfPinnedObject();


//确定该字段是否有FixedBufferAttribute属性
object[] attrs = f.GetCustomAttributes(typeof(FixedBufferAttribute), false);
if ((attrs != null) && (attrs.Length > 0))
{
FixedBufferAttribute attr = (FixedBufferAttribute)attrs[0];

Console.WriteLine("Tyep :{0} Name :{1}[{2}]", attr.ElementType.Name, f.Name, attr.Length);
Array ary = Array.CreateInstance(attr.ElementType, attr.Length);
IntPtr dest = Marshal.UnsafeAddrOfPinnedArrayElement(ary, 0);
int len = Marshal.SizeOf(attr.ElementType) * attr.Length;
//拷贝值到ary中
CopyMemory(dest, addr, len);

}

gc.Free();



}
}

}

unsafe struct _s
{
int c;
fixed int a[4];
fixed int b[5];

};


其它的再按照你的需要改就可以了
jeedispeed 2010-05-02
  • 打赏
  • 举报
回复
楼上这个算AD吗?
songfei5201314 2010-05-02
  • 打赏
  • 举报
回复
有项目管理经验的.NET开发的朋友,可以加上限500人的QQ群28720769,一起交流。
jeedispeed 2010-05-02
  • 打赏
  • 举报
回复
查遍MSDN 没有发现一个能够获得非托管字段大小的函数。
有一个折中的方法是通过Marshal.OffsetOf方法 通过这个方法可以计算出每个字段的长度

但是如果是数组,还是不能获取数组的元素个数
类似

unsafe struct _SampleStruct
{
public fixed Int64 a[3];
public fixed Int32 b[5];
public int c;

}

这样的代码,反射还是不能很好的解决。
而且如果用System.Activator.CreateInstance(f.FieldType);貌似无法真实获得字段类型。
yufenghua 2010-05-01
  • 打赏
  • 举报
回复
太菜 只能帮顶
jeedispeed 2010-05-01
  • 打赏
  • 举报
回复
如果对此字段取FieldType得到的结果类似是__FixedBuffer0 __FixedBuffer1这样的偏移值
应该是元数据一类的东西 对元数据不甚理解
jeedispeed 2010-05-01
  • 打赏
  • 举报
回复
首先谢谢xingyuebuyu

unsafe struct _s
{
fixed int a[10];
fixed int b[20];
};

如果FixedBuffer不止一个 应该如何
也就是说10是一个位置量
shixiujin 2010-05-01
  • 打赏
  • 举报
回复
值得学习..
xingyuebuyu 2010-05-01
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
_s s_obj;
//首先初始化s_obj中的数组a
int[] i = new int[] { 1, 2, 3,4,5,6,7,8,9,10 };
int k = Marshal.SizeOf(s_obj);
IntPtr ii = Marshal.AllocHGlobal(4 * 10);
Marshal.Copy(i, 0, ii, i.Length);
s_obj=(_s) Marshal.PtrToStructure(ii, typeof(_s));
Marshal.FreeHGlobal(ii);

Type t = typeof(_s);
FieldInfo[] fd = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

foreach (FieldInfo f in fd)
{
unsafe
{
//反射得到对象
object o =f.GetValue(s_obj);
//固定住对象,获取对象的地址
GCHandle gc = GCHandle.Alloc(o,GCHandleType.Pinned);
IntPtr k1 = gc.AddrOfPinnedObject();
int[] i2 = new int[10];
//将值拷贝到数组i2中
Marshal.Copy(k1, i2, 0, i2.Length);
gc.Free();
}
}
}

unsafe struct _s
{
fixed int a[10];
};

}
}

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧