最近直在研究Net Micro Framework字体文件(tinyfnt)由于tinyfnt文件头部有段描述数据所以很想

定义个结构体像VC样直接从文件中读出来省得用流个个解析很是麻烦   没有想到在中竟没有直接指令想必设

计者认为提供了流和序列化技术切问题都可以迎刃而解了

  在中结构体是个比较复杂东西在此的上有很多需要设置参数否则用起来就很容易出错下面是msdn上段描述

看看也许有助于理解C#语言中结构体

  通过使用属性可以自定义结构在内存中布局方式例如可以使用 StructLayout(LayoutKind.Explicit) 和

FieldOff 属性创建在 C/C 中称为联合布局

[.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]

struct TestUnion

{

[.Runtime.InteropServices.FieldOff(0)]

public i;

[.Runtime.InteropServices.FieldOff(0)]

public double d;

[.Runtime.InteropServices.FieldOff(0)]

public char c;

[.Runtime.InteropServices.FieldOff(0)]

public b;

}  在上个代码段中TestUnion 所有字段都从内存中同位置开始

  以下是字段从其他显式设置位置开始另个举例

[.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]

struct TestExplicit

{

[.Runtime.InteropServices.FieldOff(0)]

public long lg;

[.Runtime.InteropServices.FieldOff(0)]

public i1;

[.Runtime.InteropServices.FieldOff(4)]

public i2;

[.Runtime.InteropServices.FieldOff(8)] 

public double d;

[.Runtime.InteropServices.FieldOff(12)]

public char c;

[.Runtime.InteropServices.FieldOff(14)]

public b;

}  i1 和 i2 这两个 字段共享和 lg 相同内存位置使用平台时这种结构布局控制很有用

  我做了个简单测试基本达成预定需求不过该方式要求比较苛刻如果要解析数据和转换结构体不匹配就会

引发系列莫名其妙异常(如内存不可读等等的类)下面是测试源代码有兴趣朋友可以看看也希望网友能提出更好方



using ;

using .Collections.Generic;

using .ComponentModel;

using .Data;

using .Drawing;

using .Text;

using ..Forms;

using .IO;

using .Runtime.InteropServices;

RWFile

{

public partial Form1 : Form

{

public Form1

{

InitializeComponent;

}

//从文件中读结构体

private void button1_Click(object sender, EventArgs e)

{

strFile = Application.StartupPath + "  est.dat";

(!File.Exists(strFile))

{

MessageBox.Show("文件不存在");



}

FileStream fs = FileStream(strFile, FileMode.Open,

FileAccess.ReadWrite);

TestStruct ts = TestStruct;

bytData = [Marshal.SizeOf(ts)];

fs.Read(bytData, 0, bytData.Length);

fs.Close;

ts = rawDeserialize(bytData);

textBox1.Text = ts.dTest.;

textBox2.Text = ts.uTest.;

textBox3.Text = Encoding.Default.GetString(ts.bTest);

}

//向文件中写结构体

private void button2_Click(object sender, EventArgs e)

{

strFile = Application.StartupPath + "  est.dat";

FileStream fs = FileStream(strFile, FileMode.Create ,

                      FileAccess.Write);

TestStruct ts = TestStruct;

ts.dTest = double.Parse(textBox1.Text);

ts.uTest = UInt16.Parse(textBox2.Text);

ts.bTest = Encoding.Default.GetBytes(textBox3.Text);

bytData = rawSerialize(ts);

fs.Write(bytData, 0, bytData.Length);

fs.Close;

}

[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)] //,Size=16

public struct TestStruct

{

[MarshalAs(UnmanagedType.R8)] //,FieldOff(0)] 

public double dTest;

[MarshalAs(UnmanagedType.U2)] //, FieldOff(8)]

public UInt16 uTest;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]

                 //, FieldOff(10)]

public bTest; 

}

//序列化

public rawSerialize(object obj)

{

rawsize = Marshal.SizeOf(obj);

IntPtr buffer = Marshal.AllocHGlobal(rawsize);

Marshal.StructureToPtr(obj, buffer, false);

rawdatas = [rawsize];

Marshal.Copy(buffer, rawdatas, 0, rawsize);

Marshal.FreeHGlobal(buffer);

rawdatas;

}

//反序列化

public TestStruct rawDeserialize( rawdatas)

{

Type anytype = typeof(TestStruct);

rawsize = Marshal.SizeOf(anytype);

(rawsize > rawdatas.Length) TestStruct;

IntPtr buffer = Marshal.AllocHGlobal(rawsize);

Marshal.Copy(rawdatas, 0, buffer, rawsize);

object retobj = Marshal.PtrToStructure(buffer, anytype);

Marshal.FreeHGlobal(buffer);

(TestStruct)retobj;

}   

}

}

最新文章

  1. 【特种兵系列】String中的==和equals()
  2. CSS背景background、background-position使用详解
  3. PHP代码编写规范
  4. hdu4750Count The Pairs(最小生成树找瓶颈边)
  5. 【POJ 2774】Long Long Message 最长公共子串
  6. 云计算服务模型,第 3 部分: 软件即服务(PaaS)
  7. 【原】Storm 消息处理保障机制
  8. Python之路第四天,基础(4)-装饰器,迭代器,生成器
  9. 拥抱Node.js 8.0,N-API入门极简例子
  10. VS2013 FFmpeg开发环境配置
  11. js对象与字符串的想到转换
  12. .NET 实用扩展方法
  13. (转)tasklist命令参数应用详细图解
  14. EHCI规范(转)
  15. 【Java】【7】枚举类
  16. 取某字段不为空的数据is not null
  17. java使用反射给对象属性赋值的两种方法
  18. LoadLinked/StoreConditional (LL/SC)
  19. markdown小记(语法+markdownpad)
  20. 关于Python Profilers性能分析器

热门文章

  1. 分别用face++和百度获取人脸属性(python单机版)
  2. ngx.re.match使用示例
  3. 《Java多线程编程核心技术》推荐
  4. Leetcode难度表及解题汇总
  5. Useful command for Docker
  6. Dynamics CRM 不同的站点地图下设置默认不同的仪表板
  7. Vc2015 utf8 格式出错
  8. Spark Scheduler模块源码分析之DAGScheduler
  9. Makefile自动生成:cmake
  10. JDBC的java驱动安装