Exemple #1
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 #2
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 #3
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);
        }