コード例 #1
0
        public unsafe static byte[] GetStructureBytes(ValueType structure)
        {
            Type t = structure.GetType();

            if (t.IsPrimitive || t.IsLayoutSequential)
            {
                return(ExtractOperation.ValueStructureToBytes(structure));
            }

            byte[] b          = null;
            var    _structure = structure;

            if (structure is DateTime)
            {
                b          = new byte[8];
                _structure = ((DateTime)structure).ToBinary();
            }
            else if (structure is Enum)
            {
                b          = new byte[4];
                _structure = Convert.ToInt32((Enum)structure);
            }
            else
            {
                b = new byte[Marshal.SizeOf(_structure)];
            }


            fixed(byte *pb = b)
            Marshal.StructureToPtr(_structure, new IntPtr(pb), false);

            return(b);
        }
コード例 #2
0
        public unsafe static Byte[] GetBytes(this Object objvalue)
        {
            Type t = objvalue.GetType();

            if (objvalue is ValueType)
            {
                if (t.IsPrimitive || t.IsLayoutSequential)
                {
                    return(ExtractOperation.ValueStructureToBytes(objvalue));
                }
                if (objvalue is DateTime)
                {
                    return(((DateTime)objvalue).ToBinary().GetBytes());
                }
                if (objvalue is Enum)
                {
                    return(Convert.ToInt32(objvalue).GetBytes());
                }
                return(objvalue.GetSequentialBytes());
            }
            if (t.IsLayoutSequential)
            {
                return(objvalue.GetSequentialBytes());
            }
            if (objvalue is IUnique)
            {
                return(((IUnique)objvalue).GetBytes());
            }
            if (objvalue is String || objvalue is IFormattable)
            {
                return(objvalue.ToString().GetBytes());
            }
            return(new byte[0]);
        }
コード例 #3
0
        public unsafe static byte[] GetStructureBytes(object structure)
        {
            byte[] b          = null;
            object _structure = structure;

            if (_structure is string)
            {
                int l = ((string)_structure).Length;
                b = new byte[l];

                fixed(char *c = (string)_structure)
                fixed(byte *pb = b)
                CopyBlock(pb, (byte *)c, l);
            }

            if (structure is ValueType)
            {
                Type t = structure.GetType();
                if (t.IsPrimitive || t.IsLayoutSequential)
                {
                    return(ExtractOperation.ValueStructureToBytes(structure));
                }

                if (structure is DateTime)
                {
                    b          = new byte[8];
                    _structure = ((DateTime)_structure).ToBinary();
                }
                else if (structure is Enum)
                {
                    b         = new byte[4];
                    structure = Convert.ToInt32((Enum)structure);
                }
                else
                {
                    b = new byte[Marshal.SizeOf(_structure)];
                }
            }
            else
            {
                b = new byte[Marshal.SizeOf(_structure)];


                fixed(byte *pb = b)
                Marshal.StructureToPtr(_structure, new IntPtr(pb), false);

                return(b);
        }