GetNonNullableType() public static method

public static GetNonNullableType ( this type ) : Type
type this
return Type
Esempio n. 1
0
        //CONFORMING
        internal static MethodInfo GetUserDefinedCoercionMethod(Type convertFrom, Type convertToType, bool implicitOnly)
        {
            // check for implicit coercions first
            Type nnExprType = TypeUtils.GetNonNullableType(convertFrom);
            Type nnConvType = TypeUtils.GetNonNullableType(convertToType);

            // try exact match on types
            MethodInfo[] eMethods = nnExprType.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            MethodInfo   method   = FindConversionOperator(eMethods, convertFrom, convertToType, implicitOnly);

            if (method != null)
            {
                return(method);
            }
            MethodInfo[] cMethods = nnConvType.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            method = FindConversionOperator(cMethods, convertFrom, convertToType, implicitOnly);
            if (method != null)
            {
                return(method);
            }
            // try lifted conversion
            if (nnExprType != convertFrom || nnConvType != convertToType)
            {
                method = FindConversionOperator(eMethods, nnExprType, nnConvType, implicitOnly);
                if (method == null)
                {
                    method = FindConversionOperator(cMethods, nnExprType, nnConvType, implicitOnly);
                }
                if (method != null)
                {
                    return(method);
                }
            }
            return(null);
        }
Esempio n. 2
0
        public static MethodInfo GetUserDefinedCoercionMethod(Type convertFrom, Type convertToType, bool implicitOnly)
        {
            // check for implicit coercions first
            Type nnExprType = TypeUtils.GetNonNullableType(convertFrom);
            Type nnConvType = TypeUtils.GetNonNullableType(convertToType);

            bool retryForLifted = !AreEquivalent(nnExprType, convertFrom) || !AreEquivalent(nnConvType, convertToType);

            // try exact match on types
            IEnumerable <MethodInfo> eMethods = nnExprType.GetStaticMethods();

            if (retryForLifted)
            {
                // If this may be scanned again for a lifted match, store it in a list.
                eMethods = new List <MethodInfo>(eMethods);
            }

            MethodInfo method = FindConversionOperator(eMethods, convertFrom, convertToType, implicitOnly);

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

            IEnumerable <MethodInfo> cMethods = nnConvType.GetStaticMethods();

            if (retryForLifted)
            {
                cMethods = new List <MethodInfo>(cMethods);
            }

            method = FindConversionOperator(cMethods, convertFrom, convertToType, implicitOnly);
            if (method != null)
            {
                return(method);
            }

            // try lifted conversion
            if (retryForLifted)
            {
                return(FindConversionOperator(eMethods, nnExprType, nnConvType, implicitOnly)
                       ?? FindConversionOperator(cMethods, nnExprType, nnConvType, implicitOnly));
            }

            return(null);
        }
Esempio n. 3
0
        internal static bool HasReferenceConversion(Type source, Type dest)
        {
            Debug.Assert(source != null && dest != null);

            // void -> void conversion is handled elsewhere
            // (it's an identity conversion)
            // All other void conversions are disallowed.
            if (source == typeof(void) || dest == typeof(void))
            {
                return(false);
            }

            Type nnSourceType = TypeUtils.GetNonNullableType(source);
            Type nnDestType   = TypeUtils.GetNonNullableType(dest);

            // Down conversion
            if (nnSourceType.IsAssignableFrom(nnDestType))
            {
                return(true);
            }
            // Up conversion
            if (nnDestType.IsAssignableFrom(nnSourceType))
            {
                return(true);
            }
            // Interface conversion
            if (source.IsInterface || dest.IsInterface)
            {
                return(true);
            }
            // Variant delegate conversion
            if (IsLegalExplicitVariantDelegateConversion(source, dest))
            {
                return(true);
            }

            // Object conversion
            if (source == typeof(object) || dest == typeof(object))
            {
                return(true);
            }
            return(false);
        }
Esempio n. 4
0
        public static MethodInfo GetUserDefinedCoercionMethod(Type convertFrom, Type convertToType, bool implicitOnly)
        {
            // check for implicit coercions first
            Type nnExprType = TypeUtils.GetNonNullableType(convertFrom);
            Type nnConvType = TypeUtils.GetNonNullableType(convertToType);

            // try exact match on types
            MethodInfo[] eMethods = nnExprType.GetStaticMethods();
            MethodInfo   method   = FindConversionOperator(eMethods, convertFrom, convertToType, implicitOnly);

            if (method != null)
            {
                return(method);
            }
            MethodInfo[] cMethods = nnConvType.GetStaticMethods();
            method = FindConversionOperator(cMethods, convertFrom, convertToType, implicitOnly);
            if (method != null)
            {
                return(method);
            }
            // try lifted conversion
            if (!TypeUtils.AreEquivalent(nnExprType, convertFrom) ||
                !TypeUtils.AreEquivalent(nnConvType, convertToType))
            {
                method = FindConversionOperator(eMethods, nnExprType, nnConvType, implicitOnly);
                if (method == null)
                {
                    method = FindConversionOperator(cMethods, nnExprType, nnConvType, implicitOnly);
                }
                if (method != null)
                {
                    return(method);
                }
            }
            return(null);
        }