コード例 #1
0
ファイル: ProtoJsonParser.cs プロジェクト: kbmanikanta/WTalk
        private static object ParseObject(Type type, DynamicJson jArray)
        {
            if (jArray == null)
            {
                return(null);
            }

            object result     = ReflectionCache.GetConstructor(type).DynamicInvoke(null);
            var    properties = ReflectionCache.GetProperties(type);

            foreach (var property in properties)
            {
                try
                {
                    if (jArray.Count < property.Position || jArray[property.Position] == null)
                    {
                        continue;
                    }


                    if (property.IsList)
                    {
                        var   genericType = typeof(List <>).MakeGenericType(property.GenericType);
                        IList list        = (IList)Expression.Lambda(Expression.New(genericType)).Compile().DynamicInvoke(null);

                        if (jArray[property.Position].Count > 0)
                        {
                            object listItem = null;
                            foreach (DynamicJson jtoken in jArray[property.Position])
                            {
                                switch (property.ProtoJsonType)
                                {
                                case AttributeTypeEnum.ProtoContract:
                                    listItem = ParseObject(property.GenericType, jtoken);
                                    break;

                                case AttributeTypeEnum.Enum:
                                    listItem = Enum.Parse(property.GenericType, jtoken.ToString());
                                    break;

                                default:
                                    listItem = jtoken.Value;
                                    break;
                                }
                                if (listItem != null)
                                {
                                    list.Add(listItem);
                                }
                            }
                        }
                        property.Set(result, list);
                    }
                    else
                    {
                        switch (property.ProtoJsonType)
                        {
                        case AttributeTypeEnum.ProtoContract:
                            property.Set(result, ParseObject(property.GenericType, jArray[property.Position]));
                            break;

                        case AttributeTypeEnum.Enum:
                            if (!string.IsNullOrEmpty(jArray[property.Position].ToString()))
                            {
                                property.Set(result, Enum.Parse(property.GenericType, jArray[property.Position].ToString()));
                            }
                            break;

                        default:
                            if (property.GenericType.IsPrimitiveType() || !string.IsNullOrEmpty(jArray[property.Position].ToString()))
                            {
                                property.Set(result, Convert.ChangeType(jArray[property.Position].Value, property.GenericType));
                            }
                            break;
                        }
                    }
                }
                catch (Exception e) { }
            }


            return(result);
        }