Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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)));
        }
Exemplo n.º 4
0
            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]);
                }
            }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
0
            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);
            }
Exemplo n.º 7
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.");
     }
 }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
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();
        }
Exemplo n.º 10
0
        public T ReadValue(IValueReader valueReader)
        {
            if (valueReader is IValueReader <T> reader)
            {
                return(reader.ReadValue());
            }

            if (typeof(T).IsAssignableFrom(typeof(DataTableReader)))
            {
                return(Unsafe.As <T>(ValueInterface <DataTable> .ReadValue(valueReader)?.CreateDataReader()));
            }

            throw new NotSupportedException();
        }
Exemplo n.º 11
0
        public static IDataWriter CreateWriter <T>()
        {
            var auxiliaryValueRW = new AuxiliaryValueRW();

            ValueInterface <T> .ReadValue(auxiliaryValueRW);

            var value = auxiliaryValueRW.read_writer;

            if (value is IAsDataWriter @as)
            {
                return(@as.Content);
            }

            return((IDataWriter)value);
        }
Exemplo n.º 12
0
        public void OnWriteAll(IDataReader <TKey> dataReader)
        {
            var buckets = new KeyValuePair <TKey, TValue> [content.Count];

            var index = 0;

            foreach (var item in content.Keys)
            {
                buckets[index] = new KeyValuePair <TKey, TValue>(item, ValueInterface <TValue> .ReadValue(dataReader[item]));

                ++index;
            }

            content.Clear();

            foreach (var item in buckets)
            {
                content.Add(item);
            }
        }
Exemplo n.º 13
0
        public T ReadValue(IValueReader valueReader)
        {
            if (valueReader is ValueCopyer copyer && copyer.InternalObject is T ret)
            {
                return(ret);
            }

            if (typeof(T).IsAssignableFrom(typeof(TValue[])))
            {
                return(TypeHelper.As <TValue[], T>(ValueInterface <TValue[]> .ReadValue(valueReader)));
            }
            else if (typeof(T).IsAssignableFrom(typeof(List <TValue>)))
            {
                return(TypeHelper.As <List <TValue>, T>(ValueInterface <List <TValue> > .ReadValue(valueReader)));
            }
            else
            {
                return(XConvert.Convert <TValue[], T>(ValueInterface <TValue[]> .ReadValue(valueReader)));
            }
        }
Exemplo n.º 14
0
 public void OnWriteValue(TKey key, IValueReader valueReader)
 {
     content[key] = ValueInterface <TValue> .ReadValue(valueReader);
 }
Exemplo n.º 15
0
 public void OnWriteValue(object key, IValueReader valueReader)
 {
     content[key] = ValueInterface <object> .ReadValue(valueReader);
 }
Exemplo n.º 16
0
        public T ReadValue(IValueReader valueReader)
        {
            if (valueReader is IValueReader <T> tReader)
            {
                return(tReader.ReadValue());
            }

            if (IsAssignableToMethodInfo && valueReader is IValueReader <MethodInfo> methodReader)
            {
                return((T)(object)methodReader.ReadValue());
            }
            if (IsAssignableToFieldInfo && valueReader is IValueReader <MethodInfo> fieldReader)
            {
                return((T)(object)fieldReader.ReadValue());
            }
            if (IsAssignableToPropertyInfo && valueReader is IValueReader <PropertyInfo> propertyReader)
            {
                return((T)(object)propertyReader.ReadValue());
            }
            if (IsAssignableToConstructorInfo && valueReader is IValueReader <ConstructorInfo> constructorReader)
            {
                return((T)(object)constructorReader.ReadValue());
            }
            if (IsAssignableToEventInfo && valueReader is IValueReader <EventInfo> eventReader)
            {
                return((T)(object)eventReader.ReadValue());
            }

            var value = valueReader.DirectRead();

            if (value is null)
            {
                return(null);
            }

            if (RWHelper.CreateReader(value, false) is IDataReader <string> infoReader)
            {
                var declaringType = ValueInterface <Type> .ReadValue(infoReader["DeclaringType"]);

                var name = ValueInterface <string> .ReadValue(infoReader["Name"]);

                if (name == ConstructorName || IsAssignableToConstructorInfo)
                {
                    var parameterTypes = ValueInterface <Type[]> .ReadValue(infoReader["ParameterTypes"]);

                    var constructor = declaringType.GetConstructor(parameterTypes);

                    if (constructor != null && constructor is T ret)
                    {
                        return(ret);
                    }
                }
                else if (name == IndexerName)
                {
                    var parameterTypes = ValueInterface <Type[]> .ReadValue(infoReader["ParameterTypes"]);

                    var propertyType = ValueInterface <Type> .ReadValue(infoReader["PropertyType"]);

                    var indexer = declaringType.GetProperty(parameterTypes);

                    if (indexer != null && indexer.PropertyType == propertyType && indexer is T ret)
                    {
                        return(ret);
                    }
                }
                else if (IsAssignableToFieldInfo)
                {
                    var fieldType = ValueInterface <Type> .ReadValue(infoReader["FieldType"]);

                    var field = declaringType.GetField(name);

                    if (field != null && field.FieldType == fieldType && field is T ret)
                    {
                        return(ret);
                    }
                }
                else if (IsAssignableToPropertyInfo)
                {
                    var propertyType = ValueInterface <Type> .ReadValue(infoReader["PropertyType"]);

                    var property = declaringType.GetProperty(name);

                    if (property != null && property.PropertyType == propertyType && property is T ret)
                    {
                        return(ret);
                    }
                }
                else if (IsAssignableToEventInfo)
                {
                    var eventHandlerType = ValueInterface <Type> .ReadValue(infoReader["EventHandlerType"]);

                    var @event = declaringType.GetEvent(name);

                    if (@event != null && @event.EventHandlerType == eventHandlerType && @event is T ret)
                    {
                        return(ret);
                    }
                }
                else if (IsAssignableToMethodInfo)
                {
                    goto Method;
                }
                else
                {
                    var member = declaringType.GetMember(name, Flags);

                    if (member.Length > 0)
                    {
                        switch (member[0].MemberType)
                        {
                        case MemberTypes.Event:
                        {
                            var eventHandlerType = ValueInterface <Type> .ReadValue(infoReader["EventHandlerType"]);

                            if (member[0] is EventInfo @event && @event.EventHandlerType == eventHandlerType && @event is T ret)
                            {
                                return(ret);
                            }
                        }
                        break;

                        case MemberTypes.Field:
                        {
                            var fieldType = ValueInterface <Type> .ReadValue(infoReader["FieldType"]);

                            if (member[0] is FieldInfo field && field.FieldType == fieldType && field is T ret)
                            {
                                return(ret);
                            }
                        }
                        break;

                        case MemberTypes.Property:
                        {
                            var propertyType = ValueInterface <Type> .ReadValue(infoReader["PropertyType"]);

                            if (member[0] is PropertyInfo property && property.PropertyType == propertyType && property is T ret)
                            {
                                return(ret);
                            }
                        }
                        break;

                        case MemberTypes.Method:
                            goto Method;

                        default:
                            throw new NotSupportedException();
                        }
                    }
                }

NoFound:

                throw new MissingMemberException(declaringType.FullName, name);

Method:
                {
                    var parameterTypes = ValueInterface <Type[]> .ReadValue(infoReader["ParameterTypes"]);

                    var returnType = ValueInterface <Type> .ReadValue(infoReader["ReturnType"]);

                    var method = declaringType.GetMethod(name, Flags, Type.DefaultBinder, parameterTypes, null);

                    if (method != null && method.ReturnType == returnType && method is T ret)
                    {
                        return(ret);
                    }

                    goto NoFound;
                }
            }

            if (value is string memberName)
            {
                try
                {
                    if (Type.GetType(memberName) is T ret)
                    {
                        return(ret);
                    }
                }
                catch
                {
                }
            }

            return(XConvert <T> .FromObject(value));
        }