コード例 #1
0
    private static object DeserializeObject(Type type, Dictionary <string, object> table)
    {
        IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(type, BindingFlags.Instance | BindingFlags.Public);

        object v = Activator.CreateInstance(type);

        foreach (MemberInfo member in members)
        {
#if !NETFX_CORE
            MemberTypes memberType = member.MemberType;
            if (memberType != MemberTypes.Field && memberType != MemberTypes.Property)
            {
                continue;
            }
#else
            MemberTypes memberType;
            if (member is PropertyInfo)
            {
                memberType = MemberTypes.Property;
            }
            else if (member is FieldInfo)
            {
                memberType = MemberTypes.Field;
            }
            else
            {
                continue;
            }
#endif

            if (memberType == MemberTypes.Property && !((PropertyInfo)member).CanWrite)
            {
                continue;
            }
            object item;

#if !NETFX_CORE
            object[]       attributes = member.GetCustomAttributes(typeof(AliasAttribute), true);
            AliasAttribute alias      = attributes.Length > 0 ? attributes[0] as AliasAttribute : null;
#else
            IEnumerable <Attribute> attributes = member.GetCustomAttributes(typeof(AliasAttribute), true);
            AliasAttribute          alias      = null;
            foreach (Attribute a in attributes)
            {
                alias = a as AliasAttribute;
                break;
            }
#endif
            if (alias == null || !alias.ignoreFieldName)
            {
                if (table.TryGetValue(member.Name, out item))
                {
                    DeserializeValue(memberType, member, item, v);
                    continue;
                }
            }
            if (alias != null)
            {
                for (int j = 0; j < alias.aliases.Length; j++)
                {
                    if (table.TryGetValue(alias.aliases[j], out item))
                    {
                        DeserializeValue(memberType, member, item, v);
                        break;
                    }
                }
            }
        }

        return(v);
    }
コード例 #2
0
    public void DeserializeObject(object obj, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public)
    {
        IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(obj.GetType(), bindingFlags);

        DeserializeObject(obj, members);
    }
コード例 #3
0
    public override object Deserialize(Type type)
    {
        IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(type, BindingFlags.Instance | BindingFlags.Public);

        return(Deserialize(type, members));
    }
コード例 #4
0
    public override object Deserialize(Type type)
    {
        if (_count == 0)
        {
            return(null);
        }

        if (type.IsArray)
        {
            Type  elementType = type.GetElementType();
            Array v           = Array.CreateInstance(elementType, _count);
            if (array[0] is OnlineMapsJSONObject)
            {
                IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(elementType, BindingFlags.Instance | BindingFlags.Public);
                for (int i = 0; i < _count; i++)
                {
                    OnlineMapsJSONItem child = array[i];
                    object             item  = (child as OnlineMapsJSONObject).Deserialize(elementType, members);
                    v.SetValue(item, i);
                }
            }
            else
            {
                for (int i = 0; i < _count; i++)
                {
                    OnlineMapsJSONItem child = array[i];
                    object             item  = child.Deserialize(elementType);
                    v.SetValue(item, i);
                }
            }

            return(v);
        }
        if (OnlineMapsReflectionHelper.IsGenericType(type))
        {
            Type   listType = OnlineMapsReflectionHelper.GetGenericArguments(type)[0];
            object v        = Activator.CreateInstance(type);

            if (array[0] is OnlineMapsJSONObject)
            {
                IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(listType, BindingFlags.Instance | BindingFlags.Public);
                for (int i = 0; i < _count; i++)
                {
                    OnlineMapsJSONItem child = array[i];
                    object             item  = (child as OnlineMapsJSONObject).Deserialize(listType, members);
                    try
                    {
                        MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add");
                        if (methodInfo != null)
                        {
                            methodInfo.Invoke(v, new[] { item });
                        }
                    }
                    catch
                    {
                    }
                }
            }
            else
            {
                for (int i = 0; i < _count; i++)
                {
                    OnlineMapsJSONItem child = array[i];
                    object             item  = child.Deserialize(listType);
                    try
                    {
                        MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add");
                        if (methodInfo != null)
                        {
                            methodInfo.Invoke(v, new[] { item });
                        }
                    }
                    catch
                    {
                    }
                }
            }

            return(v);
        }


        return(null);
    }