public virtual bool IsAssignableFrom(TypeInfo typeInfo)
 {
     if (typeInfo == null)
     {
         return(false);
     }
     if (this == typeInfo)
     {
         return(true);
     }
     if (typeInfo.IsSubclassOf(this))
     {
         return(true);
     }
     if (base.IsInterface)
     {
         return(typeInfo.ImplementInterface(this));
     }
     if (this.IsGenericParameter)
     {
         Type[] genericParameterConstraints = this.GetGenericParameterConstraints();
         for (int i = 0; i < genericParameterConstraints.Length; i++)
         {
             if (!genericParameterConstraints[i].IsAssignableFrom(typeInfo))
             {
                 return(false);
             }
         }
         return(true);
     }
     return(false);
 }
예제 #2
0
        public virtual bool IsAssignableFrom(TypeInfo typeInfo)
        {
            if (typeInfo == null)
                return false;

            if (this == typeInfo)
                return true;

            // If c is a subclass of this class, then c can be cast to this type.
            if (typeInfo.IsSubclassOf(this))
                return true;

            if (this.IsInterface)
            {
                return typeInfo.ImplementInterface(this);
            }
            else if (IsGenericParameter)
            {
                Type[] constraints = GetGenericParameterConstraints();
                for (int i = 0; i < constraints.Length; i++)
                    if (!constraints[i].IsAssignableFrom(typeInfo))
                        return false;

                return true;
            }

            return false;
        }
예제 #3
0
 public virtual bool IsAssignableFrom(TypeInfo typeInfo)
 {
     if ((Type)typeInfo == (Type)null)
     {
         return(false);
     }
     if ((Type)this == (Type)typeInfo || typeInfo.IsSubclassOf((Type)this))
     {
         return(true);
     }
     if (this.IsInterface)
     {
         return(typeInfo.ImplementInterface((Type)this));
     }
     if (!this.IsGenericParameter)
     {
         return(false);
     }
     foreach (Type parameterConstraint in this.GetGenericParameterConstraints())
     {
         if (!parameterConstraint.IsAssignableFrom((Type)typeInfo))
         {
             return(false);
         }
     }
     return(true);
 }
예제 #4
0
파일: TypeInfo.cs 프로젝트: noahfalk/corert
        public virtual bool IsAssignableFrom(TypeInfo typeInfo)
        {
            if (typeInfo == null)
            {
                return(false);
            }

            if (this.Equals(typeInfo))
            {
                return(true);
            }

            // If c is a subclass of this class, then typeInfo can be cast to this type.
            if (typeInfo.IsSubclassOf(this.AsType()))
            {
                return(true);
            }

            if (this.IsInterface)
            {
                foreach (Type implementedInterface in typeInfo.ImplementedInterfaces)
                {
                    TypeInfo resolvedImplementedInterface = implementedInterface.GetTypeInfo();
                    if (resolvedImplementedInterface.Equals(this))
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (IsGenericParameter)
            {
                Type[] constraints = GetGenericParameterConstraints();
                for (int i = 0; i < constraints.Length; i++)
                {
                    if (!constraints[i].GetTypeInfo().IsAssignableFrom(typeInfo))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }
예제 #5
0
        /// <summary>
        /// Returns a value that indicates whether the specified type can be assigned to the current type.
        /// </summary>
        /// <param name="typeInfo">The type to check.</param>
        /// <returns>True if the specified type can be assigned to this type; otherwise, False.</returns>
        public virtual bool IsAssignableFrom(TypeInfo typeInfo)
        {
            if (typeInfo == null)
            {
                return(false);
            }

            if (typeInfo == this)
            {
                return(true);
            }

            if (typeInfo.IsSubclassOf(this.AsType()))
            {
                return(true);
            }

            return(false);
        }
예제 #6
0
        //a re-implementation of ISAF from Type, skipping the use of UnderlyingType
        public virtual bool IsAssignableFrom(TypeInfo typeInfo)
        {
            if (typeInfo == null)
            {
                return(false);
            }

            if (this == typeInfo)
            {
                return(true);
            }

            // If c is a subclass of this class, then c can be cast to this type.
            if (typeInfo.IsSubclassOf(this))
            {
                return(true);
            }

            if (this.IsInterface)
            {
                return(typeInfo.ImplementInterface(this));
            }
            else if (IsGenericParameter)
            {
                Type[] constraints = GetGenericParameterConstraints();
                for (int i = 0; i < constraints.Length; i++)
                {
                    if (!constraints[i].IsAssignableFrom(typeInfo))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }
예제 #7
0
        /// <summary>
        /// Returns a value that indicates whether the specified type can be assigned to the current type.
        /// </summary>
        /// <param name="typeInfo">The type to check.</param>
        /// <returns>True if the specified type can be assigned to this type; otherwise, False.</returns>
        public virtual bool IsAssignableFrom(TypeInfo typeInfo)
        {
            if (typeInfo == null)
                return false;

            if (typeInfo == this)
                return true;

            if (typeInfo.IsSubclassOf(this.AsType()))
                return true;

            return false;
        }
예제 #8
0
        //
        // Returns the effective set of custom attributes on a reflection element.
        //
        public IEnumerable <CustomAttributeData> GetMatchingCustomAttributes(E element, Type optionalAttributeTypeFilter, bool inherit, bool skipTypeValidation = false)
        {
            // Do all parameter validation here before we enter the iterator function (so that exceptions from validations
            // show up immediately rather than on the first MoveNext()).
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            bool typeFilterKnownToBeSealed = false;

            if (!skipTypeValidation)
            {
                if (optionalAttributeTypeFilter == null)
                {
                    throw new ArgumentNullException("type");
                }
                TypeInfo attributeTypeFilterInfo = optionalAttributeTypeFilter.GetTypeInfo();
                if (!(optionalAttributeTypeFilter.Equals(CommonRuntimeTypes.Attribute) ||
                      attributeTypeFilterInfo.IsSubclassOf(CommonRuntimeTypes.Attribute)))
                {
                    throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
                }

                try
                {
                    typeFilterKnownToBeSealed = attributeTypeFilterInfo.IsSealed;
                }
                catch (MissingMetadataException)
                {
                    // If we got here, the custom attribute type itself was not opted into metadata. This can and does happen in the real world when an app
                    // contains a check for custom attributes that never actually appear on any entity within the app.
                    //
                    // Since "typeFilterKnownToBeSealed" is only used to enable an optimization, it's always safe to leave it "false".
                    //
                    // Because the Project N toolchain removes any custom attribute that refuses to opt into metadata so at this point,
                    // we could simply return an empty enumeration and "be correct." However, the code paths following this already do that naturally.
                    // (i.e. the "passFilter" will never return true, thus we will never attempt to query the custom attribute type for its
                    // own AttributeUsage custom attribute.) If the toolchain behavior changes in the future, it's preferable that
                    // this shows up as new MissingMetadataExceptions rather than incorrect results from the api so we will not put
                    // in an explicit return here.
                }
            }

            Func <Type, bool> passesFilter;

            if (optionalAttributeTypeFilter == null)
            {
                passesFilter =
                    delegate(Type actualType)
                {
                    return(true);
                };
            }
            else
            {
                passesFilter =
                    delegate(Type actualType)
                {
                    if (optionalAttributeTypeFilter.Equals(actualType))
                    {
                        return(true);
                    }
                    if (typeFilterKnownToBeSealed)
                    {
                        return(false);
                    }
                    return(actualType.GetTypeInfo().IsSubclassOf(optionalAttributeTypeFilter));
                };
            }

            return(GetMatchingCustomAttributesIterator(element, passesFilter, inherit));
        }
예제 #9
0
 public static bool IsSameOrSubClass(this TypeInfo subclass, TypeInfo baseclass)
 => subclass.IsSubclassOf(baseclass.AsType()) || subclass == baseclass;