public bit_field_info(int offset, int bit_offset, int bit_size, bool bigendian = false) { _offset = offset; _size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)); _bit_offset = bit_offset; _bit_size = bit_size; if ((bit_offset > (8 * _size)) || (bit_size > (8 * _size))) { throw new ArgumentException(); } if (BitConverter.IsLittleEndian == !bigendian) { _mask = (~0ul >> (64 - bit_size)) << bit_offset; if (_size <= 1) { _getter = get_byte; _setter = set_byte; } else if (_size <= 2) { _getter = get_ushort; _setter = set_ushort; } else if (_size <= 4) { _getter = get_uint; _setter = set_uint; } else if (_size <= 8) { _getter = get_ulong; _setter = set_ulong; } else { throw new ArgumentException(); } } else { _mask = (~0ul << (64 - bit_size)) >> bit_offset; byte[] temp = BitConverter.GetBytes(_mask); Array.Reverse(temp); _mask = BitConverter.ToUInt64(temp, 0); if (_size <= 1) { _getter = get_byte; _setter = set_byte; } else if (_size <= 2) { _getter = get_ushort_swp; _setter = set_ushort_swp; } else if (_size <= 4) { _getter = get_uint_swp; _setter = set_uint_swp; } else if (_size <= 8) { _getter = get_ulong_swp; _setter = set_ulong_swp; } else { throw new ArgumentException(); } } }
public value_field_info(int offset, bool bigendian = false) { Type type = typeof(T); _offset = offset; _bigendian = bigendian; if (type == typeof(byte)) { _size = sizeof(byte); _getter = get_byte; _setter = set_byte; } else if (type == typeof(sbyte)) { _size = sizeof(sbyte); _getter = get_sbyte; _setter = set_sbyte; } else if (BitConverter.IsLittleEndian == !bigendian) { if (type == typeof(short)) { _size = sizeof(short); _getter = get_short; _setter = set_short; } else if (type == typeof(ushort)) { _size = sizeof(ushort); _getter = get_ushort; _setter = set_ushort; } else if (type == typeof(int)) { _size = sizeof(int); _getter = get_int; _setter = set_int; } else if (type == typeof(uint)) { _size = sizeof(uint); _getter = get_uint; _setter = set_uint; } else if (type == typeof(long)) { _size = sizeof(long); _getter = get_long; _setter = set_long; } else if (type == typeof(ulong)) { _size = sizeof(ulong); _getter = get_ulong; _setter = set_ulong; } else { throw new ArgumentException(); } } else { if (type == typeof(short)) { _size = sizeof(short); _getter = get_short_swp; _setter = set_short_swp; } else if (type == typeof(ushort)) { _size = sizeof(ushort); _getter = get_ushort_swp; _setter = set_ushort_swp; } else if (type == typeof(int)) { _size = sizeof(int); _getter = get_int_swp; _setter = set_int_swp; } else if (type == typeof(uint)) { _size = sizeof(uint); _getter = get_uint_swp; _setter = set_uint_swp; } else if (type == typeof(long)) { _size = sizeof(long); _getter = get_long_swp; _setter = set_long_swp; } else if (type == typeof(ulong)) { _size = sizeof(ulong); _getter = get_ulong_swp; _setter = set_ulong_swp; } else { throw new ArgumentException(); } } }