Exemple #1
0
        public static Type MakeGenericType(Type baseType, Type[] types)
        {
            // emulate Nullable<T>
            if (baseType == typeof(Nullable <>))
            {
                if (types.Length != 1)
                {
                    ThrowHelper.ThrowArgumentException(ExceptionResource.WrongNumberOfArguments);
                }

                var ret = NullableReflection.GetNullableTFromUnterlyingType(types[0]);

                if (ret != null)
                {
                    return(ret);
                }

                // can't create a nullable instance of non-predifined nullable,
                // because of all the compiler-voodoo around Nullables.
                return(null);
            }

            var expected = GetGenericArgumentCount(baseType);

            if (types.Length != expected)
            {
                throw new ArgumentOutOfRangeException("types", string.Format("exected {0} generic arguments, got {1} for type {2}", expected, types.Length, baseType.FullName));
            }

            return(GenericInstanceFactory.GetOrMakeGenericInstanceType(baseType, types));
        }
Exemple #2
0
        public static Type[] GetGenericArguments(Type type)
        {
            Type[] ret;

            // generic instance proxy?
            var genericTypeInfo = GenericInstanceFactory.GetGenericTypeInfo(type);

            if (genericTypeInfo != null)
            {
                return((Type[])genericTypeInfo.AddOrGetGenericParameters(createTypeArray: true));
            }

            // Nullable<T>?
            Type nullableUnderlying = NullableReflection.GetUnderlyingType(type);

            if (nullableUnderlying != null)
            {
                return(new[] { nullableUnderlying });
            }

            // java generic type?
            var javaTypeParam = type.GetTypeParameters();

            if (javaTypeParam.Length != 0)
            {
                // java generic type. provide some emulation.
                ret = new Type[javaTypeParam.Length];
                for (int i = 0; i < javaTypeParam.Length; ++i)
                {
                    ret[i] = typeof(object);
                }
                return(ret);
            }

            var info = GetOrCreateGenericsInfo(type);

            if (info.GenericArgumentCount > 0)
            {
                // generic definition? We can only return the number of objects.
                ret = new Type[info.GenericArgumentCount];
                for (int i = 0; i < ret.Length; ++i)
                {
                    ret[i] = typeof(object);
                }
                return(ret);
            }

            throw new InvalidOperationException("not a generic type: " + type.FullName);
        }
Exemple #3
0
        private static GenericsInfo CreateGenericsInfo(Type type)
        {
            var info = new GenericsInfo();

            if (NullableReflection.TreatAsSystemNullableT(type))
            {
                info.GenericArgumentCount = -1;
            }
            else if (GenericInstanceFactory.IsGenericInstanceType(type))
            {
                info.GenericArgumentCount = -1;
            }
            else
            {
                var annotation = type.GetAnnotation <ITypeReflectionInfo>(typeof(ITypeReflectionInfo));
                var args       = annotation != null?annotation.GenericArgumentCount() : 0;

                if (args > 0)
                {
                    var fieldNames = annotation.GenericArgumentsFields();
                    if (fieldNames.Length > 0)
                    {
                        var fields = type.JavaGetDeclaredFields();
                        info.GenericInstanceFields = fieldNames.Select(name =>
                        {
                            var field          = fields.FirstOrDefault(f => f.Name == name);
                            field.IsAccessible = true;
                            return(field);
                        });
                        info.GenericInstanceFieldIsTypeArray = info.GenericInstanceFields.Length == 1 &&
                                                               info.GenericInstanceFields[0].Type == typeof(Type[]);
                    }
                }
                info.GenericArgumentCount = args == 0 ? -1 : args;
            }

            return(info);
        }
Exemple #4
0
        public static bool IsGenericType(Type type)
        {
            // Java generic type?
            // this call makes Json.NET deserialization about 2.5 times as slow.
            // If Java generics have any meanings to us, we would need to cache the
            // return value.
            //bool hasTypeParameters = type.GetTypeParameters().Length > 0;
            //if (hasTypeParameters) return true;

            // Nullable<T>?
            if (NullableReflection.TreatAsSystemNullableT(type))
            {
                return(true);
            }

            // generic proxy?
            if (GenericInstanceFactory.IsGenericInstanceType(type))
            {
                return(true);
            }

            // .NET generic type definition?
            return(IsGenericTypeDefinition(type));
        }
Exemple #5
0
        public static Type GetGenericTypeDefinition(Type type)
        {
            // emulate Nullable<T>
            if (NullableReflection.TreatAsSystemNullableT(type))
            {
                return(typeof(Nullable <>));
            }

            // is generic proxy?
            var ret = GenericInstanceFactory.GetGenericTypeDefinition(type);

            if (ret != null)
            {
                return(ret);
            }

            // is generic type? just return itself.
            if (!IsGenericType(type))
            {
                ThrowHelper.ThrowInvalidOperationException(ExceptionResource.NotAGenericType);
            }

            return(type);
        }