當前位置: 首頁>>代碼示例>>C#>>正文


C# Type.GetNonNullableType方法代碼示例

本文整理匯總了C#中System.Type.GetNonNullableType方法的典型用法代碼示例。如果您正苦於以下問題:C# Type.GetNonNullableType方法的具體用法?C# Type.GetNonNullableType怎麽用?C# Type.GetNonNullableType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Type的用法示例。


在下文中一共展示了Type.GetNonNullableType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: GetExplicitConversion

 /// <summary>
 /// 返回從源類型到目標類型的用戶定義的顯式轉換方法。
 /// 該轉換方法的參數與源類型和目標類型並不一定完全相同,但保證存在標準顯式轉換。
 /// </summary>
 /// <param name="sourceType">要獲取用戶定義的轉換方法的源類型。</param>
 /// <param name="targetType">要獲取用戶定義的轉換方法的目標類型。</param>
 /// <returns>如果存在從源類型到目標類型的用戶定義的顯式轉換方法,則返回該方法;
 /// 否則返回 <c>null</c>。</returns>
 public static ConversionMethod GetExplicitConversion(Type sourceType, Type targetType)
 {
     Type exactSource = null, exactTarget = null;
     UniqueValue<ConversionMethod> method = new UniqueValue<ConversionMethod>();
     Conversion conv = GetTypeConversions(sourceType.GetNonNullableType());
     for (int i = conv.ConvertToIndex; i < conv.Methods.Length; i++)
     {
         ConversionMethod m = conv.Methods[i];
         if (targetType.IsStandardExplicitFrom(m.TargetType))
         {
             GetBestConversion(m, ref exactSource, ref exactTarget, method);
         }
     }
     conv = GetTypeConversions(targetType.GetNonNullableType());
     for (int i = Conversion.ConvertFromIndex; i < conv.ConvertToIndex; i++)
     {
         ConversionMethod m = conv.Methods[i];
         if (m.SourceType.IsStandardExplicitFrom(sourceType))
         {
             GetBestConversion(m, ref exactSource, ref exactTarget, method);
         }
     }
     if (method.IsUnique)
     {
         return method.Value;
     }
     return null;
 }
開發者ID:qhhndaye888,項目名稱:Cyjb,代碼行數:36,代碼來源:ConversionCache.cs

示例2: ReadValue

        object ReadValue(XmlReader reader, Type type)
        {
            var notNullableType = type.GetNonNullableType();
            if (notNullableType == typeof (DateTime)
                || notNullableType == typeof (DateTimeOffset))
            {
                reader.Read();
                DateTimeOffset value;
                if (DateTimeOffset.TryParseExact(
                    reader.Value,
                    Settings.DateTimeFormatString, Settings.Culture, DateTimeStyles.None,
                    out value))
                {
                    return value;
                }

                if (notNullableType != type) return type.GetDefault();

                throw new DeserializationException(reader.Value, type);
            }

            var typeCode = Type.GetTypeCode(type);
            if (typeCode == TypeCode.Object)
            {
                return Read(reader, type);
            }

            reader.Read();
            return Convert.ChangeType(reader.Value, type);
        }
開發者ID:MrAntix,項目名稱:Serializing,代碼行數:30,代碼來源:POXSerializer.deserialize.cs

示例3: AnalyzeTypeIs

        /// <summary>
        /// If the result of an isinst opcode is known statically, this
        /// returns the result, otherwise it returns null, meaning we'll need
        /// to perform the IsInst instruction at runtime.
        /// 
        /// The result of this function must be equivalent to IsInst, or
        /// null.
        /// </summary>
        private static AnalyzeTypeIsResult AnalyzeTypeIs(Expression operand, Type testType)
        {
            Type operandType = operand.Type;

            // An expression is either of type void, or it isn't.
            if (operandType == typeof(void))
            {
                return testType == typeof(void) ? AnalyzeTypeIsResult.KnownTrue : AnalyzeTypeIsResult.KnownFalse;
            }

            if (testType == typeof(void))
            {
                return AnalyzeTypeIsResult.KnownFalse;
            }

            //
            // Type comparisons treat nullable types as if they were the
            // underlying type. The reason is when you box a nullable it
            // becomes a boxed value of the underlying type, or null.
            //
            Type nnOperandType = operandType.GetNonNullableType();
            Type nnTestType = testType.GetNonNullableType();

            //
            // See if we can determine the answer based on the static types
            //
            // Extensive testing showed that Type.IsAssignableFrom,
            // Type.IsInstanceOfType, and the isinst instruction were all
            // equivalent when used against a live object
            //
            if (nnTestType.IsAssignableFrom(nnOperandType))
            {
                // If the operand is a value type (other than nullable), we
                // know the result is always true.
                if (operandType.GetTypeInfo().IsValueType && !operandType.IsNullableType())
                {
                    return AnalyzeTypeIsResult.KnownTrue;
                }

                // For reference/nullable types, we need to compare to null at runtime
                return AnalyzeTypeIsResult.KnownAssignable;
            }

            // We used to have an if IsSealed, return KnownFalse check here.
            // but that doesn't handle generic types & co/contravariance correctly.
            // So just use IsInst, which we know always gives us the right answer.

            // Otherwise we need a full runtime check
            return AnalyzeTypeIsResult.Unknown;
        }
開發者ID:Gscienty,項目名稱:corefx,代碼行數:58,代碼來源:ConstantCheck.cs

示例4: AnalyzeTypeIs

 private static AnalyzeTypeIsResult AnalyzeTypeIs(Expression operand, Type testType)
 {
     Type type = operand.Type;
     if (type == typeof(void))
     {
         return AnalyzeTypeIsResult.KnownFalse;
     }
     Type nonNullableType = type.GetNonNullableType();
     if (!testType.GetNonNullableType().IsAssignableFrom(nonNullableType))
     {
         return AnalyzeTypeIsResult.Unknown;
     }
     if (type.IsValueType && !type.IsNullableType())
     {
         return AnalyzeTypeIsResult.KnownTrue;
     }
     return AnalyzeTypeIsResult.KnownAssignable;
 }
開發者ID:pritesh-mandowara-sp,項目名稱:DecompliedDotNetLibraries,代碼行數:18,代碼來源:ConstantCheck.cs

示例5: IsArithmetic

 internal static bool IsArithmetic(Type type)
 {
     type = type.GetNonNullableType();
     if (!type.IsEnum)
     {
         switch (Type.GetTypeCode(type))
         {
             case TypeCode.Int16:
             case TypeCode.UInt16:
             case TypeCode.Int32:
             case TypeCode.UInt32:
             case TypeCode.Int64:
             case TypeCode.UInt64:
             case TypeCode.Single:
             case TypeCode.Double:
                 return true;
         }
     }
     return false;
 }
開發者ID:nickchal,項目名稱:pash,代碼行數:20,代碼來源:TypeUtils.cs

示例6: IsSupportedConversion

        /// <summary>
        /// Determine if the <paramref name="source"/> type can be converted
        /// to the <paramref name="target"/> type.
        /// </summary>
        /// <param name="source">The source type.</param>
        /// <param name="target">The desired target type.</param>
        /// <returns>Whether or not the source <see cref="Type"/> can be
        /// converted to the target <see cref="Type"/>.</returns>
        private static bool IsSupportedConversion(Type source, Type target)
        {
            if (source == target)
            {
                return true;
            }

            if (!target.IsValueType)
            {
                return target.IsAssignableFrom(source);
            }

            Type coreSource = source.GetNonNullableType();
            Type coreTarget = target.GetNonNullableType();

            // If the source is nullable, but the target isn't,
            // then we cannot convert.
            if (coreSource != source && coreTarget == target)
            {
                return false;
            }

            // Enums will be compared using the underlying type
            if (coreSource.IsEnum)
            {
                coreSource = Enum.GetUnderlyingType(coreSource);
            }

            if (coreTarget.IsEnum)
            {
                coreTarget = Enum.GetUnderlyingType(coreTarget);
            }

            return (coreSource == coreTarget) || (supportedConversions.ContainsKey(coreSource) && supportedConversions[coreSource].Contains(coreTarget));
        }
開發者ID:OpenRIAServices,項目名稱:OpenRiaServices,代碼行數:43,代碼來源:LinqHelper.cs

示例7: AnalyzeTypeIs

        /// <summary>
        /// If the result of an isinst opcode is known statically, this
        /// returns the result, otherwise it returns null, meaning we'll need
        /// to perform the IsInst instruction at runtime.
        /// 
        /// The result of this function must be equivalent to IsInst, or
        /// null.
        /// </summary>
        private static AnalyzeTypeIsResult AnalyzeTypeIs(Expression operand, Type testType) {
            Type operandType = operand.Type;

            // Oddly, we allow void operands
            // This is LinqV1 behavior of TypeIs
            if (operandType == typeof(void)) {
                return AnalyzeTypeIsResult.KnownFalse;
            }

            //
            // Type comparisons treat nullable types as if they were the
            // underlying type. The reason is when you box a nullable it
            // becomes a boxed value of the underlying type, or null.
            //
            Type nnOperandType = operandType.GetNonNullableType();
            Type nnTestType = testType.GetNonNullableType();

            //
            // See if we can determine the answer based on the static types
            //
            // Extensive testing showed that Type.IsAssignableFrom,
            // Type.IsInstanceOfType, and the isinst instruction were all
            // equivalent when used against a live object
            //
            if (nnTestType.IsAssignableFrom(nnOperandType)) {
                // If the operand is a value type (other than nullable), we
                // know the result is always true.
                if (operandType.IsValueType && !operandType.IsNullableType()) {
                    return AnalyzeTypeIsResult.KnownTrue;
                }

                // For reference/nullable types, we need to compare to null at runtime
                return AnalyzeTypeIsResult.KnownAssignable;
            }

            //
            // If we couldn't statically assign and the type is sealed, no
            // value at runtime can make isinst succeed
            //
            if (nnOperandType.IsSealed) {
                return AnalyzeTypeIsResult.KnownFalse;
            }

            // Otherwise we need a full runtime check
            return AnalyzeTypeIsResult.Unknown;
        }
開發者ID:calumjiao,項目名稱:Mono-Class-Libraries,代碼行數:54,代碼來源:ConstantCheck.cs

示例8: CanEdit

        public static bool CanEdit(Type type)
        {
            Debug.Assert(type != null);

            type = type.GetNonNullableType();

            return
                type.IsEnum
                || type == typeof(System.String)
                || type == typeof(System.Char)
                || type == typeof(System.DateTime)
                || type == typeof(System.Boolean)
                || type == typeof(System.Byte)
                || type == typeof(System.SByte)
                || type == typeof(System.Single)
                || type == typeof(System.Double)
                || type == typeof(System.Decimal)
                || type == typeof(System.Int16)
                || type == typeof(System.Int32)
                || type == typeof(System.Int64)
                || type == typeof(System.UInt16)
                || type == typeof(System.UInt32)
                || type == typeof(System.UInt64);
        }
開發者ID:expanz,項目名稱:expanz-Microsoft-XAML-SDKs,代碼行數:24,代碼來源:DataGridDataConnection.cs

示例9: InitializeAutoColumn

 private static void InitializeAutoColumn(Column c, Type type)
 {
     c.DataType = type;
     type = type.GetNonNullableType();
     if (type == typeof (string))
     {
         c.Width = new GridLength(180);
         return;
     }
     if (type.IsNumericIntegral())
     {
         c.Width = new GridLength(80);
         c.Format = "n0";
         return;
     }
     if (type.IsNumericNonIntegral())
     {
         c.Width = new GridLength(80);
         c.Format = "n2";
         return;
     }
     if (type == typeof (bool))
     {
         c.Width = new GridLength(60);
         return;
     }
     if (type == typeof (DateTime))
     {
         c.Format = "d";
     }
 }
開發者ID:mdjabirov,項目名稱:C1Decompiled,代碼行數:31,代碼來源:C1FlexGrid.cs

示例10: GetUserDefinedConversion

 /// <summary>
 /// 返回的將對象從 <paramref name="inputType"/> 類型轉換為 <paramref name="outputType"/> 
 /// 類型的用戶自定義類型轉換。
 /// </summary>
 /// <param name="inputType">要轉換的對象的類型。</param>
 /// <param name="outputType">要將輸入對象轉換到的類型。</param>
 /// <returns>將對象從 <paramref name="inputType"/> 類型轉換為 <paramref name="outputType"/> 
 /// 類型的用戶自定義類型轉換,如果不存在則為 <c>null</c>。</returns>
 private static Conversion GetUserDefinedConversion(Type inputType, Type outputType)
 {
     Contract.Requires(inputType != null && outputType != null &&
         inputType != typeof(void) && outputType != typeof(void));
     // 判斷可空類型。
     Type inputUnderlyingType = inputType.GetNonNullableType();
     Type outputUnderlyingType = outputType.GetNonNullableType();
     MethodInfo method = UserConversionCache.GetConversion(inputUnderlyingType, outputUnderlyingType);
     if (method == null)
     {
         return null;
     }
     Conversion conversion = new UserConversion(method);
     // 存入緩存。
     Type methodInputType = method.GetParametersNoCopy()[0].ParameterType;
     Tuple<Type, Type> key = new Tuple<Type, Type>(inputType, outputType);
     if (inputType != methodInputType || outputType != method.ReturnType)
     {
         conversion = userDefinedConverers.GetOrAdd(new Tuple<Type, Type>(methodInputType, method.ReturnType),
             conversion);
     }
     if (inputUnderlyingType != inputType || outputUnderlyingType != outputType)
     {
         userDefinedConverers.TryAdd(new Tuple<Type, Type>(inputUnderlyingType, outputUnderlyingType), conversion);
     }
     if (inputUnderlyingType == inputType || !methodInputType.IsValueType || methodInputType.IsNullable())
     {
         return userDefinedConverers.GetOrAdd(key, conversion);
     }
     // 需要將輸入的 Nullable<T> 解包。
     if (outputUnderlyingType != outputType || !outputType.IsValueType)
     {
         // outputType 可以為 null(引用類型或 Nullable<T>)。
         return userDefinedConverers.GetOrAdd(key, BetweenNullableConversion.UserDefined);
     }
     return userDefinedConverers.GetOrAdd(key, FromNullableConversion.UserDefined);
 }
開發者ID:GISwilson,項目名稱:Cyjb,代碼行數:45,代碼來源:ConversionFactory.cs

示例11: TypesArePrimitiveAndConvertible

        private bool TypesArePrimitiveAndConvertible(Type source, Type dest) {
            Type nonNullableSourceType = source.GetNonNullableType();
            Type nonNullableDestinationType = dest.GetNonNullableType();

            return (!(source.IsEnum || dest.IsEnum)) && (source.IsEquivalentTo(dest) || ((source.IsNullableType() && dest.IsEquivalentTo(nonNullableSourceType)) || ((dest.IsNullableType() && source.IsEquivalentTo(nonNullableDestinationType)) ||
                   ((source.IsNumeric() && dest.IsNumeric()) && (nonNullableDestinationType != TypeSystem.Boolean)))));
        }
開發者ID:sagifogel,項目名稱:NJection.LambdaConverter,代碼行數:7,代碼來源:Binary.cs

示例12: AlignRight

 private static bool AlignRight(Row r, Column c, Type t)
 {
     if (r is GroupRow && c.Grid != null && c.Index <= c.Grid.Columns.FirstVisibleIndex)
     {
         return false;
     }
     if (t.GetNonNullableType().IsNumeric())
     {
         ColumnValueConverter converter = c.ValueConverter as ColumnValueConverter;
         if (converter == null || converter.Values == null)
         {
             return true;
         }
     }
     return false;
 }
開發者ID:mdjabirov,項目名稱:C1Decompiled,代碼行數:16,代碼來源:CellFactory.cs

示例13: HasBuiltInEqualityOperator

 internal static bool HasBuiltInEqualityOperator(Type left, Type right)
 {
     if (!left.IsInterface || right.IsValueType)
     {
         if (right.IsInterface && !left.IsValueType)
         {
             return true;
         }
         if ((!left.IsValueType && !right.IsValueType) && (AreReferenceAssignable(left, right) || AreReferenceAssignable(right, left)))
         {
             return true;
         }
         if (!AreEquivalent(left, right))
         {
             return false;
         }
         Type nonNullableType = left.GetNonNullableType();
         if ((!(nonNullableType == typeof(bool)) && !IsNumeric(nonNullableType)) && !nonNullableType.IsEnum)
         {
             return false;
         }
     }
     return true;
 }
開發者ID:pritesh-mandowara-sp,項目名稱:DecompliedDotNetLibraries,代碼行數:24,代碼來源:TypeUtils.cs

示例14: IsFloatingPoint

 internal static bool IsFloatingPoint(Type type)
 {
     type = type.GetNonNullableType();
     switch (Type.GetTypeCode(type))
     {
         case TypeCode.Single:
         case TypeCode.Double:
             return true;
     }
     return false;
 }
開發者ID:pritesh-mandowara-sp,項目名稱:DecompliedDotNetLibraries,代碼行數:11,代碼來源:TypeUtils.cs

示例15: IsImplicitNullableConversion

 private static bool IsImplicitNullableConversion(Type source, Type destination)
 {
     return (destination.IsNullableType() && IsImplicitlyConvertible(source.GetNonNullableType(), destination.GetNonNullableType()));
 }
開發者ID:pritesh-mandowara-sp,項目名稱:DecompliedDotNetLibraries,代碼行數:4,代碼來源:TypeUtils.cs


注:本文中的System.Type.GetNonNullableType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。