private static bool CanCastToClass(this TypeDesc thisType, TypeDesc otherType, StackOverflowProtect protect) { TypeDesc curType = thisType; // If the target type has variant type parameters, we take a slower path if (curType.HasVariance) { // First chase inheritance hierarchy until we hit a class that only differs in its instantiation do { if (curType == otherType) { return true; } if (curType.CanCastByVarianceToInterfaceOrDelegate(otherType, protect)) { return true; } curType = curType.BaseType; } while (curType != null); } else { // If there are no variant type parameters, just chase the hierarchy // Allow curType to be nullable, which means this method // will additionally return true if curType is Nullable<T> && ( // currType == otherType // OR otherType is System.ValueType or System.Object) // Always strip Nullable from the otherType, if present if (otherType.IsNullable && !curType.IsNullable) { return thisType.CanCastTo(otherType.Instantiation[0]); } do { if (curType == otherType) return true; curType = curType.BaseType; } while (curType != null); } return false; }
// // Contra/CoVariance. // // IEnumerable<D> can cast to IEnumerable<B> if D can cast to B and if there's no possibility that D is a value type. // private static bool IsGcReferenceTypeAndCastableTo(this TypeInfo fromTypeInfo, TypeInfo toTypeInfo, FoundationTypes foundationTypes) { if (fromTypeInfo.Equals(toTypeInfo)) return true; if (fromTypeInfo.ProvablyAGcReferenceType(foundationTypes)) return fromTypeInfo.CanCastTo(toTypeInfo, foundationTypes); return false; }
// // Contra/CoVariance. // // IEnumerable<D> can cast to IEnumerable<B> if D can cast to B and if there's no possibility that D is a value type. // private static bool IsGcReferenceTypeAndCastableTo(this Type fromTypeInfo, Type toTypeInfo) { if (fromTypeInfo.Equals(toTypeInfo)) return true; if (fromTypeInfo.ProvablyAGcReferenceType()) return fromTypeInfo.CanCastTo(toTypeInfo); return false; }