Exemple #1
0
 public void WriteValue(IValueWriter valueWriter, T value)
 {
     if (value is null)
     {
         valueWriter.DirectWrite(null);
     }
     else if (valueWriter is IValueWriter <T> writer)
     {
         writer.WriteValue(value);
     }
     else if (value is IFormattable)
     {
         valueWriter.DirectWrite(value);
     }
     else if (value is string str)
     {
         valueWriter.WriteString(str);
     }
     else if (TypeHandle != TypeHelper.GetTypeHandle(value))
     {
         ValueInterface.GetInterface(value).Write(valueWriter, value);
     }
     else
     {
         valueWriter.DirectWrite(value);
     }
 }
Exemple #2
0
 public void OnReadAll(IDataWriter <string> dataWriter)
 {
     foreach (DataColumn item in Content.Table.Columns)
     {
         ValueInterface.GetInterface(item.DataType).Write(dataWriter[item.ColumnName], Content[item.Ordinal]);
     }
 }
Exemple #3
0
        public static IDataWriter CreateWriter(object obj, bool throwException = true)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var auxiliary = new AuxiliaryValueRW();

            var @interface = ValueInterface.GetInterface(obj);

            @interface.Write(auxiliary, obj);

            var writer = auxiliary.GetDataWriter();

            if (writer != null)
            {
                return(writer);
            }

            @interface.Read(auxiliary);

            writer = auxiliary.GetDataWriter();

            if (writer == null)
            {
                return(auxiliary.GetDataWriter() ?? (throwException ? throw new NotSupportedException($"Unable create data writer of  '{obj.GetType()}'.") : default(IDataWriter)));
            }

            SetContent(writer, obj);

            return(writer);
        }
Exemple #4
0
 public void OnReadAll(IDataWriter <object> dataWriter)
 {
     foreach (DictionaryEntry item in content)
     {
         ValueInterface.WriteValue(dataWriter[item.Key], item.Value);
     }
 }
Exemple #5
0
 public void OnWriteAll(IDataReader <string> dataReader)
 {
     foreach (DataColumn item in Content.Table.Columns)
     {
         Content[item.Ordinal] = ValueInterface.GetInterface(item.DataType).Read(dataReader[item.ColumnName]);
     }
 }
 public void OnReadAll(IDataWriter <int> dataWriter)
 {
     for (int i = 0; i < dbDataReader.FieldCount; i++)
     {
         ValueInterface.WriteValue(dataWriter[i], dbDataReader[i]);
     }
 }
Exemple #7
0
        public static IDataWriter CreateWriter <T>(T obj, bool throwException = true)
        {
            var auxiliary = new AuxiliaryValueRW();

            try
            {
                ValueInterface <T> .WriteValue(auxiliary, obj);
            }
            catch (Exception)
            {
            }

            var writer = auxiliary.GetDataWriter();

            if (writer != null)
            {
                return(writer);
            }

            ValueInterface <T> .ReadValue(auxiliary);

            writer = auxiliary.GetDataWriter();

            if (writer == null)
            {
                return(auxiliary.GetDataWriter() ?? (throwException ? throw new NotSupportedException($"Unable create data writer of  '{typeof(T)}'.") : default(IDataWriter)));
            }

            SetContent(writer, obj);

            return(writer);
        }
 public void OnReadAll(IDataWriter <string> dataWriter)
 {
     for (int i = 0; i < dbDataReader.FieldCount; i++)
     {
         ValueInterface.WriteValue(dataWriter[dbDataReader.GetName(i)], dbDataReader[i]);
     }
 }
Exemple #9
0
        public static IDataRW CreateRW(object obj)
        {
            var helperValueRW = HelperValueRW;

            ValueInterface.GetInterface(obj).WriteValue(helperValueRW, obj);

            var value = helperValueRW.Content;

            if (value == null)
            {
                throw new NotSupportedException(StringHelper.Format("Failure to create IDataRW for BasicTypes '{0}'.", helperValueRW.GetBasicType().ToString()));
            }

            if (value is IAsDataReader)
            {
                value = ((IAsDataReader)value).Content;
            }

            if (value is IAsDataWriter)
            {
                value = ((IAsDataWriter)value).Content;
            }

            if (!(value is IDataRW))
            {
                throw new NotSupportedException(StringHelper.Format("Failure to create IDataRW for '{0}'.", obj.GetType().Name));
            }

            return((IDataRW)value);
        }
Exemple #10
0
        public static IDataWriter CreateWriter <T>(T obj)
        {
            var auxiliary = new AuxiliaryValueRW();

            ValueInterface <T> .WriteValue(auxiliary, obj);

            var writer = auxiliary.GetDataWriter();

            if (writer != null)
            {
                return(writer);
            }

            ValueInterface <T> .ReadValue(auxiliary);

            writer = auxiliary.GetDataWriter();

            if (writer == null)
            {
                return(auxiliary.GetDataWriter() ?? throw new NotSupportedException($"Unable create data writer of  '{typeof(T)}'."));
            }

            SetContent(writer, obj);

            return(writer);
        }
Exemple #11
0
        public void WriteValue(IValueWriter valueWriter, object value)
        {
            if (value == null)
            {
                valueWriter.DirectWrite(null);

                return;
            }

            if (valueWriter is IValueWriter <object> writer)
            {
                writer.WriteValue(value);

                return;
            }

            /* 父类引用,子类实例时使用 Type 获取写入器。 */

            if (TypeInfo <object> .Int64TypeHandle != (long)TypeHelper.GetTypeHandle(value))
            {
                ValueInterface.GetInterface(value).Write(valueWriter, value);

                return;
            }

            valueWriter.DirectWrite(value);
        }
 public void OnReadAll(IDataWriter <TKey> dataWriter)
 {
     foreach (var item in content)
     {
         ValueInterface <TValue> .WriteValue(dataWriter[item.Key], item.Value);
     }
 }
Exemple #13
0
        public static IDataWriter CreateWriter <T>(bool throwException = true)
        {
            var auxiliary = new AuxiliaryValueRW();

            ValueInterface <T> .ReadValue(auxiliary);

            return(auxiliary.GetDataWriter() ?? (throwException ? throw new NotSupportedException($"Unable create data writer of '{typeof(T)}'.") : default(IDataWriter)));
        }
Exemple #14
0
        public static IDataReader CreateReader <T>(T obj)
        {
            var auxiliary = new AuxiliaryValueRW();

            ValueInterface <T> .WriteValue(auxiliary, obj);

            return(auxiliary.GetDataReader() ?? throw new NotSupportedException($"Unable create data reader of '{typeof(T)}'."));
        }
Exemple #15
0
        public static IDataRW CreateRW <T>(T obj, bool throwException = true)
        {
            var auxiliary = new AuxiliaryValueRW();

            ValueInterface <T> .WriteValue(auxiliary, obj);

            return(auxiliary.GetDataRW() ?? (throwException ? throw new NotSupportedException($"Unable create data reader-writer of '{typeof(T)}'.") : default(IDataRW)));
        }
            public void OnReadAll(IDataWriter <int> dataWriter)
            {
                int length = Count;

                for (int i = 0; i < length; ++i)
                {
                    ValueInterface <T> .WriteValue(dataWriter[i], content.content[baseIndex, i]);
                }
            }
            public void OnWriteAll(IDataReader <int> dataReader)
            {
                int length = Count;

                for (int i = 0; i < length; ++i)
                {
                    content.content[baseIndex, i] = ValueInterface <T> .ReadValue(dataReader[i]);
                }
            }
Exemple #18
0
        public override void OnWriteValue(int key, IValueReader valueReader)
        {
            if (typeof(TMode) == typeof(ArrayRWModes.Builder))
            {
                if (index >= content.Length)
                {
                    Extend(index * 2 + 1);
                }

                if (ValueInterface <TElement> .IsNotModified)
                {
                    if (typeof(TElement) == typeof(string))
                    {
                        Unsafe.As <string[]>(content)[index] = valueReader.ReadString();
                    }
                    else if (typeof(TElement) == typeof(int))
                    {
                        Unsafe.As <int[]>(content)[index] = valueReader.ReadInt32();
                    }
                    else if (typeof(TElement) == typeof(bool))
                    {
                        Unsafe.As <bool[]>(content)[index] = valueReader.ReadBoolean();
                    }
                    else if (typeof(TElement) == typeof(double))
                    {
                        Unsafe.As <double[]>(content)[index] = valueReader.ReadDouble();
                    }
                    else if (typeof(TElement) == typeof(long))
                    {
                        Unsafe.As <long[]>(content)[index] = valueReader.ReadInt64();
                    }
                    else if (typeof(TElement) == typeof(DateTime))
                    {
                        Unsafe.As <DateTime[]>(content)[index] = valueReader.ReadDateTime();
                    }
                    else if (typeof(TElement) == typeof(decimal))
                    {
                        Unsafe.As <decimal[]>(content)[index] = valueReader.ReadDecimal();
                    }
                    else
                    {
                        content[index] = ValueInterface <TElement> .ReadValue(valueReader);
                    }
                }
                else
                {
                    content[index] = ValueInterface <TElement> .ReadValue(valueReader);
                }

                ++index;
            }
            else
            {
                content[key] = ValueInterface <TElement> .ReadValue(valueReader);
            }
        }
            public void OnWriteValue(int key, IValueReader valueReader)
            {
                CheckExpansion(key);

                content.content[baseIndex, key] = ValueInterface <T> .ReadValue(valueReader);

                content.count = Math.Max(baseIndex + 1, content.count);

                content.rank2Count = Math.Max(key + 1, content.rank2Count);
            }
Exemple #20
0
 public void OnWriteValue(int key, IValueReader valueReader)
 {
     if (key == Content.Tables.Count)
     {
         Content.Tables.Add(ValueInterface<DataTable>.ReadValue(valueReader));
     }
     else
     {
         throw new NotSupportedException($"'{typeof(T)}' not supported set tables.");
     }
 }
        public static void Set <T>(long id, IValueInterface <T> valueInterface)
        {
            if (!(ValueInterface <T> .Content is Impl <T>))
            {
                Impl <T> .DefaultInterface = ValueInterface <T> .Content;

                ValueInterface <T> .SetInterface(Impl <T> .Instance);
            }

            Impl <T> .Instance.Set(id, valueInterface);
        }
 public void WriteValue(IValueWriter valueWriter, T?value)
 {
     if (value == null)
     {
         valueWriter.DirectWrite(null);
     }
     else
     {
         ValueInterface <T> .WriteValue(valueWriter, value.Value);
     }
 }
        public override void OnWriteValue(int key, IValueReader valueReader)
        {
            if (key >= content.Length)
            {
                Array.Resize(ref content, (key * 3) + 1);
            }

            count = Math.Max(key + 1, count);

            content[key] = ValueInterface <T> .ReadValue(valueReader);
        }
Exemple #24
0
        public void OnReadValue(int key, IValueWriter valueWriter)
        {
            var value = Content[key];

            if (value == DBNull.Value)
            {
                value = null;
            }

            ValueInterface.WriteValue(valueWriter, value);
        }
Exemple #25
0
        public void OnWriteValue(string key, IValueReader valueReader)
        {
            DataColumn column;

            if (DataRow != null && (column = DataRow.Table.Columns[key]) != null && column.DataType != typeof(object))
            {
                DataRow[column] = ValueInterface.GetInterface(column.DataType).Read(valueReader);

                return;
            }

            Items.Add(new KeyValuePair <string, object>(key, valueReader.DirectRead()));
        }
Exemple #26
0
        public T ReadValue(IValueReader valueReader)
        {
            if (typeof(T).IsAssignableFrom(typeof(TValue[])))
            {
                return((T)(object)ValueInterface <TValue[]> .ReadValue(valueReader));
            }
            else if (typeof(T).IsAssignableFrom(typeof(List <TValue>)))
            {
                return((T)(object)ValueInterface <List <TValue> > .ReadValue(valueReader));
            }

            throw new NotSupportedException();
        }
Exemple #27
0
        public static IDataWriter CreateWriter(Type type, bool throwException = true)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var auxiliary = new AuxiliaryValueRW();

            ValueInterface.GetInterface(type).Read(auxiliary);

            return(auxiliary.GetDataWriter() ?? (throwException ? throw new NotSupportedException($"Unable create data writer of '{type}'.") : default(IDataRW)));
        }
Exemple #28
0
        public static IDataRW CreateRW(object obj, bool throwException = true)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var auxiliary = new AuxiliaryValueRW();

            ValueInterface.GetInterface(obj).Write(auxiliary, obj);

            return(auxiliary.GetDataRW() ?? (throwException ? throw new NotSupportedException($"Unable create data reader-writer of '{obj.GetType()}'.") : default(IDataRW)));
        }
Exemple #29
0
        public static IDataReader CreateReader(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var auxiliary = new AuxiliaryValueRW();

            ValueInterface.GetInterface(obj).Write(auxiliary, obj);

            return(auxiliary.GetDataReader() ?? throw new NotSupportedException($"Unable create data reader of '{obj.GetType()}'."));
        }
Exemple #30
0
        public void WriteValue(IValueWriter valueWriter, T value)
        {
            if (value == null)
            {
                valueWriter.DirectWrite(null);

                return;
            }

            /* 父类引用,子类实例时使用 Type 获取写入器。 */
            if (CheckChildrenInstance && Int64TypeHandle != (long)TypeHelper.GetTypeHandle(value))
            {
                ValueInterface.GetInterface(value).Write(valueWriter, value);

                return;
            }

            if (valueWriter is ISingleThreadOptimize)
            {
                var current_cache = thread_cache;

                if (current_cache == null)
                {
                    InitializeInstance(ref current_cache);
                }

                if (current_cache.IsUsed)
                {
                    goto Default;
                }

                current_cache.IsUsed = true;

                current_cache.Instance.Initialize(value);

                valueWriter.WriteObject(current_cache.Instance);

                current_cache.IsUsed = false;

                return;
            }

Default:

            var fastObjectRW = FastObjectRW <T> .Create();

            fastObjectRW.Initialize(value);

            valueWriter.WriteObject(fastObjectRW);
        }