Пример #1
0
        private Type ResolveType(TypeSerializationInfo info)
        {
            if (!_typeNameShortener.TryExpand(info.Name, out Type type))
            {
                if (_assemblyNameShortener.TryExpand(info.Assembly?.Name, out Assembly assembly))
                {
                    info.Assembly = assembly.ToAssemblySerializationInfo();
                }

                var infoForResolving = info.GenericArgs?.Length > 0
                    ? new TypeSerializationInfo
                {
                    Name     = info.Name,
                    Assembly = info.Assembly
                }
                    : info;
                type = _typeResolver.Resolve(infoForResolving);
            }

            if (type.IsGenericTypeDefinition())
            {
                var genericArguments = new Type[info.GenericArgs.Length];
                for (var i = 0; i < genericArguments.Length; i++)
                {
                    var genericArgument = ResolveType(info.GenericArgs[i]);
                    genericArguments[i] = genericArgument;
                }
                type = type.MakeGenericType(genericArguments);
            }

            return(type);
        }
Пример #2
0
        public object Compose(IValueContainer container, Type valueType)
        {
            var c = (EntityProjectionContainer)container;
            var projectionInterface = _typeNameShortener.TryExpand(c.Type, out var type) ? type : Type.GetType(c.Type);
            var result = EntityProjection.CreateInstance(projectionInterface);

            if (c.Properties != null)
            {
                foreach (var pair in c.Properties)
                {
                    // BAD CODE
                    // The problem is in JSON serialization, so we have to perform some extra type checks and conversion.
                    // I know, it's bad, just need to make it work as a quick-fix without going back to the drawing board.
                    var value = pair.Value;
                    if (value != null)
                    {
                        var expectedValueType = projectionInterface.GetProperty(pair.Key).PropertyType;
                        var actualValueType   = value.GetType();
                        if (actualValueType != expectedValueType)
                        {
                            if ((expectedValueType == typeof(Guid) || expectedValueType == typeof(Guid?)) && actualValueType == typeof(string))
                            {
                                value = Guid.Parse((string)value);
                            }
                            else if (expectedValueType == typeof(Uri) && actualValueType == typeof(string))
                            {
                                value = new Uri((string)value);
                            }
                            else if (expectedValueType.IsEnum)
                            {
                                value = Enum.ToObject(expectedValueType, value);
                            }
                            else if (expectedValueType.IsGenericType && !expectedValueType.IsClass && expectedValueType.Name == "Nullable`1")
                            {
                                var nullableValueType = expectedValueType.GetGenericArguments()[0];
                                if (nullableValueType != actualValueType)
                                {
                                    value = Convert.ChangeType(value, nullableValueType);
                                }
                                value = Activator.CreateInstance(expectedValueType, value);
                            }
                            else
                            {
                                value = Convert.ChangeType(value, expectedValueType);
                            }
                        }
                    }
                    // BAD CODE

                    EntityProjection.SetValue(result, pair.Key, value);
                }
            }
            return(result);
        }
Пример #3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            var typeName = reader.Value as string;

            if (!_typeNameShortener.TryExpand(typeName, out var type))
            {
                type = Type.GetType(typeName);
            }

            return(type);
        }