示例#1
0
        public T ReadValue(IValueReader valueReader)
        {
            if (valueReader is IValueReader <T> tReader)
            {
                return(tReader.ReadValue());
            }

            var value = valueReader.DirectRead();

            if (value is T tValue)
            {
                return(tValue);
            }

            var reader = RWHelper.CreateReader(value);

            if (reader is T tResult)
            {
                return(tResult);
            }

            if (reader != null && reader.As <TKey>() is T tResult2)
            {
                return(tResult2);
            }

            return(XConvert <T> .FromObject(value));
        }
示例#2
0
        public T ReadValue(IValueReader valueReader)
        {
            if (valueReader is IValueReader <T> tReader)
            {
                return(tReader.ReadValue());
            }

            if (valueReader is IValueFiller <TKey> tFiller &&
                typeof(IDataWriter <TKey>).IsAssignableFrom(typeof(T)))
            {
                T instance = default(T);

                try
                {
                    instance = Activator.CreateInstance <T>();;
                }
                catch (Exception)
                {
                    goto Direct;
                }

                tFiller.FillValue((IDataWriter <TKey>)instance);

                return(instance);
            }

Direct:

            var value = valueReader.DirectRead();

            if (value is T tValue)
            {
                return(tValue);
            }

            var reader = RWHelper.CreateReader(value);

            if (reader is T tResult)
            {
                return(tResult);
            }

            throw new NotSupportedException(StringHelper.Format("Cannot read a '{0}', It is a data {1}.", typeof(T).Name, "reader"));
        }
示例#3
0
        public T ReadValue(IValueReader valueReader)
        {
            if (valueReader is IValueReader <T> tReader)
            {
                return(tReader.ReadValue());
            }

            var value = valueReader.DirectRead();

            if (value is T tValue)
            {
                return(tValue);
            }

            var reader = RWHelper.CreateReader(value);

            if (reader is T tResult)
            {
                return(tResult);
            }

            throw new NotSupportedException($"Cannot read a '{typeof(T).Name}', It is a data {"reader"}.");
        }
示例#4
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));
        }