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*呢?
...全文
281 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];
};

}
}
内容概要:本文围绕基于PI双闭环解耦控制的三相电压型PWM整流器在第四象限运行的仿真研究展开,重点分析其在电流反向流动工况下的控制性能。通过Simulink搭建系统模型,采用电压外环与电流内环构成的双闭环PI控制策略,并引入d-q轴解耦环节以消除交叉耦合影响,实现对整流器在能量回馈状态下的高精度、稳定控制。研究涵盖了系统数学建模、控制器参数设计、解耦算法实现及动态响应仿真验证,充分展示了该控制方法在抑制扰动、提升系统鲁棒性方面的有效性。; 适合人群:电力电子、电气工程及其自动化等相关专业的研究生、科研人员及从事新能源变流器、电能质量治理或工业传动系统开发的工程技术人员;具备自动控制理论基础和Simulink仿真能力者更佳。; 使用场景及目标:①深入掌握三相电压型PWM整流器的工作原理及其在不同运行象限的能量流动特性;②理解并实践PI双闭环控制系统的设计思路与参数整定方法;③学习d-q坐标系下电流解耦控制的实现机制;④熟练运用Simulink进行电力电子系统建模与仿真分析;⑤为实际工程中实现高效能量双向变换提供理论依据与技术参考。; 阅读建议:建议结合Simulink环境同步搭建模型,细致分析各模块的信号流向与控制逻辑,重点关注电流内环的动态跟踪能力和电压外环的稳态调节性能,可通过改变负载突变、电网电压波动等条件进行对比实验,进一步评估系统的抗干扰能力与稳定性表现。

111,129

社区成员

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

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

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