C++ code
struct{
unsigned int ia;
}A;
struct{
unsigned int ib;
}B;
union{
A a;
B b;
}U;
转成C#中的:
[StructLayout(LayoutKind.Sequential)]
public struct A{
public UInt32 ia;
};
[StructLayout(LayoutKind.Sequential)]
public struct B{
public UInt32 ib;
};
[StructLayout(LayoutKind.Explicit)]
public struct U{
[FieldOffset(0)]
public A a;
[FieldOffset(0)]
public B b;
};
会报错
An unhandled exception of type 'System.TypeLoadException' occurred in Debugger.exe
Additional information: 未能从程序 集 Debugger, Version=1.0.1572.568, Culture=neutral, PublicKeyToken=null 中 加载类型 U,因为它在 0偏移位置处包含一个对象字段,该字段已由一个非对象字段不正确地对齐或重叠。
//////////
但是,如果把A,B改成如下就能运行正常。[StructLayout(LayoutKind.Sequential)]
public struct A{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public UInt32[] ia;
};
[StructLayout(LayoutKind.Sequential)]
public struct B{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public UInt32[] ib;
};
但不知道能否达到相同的union效果……,请问我刚开始该怎么转才能实现union套struct的功能啊!!跪谢!
c#c++structunion
struct{
unsigned int ia;
}A;
struct{
unsigned int ib;
}B;
union{
A a;
B b;
}U;
转成C#中的:
[StructLayout(LayoutKind.Sequential)]
public struct A{
public UInt32 ia;
};
[StructLayout(LayoutKind.Sequential)]
public struct B{
public UInt32 ib;
};
[StructLayout(LayoutKind.Explicit)]
public struct U{
[FieldOffset(0)]
public A a;
[FieldOffset(0)]
public B b;
};
会报错
An unhandled exception of type 'System.TypeLoadException' occurred in Debugger.exe
Additional information: 未能从程序 集 Debugger, Version=1.0.1572.568, Culture=neutral, PublicKeyToken=null 中 加载类型 U,因为它在 0偏移位置处包含一个对象字段,该字段已由一个非对象字段不正确地对齐或重叠。
//////////
但是,如果把A,B改成如下就能运行正常。[StructLayout(LayoutKind.Sequential)]
public struct A{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public UInt32[] ia;
};
[StructLayout(LayoutKind.Sequential)]
public struct B{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public UInt32[] ib;
};
但不知道能否达到相同的union效果……,请问我刚开始该怎么转才能实现union套struct的功能啊!!跪谢!
c#c++structunion
解决方案 »
- 如何做到功能键的取消?
- c#中如何使用keybd_event函数?主要是keyup的问题
- C#自定义控件一步一步走
- datagrid中的数据读取问题
- TreeView的问题+在线给分
- 急!showModalDialog传递多个参数,高手帮忙!up有分!!
- 大虾请进,多线程调用问题
- 我做了一个用listView列举系统进程的程序,为什么老是提示“进程计数器已禁用,因此无法执行请求“急急,在线等(分数不够再加)(附原吗
- winform的listview中怎样处理类似SelectedIndexChanged事件?
- 小弟下载了VisualStudio.NET中文版,7张盘,但不知 .iso的用法,请帮忙!
- 提取11对战平台改键工具
- 如何让服务器控件FileUpload上传完文件后保证不刷新呢?
[StructLayout(LayoutKind.Explicit)]
public struct U{
[FieldOffset(0)]
public A a;
[FieldOffset(1)]
public B b;
};
比如
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication1
{
struct Integer
{
public uint Value;
public ushort High
{
get { return Convert.ToUInt16(Value / 0x10000); }
set { Value = (uint)value * 0x10000 + Low; }
}
public ushort Low
{
get { return Convert.ToUInt16(Value % 0x10000); }
set { Value = (uint)High * 0x10000 + value; }
}
}
class Program
{
static void Main(string[] args)
{
Integer i;
i.Value = 0x87654321;
Console.WriteLine("H: {0:x}, L: {1:x}", i.High, i.Low);
i.Low = 0x5678;
i.High = 0x1234;
Console.WriteLine("{0:x}", i.Value);
}
}
}
把Value作为属性,High和Low作为字段,总之重叠元素的一方作为属性访问器。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication1
{
struct Integer
{
public ushort High;
public ushort Low;
public uint Value
{
get { return (uint)High * 0x10000 + Low; }
set { High = Convert.ToUInt16(value / 0x10000); Low = Convert.ToUInt16(value % 0x10000); }
}
}
class Program
{
static void Main(string[] args)
{
Integer i = new Integer();
i.Value = 0x87654321;
Console.WriteLine("H: {0:x}, L: {1:x}", i.High, i.Low);
i.Low = 0x5678;
i.High = 0x1234;
Console.WriteLine("{0:x}", i.Value);
}
}
}
大致是关于那个union 写两个struct 对应
如果每个union对应多个struct,那我的参数就变成了多种了
C#支持一个函数名多种参数的复用么?
谢谢啦!!
但话说回来了,如果各个字段都相同的话,何必用union???