public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            SqlParameter par = (SqlParameter)obj;

            par.ParameterName = info.GetString("ParameterName");
            par.DbType        = SurrogateEnumSetter.Parse <DbType>(info, "DbType");
            par.Direction     = SurrogateEnumSetter.Parse <ParameterDirection>(info, "Direction");
            par.Value         = info.GetValue("Value", typeof(object));
            par.Size          = info.GetInt32("Size");
            par.IsNullable    = info.GetBoolean("IsNullable");
            return(null);
        }
        public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            SqlCommand comm = (SqlCommand)obj;

            comm.CommandText = info.GetString("CommandText");
            comm.CommandType = SurrogateEnumSetter.Parse <CommandType>(info, "CommandType");
            int parcount = info.GetInt32("ParametersCount");

            for (int i = 0; i < parcount; i++)
            {
                SqlParameter par = (SqlParameter)info.GetValue("Parameter" + i, typeof(SqlParameter));
                comm.Parameters.Add(par);
            }
            return(null);
        }
        public static SqlCommand FromByteArray(byte[] array)
        {
            /*
             * var ss = new SurrogateSelector();
             * ss.AddSurrogate(typeof(SqlCommand), new StreamingContext(StreamingContextStates.All), new SqlCommandSurrogate());
             * ss.AddSurrogate(typeof(SqlParameter), new StreamingContext(StreamingContextStates.All), new SqlParameterSurrogate());
             * return Serializer.FromByteArray<SqlCommand>(array, ss);
             */
            SqlCommand cmd = new SqlCommand();

            using (MemoryStream stm = new MemoryStream(array))
            {
                using (BinaryReader reader = new BinaryReader(stm))
                {
                    cmd.CommandText = reader.ReadString();
                    cmd.CommandType = SurrogateEnumSetter.Parse <CommandType>(reader.ReadString());
                    int parcnt = reader.ReadInt32();
                    for (int i = 0; i < parcnt; i++)
                    {
                        string       name = reader.ReadString();
                        SqlParameter par  = cmd.Parameters.AddWithValue(name, null);
                        par.DbType    = SurrogateEnumSetter.Parse <DbType>(reader.ReadString());
                        par.Direction = SurrogateEnumSetter.Parse <ParameterDirection>(reader.ReadString());
                        string type = reader.ReadString();
                        switch (type)
                        {
                        case "DBNull":
                            reader.ReadString();
                            par.Value = DBNull.Value;
                            break;

                        case "Boolean":
                            par.Value = reader.ReadBoolean();
                            break;

                        case "Double":
                            par.Value = reader.ReadDouble();
                            break;

                        case "Decimal":
                            par.Value = reader.ReadDecimal();
                            break;

                        case "Int16":
                            par.Value = reader.ReadInt16();
                            break;

                        case "Int32":
                            par.Value = reader.ReadInt32();
                            break;

                        case "Int64":
                            par.Value = reader.ReadInt64();
                            break;

                        case "String":
                            par.Value = reader.ReadString();
                            break;

                        case "DateTime":
                            par.Value = DateTime.ParseExact(reader.ReadString(), "yyyy-MM-dd HH:mm:ss.fffffff", null);
                            break;

                        case "Byte[]":
                            par.Value = reader.ReadBytes(reader.ReadInt32());
                            break;

                        case "(null)":
                            reader.ReadString();
                            par.Value = null;
                            break;

                        default:
                            reader.ReadString();
                            par.Value = null;
                            break;
                        }
                        par.Size       = reader.ReadInt32();
                        par.IsNullable = reader.ReadBoolean();
                    }
                }
            }
            return(cmd);
        }