コード例 #1
0
        /// <summary>
        /// Gets the fields.
        /// </summary>
        /// <param name="typeInfo">The <see cref="TypeInfo"/>.</param>
        /// <param name="bindingFlags">The binding flags.</param>
        /// <returns>An array of <see cref="FieldInfo"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="typeInfo"/> is <c>null</c>.</exception>
        public static FieldInfo[] GetFields(this TypeInfo typeInfo, BindingFlags bindingFlags)
        {
            Argument.IsNotNull("typeInfo", typeInfo);

            var flattenHierarchy = ShouldFlattenHierarchy(bindingFlags);
            var source = flattenHierarchy ? typeInfo.AsType().GetRuntimeFields().ToList() : typeInfo.DeclaredFields.ToList();

            var includeStatics = Enum<BindingFlags>.Flags.IsFlagSet(bindingFlags, BindingFlags.Static);

            // TODO: This is a fix because static members are not included in FlattenHierarcy, remove when this is fixed in WinRT
            if (flattenHierarchy)
            {
                var baseType = typeInfo.BaseType;
                if ((baseType != null) && (baseType != typeof(object)))
                {
                    source.AddRange(from member in GetFields(baseType.GetTypeInfo(), bindingFlags)
                                    where member.IsStatic
                                    select member);
                }
            }

            if (!includeStatics)
            {
                source = (from x in source
                            where !x.IsStatic
                            select x).ToList();
            }

            return (from x in source
                    where x.IsStatic == includeStatics
                    select x).ToArray();
        }
コード例 #2
0
        internal static IEnumerable<MethodInfo> GetDeclaredMethodsRecursively(this TypeInfo typeInfo)
        {
            if (typeInfo == null)
            {
                return null;
            }

            var methods = GetDeclaredMethodsRecursively(typeInfo.AsType(), new List<MethodInfo>());
            return methods;
        }
コード例 #3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static Type GetCollectionType(this TypeInfo info)
        {
            if (info == EnumerableTypeInfo) { return Types.Enumerable; }
            if (info == GenericEnumerableTypeInfo) { return Types.GenericEnumerable; }
            if (info.IsGenericType && info.GetGenericTypeDefinition() == Types.GenericEnumerable) { return info.AsType(); }

            return info.IsCollection() ? info.GetAllImplementedInterfaces()
                    .Where(x => x.GetTypeInfo().IsGenericType)
                    .FirstOrDefault(x => x.GetGenericTypeDefinition() == Types.GenericEnumerable) // return IEnumerable<T>
                    ?? Types.Enumerable // return IEnumerable
                : null;
        }
コード例 #4
0
        public static IEnumerable<Type> BaseTypes(this TypeInfo typeInfo, bool includeSelf = false)
        {
            if (includeSelf)
                yield return typeInfo.AsType();

            var baseType = typeInfo.BaseType;

            while (baseType != null)
            {
                yield return baseType;

                baseType = baseType.GetTypeInfo().BaseType;
            }
        }
コード例 #5
0
        public static Boolean IsNullable(this TypeInfo typeInfo)
        {
            if (typeInfo == null)
            {
                throw Logger.Fatal.ArgumentNull(nameof(typeInfo));
            }

            if (typeInfo.IsValueType)
            {
                return Nullable.GetUnderlyingType(typeInfo.AsType()) != null;
            }

            return true;
        }
コード例 #6
0
        /// <summary>
        /// Gets the events.
        /// </summary>
        /// <param name="typeInfo">The <see cref="TypeInfo"/>.</param>
        /// <param name="bindingFlags">The binding flags.</param>
        /// <returns>An array of <see cref="EventInfo"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="typeInfo"/> is <c>null</c>.</exception>
        public static EventInfo[] GetEvents(this TypeInfo typeInfo, BindingFlags bindingFlags)
        {
            Argument.IsNotNull("typeInfo", typeInfo);

            var flattenHierarchy = ShouldFlattenHierarchy(bindingFlags);
            var eventsSource = flattenHierarchy ? typeInfo.AsType().GetRuntimeEvents() : typeInfo.DeclaredEvents;

            return (from x in eventsSource
                    select x).ToArray();
        }
コード例 #7
0
        /// <summary>
        /// Gets the properties.
        /// </summary>
        /// <param name="typeInfo">The <see cref="TypeInfo"/>.</param>
        /// <param name="bindingFlags">The binding flags.</param>
        /// <returns>An array of <see cref="PropertyInfo"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="typeInfo"/> is <c>null</c>.</exception>
        public static PropertyInfo[] GetProperties(this TypeInfo typeInfo, BindingFlags bindingFlags)
        {
            Argument.IsNotNull("typeInfo", typeInfo);

            var flattenHierarchy = ShouldFlattenHierarchy(bindingFlags);
            var source = flattenHierarchy ? typeInfo.AsType().GetRuntimeProperties() : typeInfo.DeclaredProperties;

            return (from x in source
                    select x).ToArray();
        }
コード例 #8
0
ファイル: TypeRefExtensions.cs プロジェクト: jrgcubano/zetbox
        public static object Create(this TypeRef t, params object[] parameter)
        {
            if (t == null) { throw new ArgumentNullException("t"); }

            return Activator.CreateInstance(t.AsType(true), parameter);
        }
コード例 #9
0
ファイル: TypeUtilities.cs プロジェクト: Rejendo/orleans
 internal static bool IsOrleansPrimitive(this TypeInfo typeInfo)
 {
     var t = typeInfo.AsType();
     return typeInfo.IsPrimitive || typeInfo.IsEnum || t == typeof(string) || t == typeof(DateTime) || t == typeof(Decimal) || (typeInfo.IsArray && typeInfo.GetElementType().GetTypeInfo().IsOrleansPrimitive());
 }
コード例 #10
0
ファイル: Assignability.cs プロジェクト: noahfalk/corert
        //
        // Check a base type or implemented interface type for equivalence (taking into account variance for generic instantiations.)
        // Does not check ancestors recursively.
        //
        private static bool MatchesWithVariance(this TypeInfo fromTypeInfo, TypeInfo toTypeInfo, FoundationTypes foundationTypes)
        {
            Debug.Assert(!(fromTypeInfo.IsArray || fromTypeInfo.IsByRef || fromTypeInfo.IsPointer || fromTypeInfo.IsGenericParameter));
            Debug.Assert(!(toTypeInfo.IsArray || toTypeInfo.IsByRef || toTypeInfo.IsPointer || toTypeInfo.IsGenericParameter));

            if (fromTypeInfo.Equals(toTypeInfo))
                return true;

            if (!(fromTypeInfo.AsType().IsConstructedGenericType && toTypeInfo.AsType().IsConstructedGenericType))
                return false;

            TypeInfo genericTypeDefinition = fromTypeInfo.GetGenericTypeDefinition().GetTypeInfo();
            if (!genericTypeDefinition.AsType().Equals(toTypeInfo.GetGenericTypeDefinition()))
                return false;

            Type[] fromTypeArguments = fromTypeInfo.GenericTypeArguments;
            Type[] toTypeArguments = toTypeInfo.GenericTypeArguments;
            Type[] genericTypeParameters = genericTypeDefinition.GenericTypeParameters;
            for (int i = 0; i < genericTypeParameters.Length; i++)
            {
                TypeInfo fromTypeArgumentInfo = fromTypeArguments[i].GetTypeInfo();
                TypeInfo toTypeArgumentInfo = toTypeArguments[i].GetTypeInfo();

                GenericParameterAttributes attributes = genericTypeParameters[i].GetTypeInfo().GenericParameterAttributes;
                switch (attributes & GenericParameterAttributes.VarianceMask)
                {
                    case GenericParameterAttributes.Covariant:
                        if (!(fromTypeArgumentInfo.IsGcReferenceTypeAndCastableTo(toTypeArgumentInfo, foundationTypes)))
                            return false;
                        break;

                    case GenericParameterAttributes.Contravariant:
                        if (!(toTypeArgumentInfo.IsGcReferenceTypeAndCastableTo(fromTypeArgumentInfo, foundationTypes)))
                            return false;
                        break;

                    case GenericParameterAttributes.None:
                        if (!(fromTypeArgumentInfo.Equals(toTypeArgumentInfo)))
                            return false;
                        break;

                    default:
                        throw new BadImageFormatException();  // Unexpected variance value in metadata.
                }
            }
            return true;
        }
コード例 #11
0
 static bool IsSystemVoid(this TypeInfo type)
 {
     return CommonRuntimeTypes.Void.Equals(type.AsType());
 }
コード例 #12
0
 static bool IsSystemArray(this TypeInfo type)
 {
     return CommonRuntimeTypes.Array.Equals(type.AsType());
 }
コード例 #13
0
 static bool IsSystemObject(this TypeInfo type)
 {
     return CommonRuntimeTypes.Object.Equals(type.AsType());
 }
コード例 #14
0
 private static bool IsSystemValueType(this TypeInfo type)
 {
     return CommonRuntimeTypes.ValueType.Equals(type.AsType());
 }
コード例 #15
0
ファイル: Assignability.cs プロジェクト: noahfalk/corert
        //
        // A[] can cast to B[] if one of the following are true:
        //
        //    A can cast to B under variance rules.
        //
        //    A and B are both integers or enums and have the same reduced type (i.e. represent the same-sized integer, ignoring signed/unsigned differences.)
        //        "char" is not interchangable with short/ushort. "bool" is not interchangable with byte/sbyte.
        //
        // For desktop compat, A& and A* follow the same rules.
        //
        private static bool IsElementTypeCompatibleWith(this TypeInfo fromTypeInfo, TypeInfo toTypeInfo, FoundationTypes foundationTypes)
        {
            if (fromTypeInfo.IsGcReferenceTypeAndCastableTo(toTypeInfo, foundationTypes))
                return true;

            Type reducedFromType = fromTypeInfo.AsType().ReducedType(foundationTypes);
            Type reducedToType = toTypeInfo.AsType().ReducedType(foundationTypes);
            if (reducedFromType.Equals(reducedToType))
                return true;

            return false;
        }
コード例 #16
0
        unsafe static int NormalizedPrimitiveTypeSizeForIntegerTypes(this TypeInfo type)
        {
            // Strip InstantiatedTypeInfo - IsEnum is not implemented on InstantiatedTypeInfo
            if (type is InstantiatedTypeInfo)
                type = ((InstantiatedTypeInfo)type).UnderlyingTypeInfo;

            Type normalizedType;

            if (type.IsEnum)
            {
                // TODO: Enum.GetUnderlyingType does not work for generic type definitions
                return NormalizedPrimitiveTypeSizeForIntegerTypes(Enum.GetUnderlyingType(type.AsType()).GetTypeInfo());
            }
            else
            if (type.IsPrimitive)
            {
                normalizedType = type.AsType();
            }
            else
            {
                return 0;
            }

            if (CommonRuntimeTypes.Byte.Equals(normalizedType) || CommonRuntimeTypes.SByte.Equals(normalizedType))
                return 1;

            if (CommonRuntimeTypes.UInt16.Equals(normalizedType) || CommonRuntimeTypes.Int16.Equals(normalizedType))
                return 2;

            if (CommonRuntimeTypes.UInt32.Equals(normalizedType) || CommonRuntimeTypes.Int32.Equals(normalizedType))
                return 4;

            if (CommonRuntimeTypes.UInt64.Equals(normalizedType) || CommonRuntimeTypes.Int64.Equals(normalizedType))
                return 8;

            if (CommonRuntimeTypes.UIntPtr.Equals(normalizedType) || CommonRuntimeTypes.IntPtr.Equals(normalizedType))
                return sizeof(IntPtr);

            return 0;
        }
コード例 #17
0
		public static TypeAdapter Adapt( this TypeInfo @this ) => Adapt( @this.AsType() );