本文整理汇总了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;
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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));
}
示例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;
}
示例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);
}
示例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";
}
}
示例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);
}
示例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)))));
}
示例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;
}
示例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;
}
示例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;
}
示例15: IsImplicitNullableConversion
private static bool IsImplicitNullableConversion(Type source, Type destination)
{
return (destination.IsNullableType() && IsImplicitlyConvertible(source.GetNonNullableType(), destination.GetNonNullableType()));
}