Mono.Debugger.Languages.TargetFundamentalType.CreateObject C# (CSharp) Method

CreateObject() public method

public CreateObject ( object obj ) : byte[]
obj object
return byte[]
        public virtual byte[] CreateObject(object obj)
        {
            switch (fundamental_kind) {
            case FundamentalKind.Boolean:
                return BitConverter.GetBytes (Convert.ToBoolean (obj));

            case FundamentalKind.Char:
                return BitConverter.GetBytes (Convert.ToChar (obj));

            case FundamentalKind.SByte:
                return BitConverter.GetBytes (Convert.ToSByte (obj));

            case FundamentalKind.Byte:
                return BitConverter.GetBytes (Convert.ToByte (obj));

            case FundamentalKind.Int16:
                return BitConverter.GetBytes (Convert.ToInt16 (obj));

            case FundamentalKind.UInt16:
                return BitConverter.GetBytes (Convert.ToUInt16 (obj));

            case FundamentalKind.Int32:
                return BitConverter.GetBytes (Convert.ToInt32 (obj));

            case FundamentalKind.UInt32:
                return BitConverter.GetBytes (Convert.ToUInt32 (obj));

            case FundamentalKind.Int64:
                return BitConverter.GetBytes (Convert.ToInt64 (obj));

            case FundamentalKind.UInt64:
                return BitConverter.GetBytes (Convert.ToUInt64 (obj));

            case FundamentalKind.Single:
                return BitConverter.GetBytes (Convert.ToSingle (obj));

            case FundamentalKind.Double:
                return BitConverter.GetBytes (Convert.ToDouble (obj));

            case FundamentalKind.IntPtr:
            case FundamentalKind.UIntPtr: {
                IntPtr ptr = (IntPtr) obj;
                if (IntPtr.Size == 4)
                    return BitConverter.GetBytes (ptr.ToInt32 ());
                else
                    return BitConverter.GetBytes (ptr.ToInt64 ());
            }

            case FundamentalKind.Decimal: {
                IntPtr ptr = IntPtr.Zero;
                try {
                    int size = Marshal.SizeOf (typeof (decimal));

                    byte[] data = new byte [size];
                    ptr = Marshal.AllocHGlobal (size);
                    Marshal.StructureToPtr (obj, ptr, false);

                    Marshal.Copy (ptr, data, 0, size);
                    return data;
                } finally {
                    if (ptr != IntPtr.Zero)
                        Marshal.FreeHGlobal (ptr);
                }
            }

            default:
                throw new InvalidOperationException ();
            }
        }