Ejemplo n.º 1
0
        private static object Materialize(Type tType, MpMap map)
        {
            PropertyInfo[] props = GetSerializedProps(tType);
            Dictionary <string, object> propVals = map.GetTypedValue <Dictionary <string, object> >();

            object result = Activator.CreateInstance(tType);

            for (int t = props.Length - 1; t >= 0; t--)
            {
                object val;
                if (propVals.TryGetValue(props[t].Name, out val))
                {
                    Type propType = props[t].PropertyType;
                    if (!ReferenceEquals(val, null))
                    {
                        if (!MsgPackSerializer.NativelySupportedTypes.Contains(propType) &&
                            val is KeyValuePair <object, object>[])
                        {
                            val = Materialize(propType, new MpMap((KeyValuePair <object, object>[])val));
                        }
                        if (propType.IsArray && !(propType == typeof(object)))
                        {
                            // Need to cast object[] to whatever[]
                            object[] valAsArr = (object[])val;
                            propType = propType.GetElementType();
                            Array newInstance = Array.CreateInstance(propType, valAsArr.Length);

                            // Part of the check for complex types can be done outside the loop
                            bool complexTypes = !MsgPackSerializer.NativelySupportedTypes.Contains(propType);
                            for (int i = valAsArr.Length - 1; i >= 0; i--)
                            {
                                if (complexTypes && !ReferenceEquals(valAsArr[i], null) &&
                                    valAsArr[i] is KeyValuePair <object, object>[])
                                {
                                    valAsArr[i] = Materialize(propType, new MpMap((KeyValuePair <object, object>[])valAsArr[i]));
                                }
                                newInstance.SetValue(valAsArr[i], i);
                            }
                            props[t].SetValue(result, newInstance, null);
                            continue;
                        }
                        if (typeof(IList).IsAssignableFrom(propType))
                        {
                            IList newInstance = (IList)Activator.CreateInstance(propType);

                            object[] valAsArr = (object[])val;

                            // Part of the check for complex types can be done outside the loop
                            bool complexTypes = !MsgPackSerializer.NativelySupportedTypes.Contains(propType);
                            for (int i = 0; i < valAsArr.Length; i++)
                            {
                                if (complexTypes && !ReferenceEquals(valAsArr[i], null) &&
                                    valAsArr[i] is KeyValuePair <object, object>[])
                                {
                                    valAsArr[i] = Materialize(propType, new MpMap((KeyValuePair <object, object>[])valAsArr[i]));
                                }
                                newInstance.Add(valAsArr[i]);
                            }
                            props[t].SetValue(result, newInstance, null);
                            continue;
                        }
                    }
                    // Fix ArgumentException like "System.Byte cannot be converted to System.Nullable`1[System.Int32]"
                    Type nullableType = Nullable.GetUnderlyingType(props[t].PropertyType);
                    if (!(nullableType is null) && !(val is null))
                    {
                        if (val.GetType() != nullableType)
                        {
                            val = Convert.ChangeType(val, nullableType);
                        }
                    }
                    props[t].SetValue(result, val, null);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        private static object Materialize(Type tType, MpMap map)
        {
            PropertyInfo[] props = GetSerializedProps(tType);
            Dictionary <string, object> propVals = map.GetTypedValue <Dictionary <string, object> >();

            //object result = FormatterServices.GetUninitializedObject(tType);
            object result = Activator.CreateInstance(tType);

            for (int t = props.Length - 1; t >= 0; t--)
            {
                object val;
                if (propVals.TryGetValue(props[t].Name, out val))
                {
                    Type propType = props[t].PropertyType;
                    if (!ReferenceEquals(val, null))
                    {
                        if (!MsgPackSerializer.NativelySupportedTypes.Contains(propType) &&
                            val is KeyValuePair <object, object>[])
                        {
                            val = Materialize(propType, new MpMap((KeyValuePair <object, object>[])val, map.Settings));
                        }
                        if (propType.IsArray && !(propType == typeof(object)))
                        {
                            // Need to cast object[] to whatever[]
                            object[] valAsArr = (object[])val;
                            propType = propType.GetElementType();
                            Array newInstance = Array.CreateInstance(propType, valAsArr.Length);

                            // Part of the check for complex types can be done outside the loop
                            bool complexTypes = !MsgPackSerializer.NativelySupportedTypes.Contains(propType);
                            for (int i = valAsArr.Length - 1; i >= 0; i--)
                            {
                                if (complexTypes && !ReferenceEquals(valAsArr[i], null) &&
                                    valAsArr[i] is KeyValuePair <object, object>[])
                                {
                                    valAsArr[i] = Materialize(propType, new MpMap((KeyValuePair <object, object>[])valAsArr[i], map.Settings));
                                }
                                newInstance.SetValue(valAsArr[i], i);
                            }
                            props[t].SetValue(result, newInstance, null);
                            continue;
                        }
                        if (typeof(IList).IsAssignableFrom(propType))
                        {
                            //IList newInstance = (IList)FormatterServices.GetUninitializedObject(propType);
                            //ConstructorInfo constructor = propType.GetConstructor(Type.EmptyTypes);
                            //constructor.Invoke(newInstance, null);
                            IList    newInstance = (IList)Activator.CreateInstance(propType);
                            object[] valAsArr    = (object[])val;
                            // Part of the check for complex types can be done outside the loop
                            bool complexTypes = !MsgPackSerializer.NativelySupportedTypes.Contains(propType);
                            for (int i = 0; i < valAsArr.Length; i++)
                            {
                                if (complexTypes && !ReferenceEquals(valAsArr[i], null) &&
                                    valAsArr[i] is KeyValuePair <object, object>[])
                                {
                                    valAsArr[i] = Materialize(propType, new MpMap((KeyValuePair <object, object>[])valAsArr[i], map.Settings));
                                }
                                newInstance.Add(valAsArr[i]);
                            }
                            props[t].SetValue(result, newInstance, null);
                            continue;
                        }
                    }
                    props[t].SetValue(result, val, null);
                }
            }

            return(result);
        }