本文整理汇总了C#中Type.IsNullableType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsNullableType方法的具体用法?C# Type.IsNullableType怎么用?C# Type.IsNullableType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.IsNullableType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertBack
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType != null && targetType.IsNullableType())
{
String strValue = value as String;
if (strValue == String.Empty)
{
return null;
}
}
return value;
}
示例2: DataRowFieldAccessExpressionBuilder
public DataRowFieldAccessExpressionBuilder(Type memberType, string memberName)
: base(typeof(DataRow), memberName)
{
//Handle value types for null and DBNull.Value support converting them to Nullable<>
if (memberType.IsValueType && !memberType.IsNullableType())
{
this.columnDataType = typeof(Nullable<>).MakeGenericType(memberType);
}
else
{
this.columnDataType = memberType;
}
}
示例3: GetPropertyType
private Type GetPropertyType(Type memberType)
{
var descriptorProviderPropertyType = this.GetPropertyTypeFromTypeDescriptorProvider();
if (descriptorProviderPropertyType != null)
{
memberType = descriptorProviderPropertyType;
}
//Handle value types for null and DBNull.Value support converting them to Nullable<>
if (memberType.IsValueType && !memberType.IsNullableType())
{
return typeof(Nullable<>).MakeGenericType(memberType);
}
return memberType;
}
开发者ID:vialpando09,项目名称:RallyPortal2,代码行数:16,代码来源:CustomTypeDescriptorPropertyAccessExpressionBuilder.cs
示例4: TypeIsPrimitive
/// <summary>
/// Returns whether or not the type is a primitive type.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>Whether or not the type is a primitive type.</returns>
internal static bool TypeIsPrimitive(Type type)
{
// If the type is a nullable type, get the base type.
if (type.IsNullableType())
{
type = type.GetGenericArguments()[0];
}
return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal);
}
示例5: EmitUnaryOperator
private void EmitUnaryOperator(ExpressionType op, Type operandType, Type resultType)
{
bool operandIsNullable = operandType.IsNullableType();
if (op == ExpressionType.ArrayLength)
{
_ilg.Emit(OpCodes.Ldlen);
return;
}
if (operandIsNullable)
{
switch (op)
{
case ExpressionType.Not:
{
if (operandType != typeof(bool?))
{
goto case ExpressionType.Negate;
}
Label labEnd = _ilg.DefineLabel();
LocalBuilder loc = GetLocal(operandType);
// store values (reverse order since they are already on the stack)
_ilg.Emit(OpCodes.Stloc, loc);
// test for null
_ilg.Emit(OpCodes.Ldloca, loc);
_ilg.EmitHasValue(operandType);
_ilg.Emit(OpCodes.Brfalse_S, labEnd);
// do op on non-null value
_ilg.Emit(OpCodes.Ldloca, loc);
_ilg.EmitGetValueOrDefault(operandType);
Type nnOperandType = operandType.GetNonNullableType();
EmitUnaryOperator(op, nnOperandType, typeof(bool));
// construct result
ConstructorInfo ci = resultType.GetConstructor(ArrayOfType_Bool);
_ilg.Emit(OpCodes.Newobj, ci);
_ilg.Emit(OpCodes.Stloc, loc);
_ilg.MarkLabel(labEnd);
_ilg.Emit(OpCodes.Ldloc, loc);
FreeLocal(loc);
return;
}
case ExpressionType.UnaryPlus:
case ExpressionType.NegateChecked:
case ExpressionType.Negate:
case ExpressionType.Increment:
case ExpressionType.Decrement:
case ExpressionType.OnesComplement:
case ExpressionType.IsFalse:
case ExpressionType.IsTrue:
{
Debug.Assert(TypeUtils.AreEquivalent(operandType, resultType));
Label labIfNull = _ilg.DefineLabel();
Label labEnd = _ilg.DefineLabel();
LocalBuilder loc = GetLocal(operandType);
// check for null
_ilg.Emit(OpCodes.Stloc, loc);
_ilg.Emit(OpCodes.Ldloca, loc);
_ilg.EmitHasValue(operandType);
_ilg.Emit(OpCodes.Brfalse_S, labIfNull);
// apply operator to non-null value
_ilg.Emit(OpCodes.Ldloca, loc);
_ilg.EmitGetValueOrDefault(operandType);
Type nnOperandType = resultType.GetNonNullableType();
EmitUnaryOperator(op, nnOperandType, nnOperandType);
// construct result
ConstructorInfo ci = resultType.GetConstructor(new Type[] { nnOperandType });
_ilg.Emit(OpCodes.Newobj, ci);
_ilg.Emit(OpCodes.Stloc, loc);
_ilg.Emit(OpCodes.Br_S, labEnd);
// if null then create a default one
_ilg.MarkLabel(labIfNull);
_ilg.Emit(OpCodes.Ldloca, loc);
_ilg.Emit(OpCodes.Initobj, resultType);
_ilg.MarkLabel(labEnd);
_ilg.Emit(OpCodes.Ldloc, loc);
FreeLocal(loc);
return;
}
case ExpressionType.TypeAs:
_ilg.Emit(OpCodes.Box, operandType);
_ilg.Emit(OpCodes.Isinst, resultType);
if (resultType.IsNullableType())
{
_ilg.Emit(OpCodes.Unbox_Any, resultType);
}
return;
default:
throw Error.UnhandledUnary(op);
}
//.........这里部分代码省略.........
示例6: TypeAs
/// <summary>Creates a <see cref="UnaryExpression"/> that represents an explicit reference or boxing conversion where null is supplied if the conversion fails.</summary>
/// <returns>A <see cref="UnaryExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="ExpressionType.TypeAs"/> and the <see cref="UnaryExpression.Operand"/> and <see cref="Expression.Type"/> properties set to the specified values.</returns>
/// <param name="expression">An <see cref="Expression"/> to set the <see cref="UnaryExpression.Operand"/> property equal to.</param>
/// <param name="type">A <see cref="System.Type"/> to set the <see cref="Type"/> property equal to.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="expression"/> or <paramref name="type"/> is null.</exception>
public static UnaryExpression TypeAs(Expression expression, Type type)
{
RequiresCanRead(expression, nameof(expression));
ContractUtils.RequiresNotNull(type, nameof(type));
TypeUtils.ValidateType(type, nameof(type));
if (type.GetTypeInfo().IsValueType && !type.IsNullableType())
{
throw Error.IncorrectTypeForTypeAs(type, nameof(type));
}
return new UnaryExpression(ExpressionType.TypeAs, expression, type, null);
}
示例7: EmitConvertArithmeticResult
// Binary/unary operations on 8 and 16 bit operand types will leave a
// 32-bit value on the stack, because that's how IL works. For these
// cases, we need to cast it back to the resultType, possibly using a
// checked conversion if the original operator was convert
private void EmitConvertArithmeticResult(ExpressionType op, Type resultType) {
Debug.Assert(!resultType.IsNullableType());
switch (Type.GetTypeCode(resultType)) {
case TypeCode.Byte:
_ilg.Emit(IsChecked(op) ? OpCodes.Conv_Ovf_U1 : OpCodes.Conv_U1);
break;
case TypeCode.SByte:
_ilg.Emit(IsChecked(op) ? OpCodes.Conv_Ovf_I1 : OpCodes.Conv_I1);
break;
case TypeCode.UInt16:
_ilg.Emit(IsChecked(op) ? OpCodes.Conv_Ovf_U2 : OpCodes.Conv_U2);
break;
case TypeCode.Int16:
_ilg.Emit(IsChecked(op) ? OpCodes.Conv_Ovf_I2 : OpCodes.Conv_I2);
break;
}
}
示例8: IsValidLiftedConditionalLogicalOperator
private static bool IsValidLiftedConditionalLogicalOperator(Type left, Type right, ParameterInfo[] pms)
{
return TypeUtils.AreEquivalent(left, right) &&
right.IsNullableType() &&
TypeUtils.AreEquivalent(pms[1].ParameterType, right.GetNonNullableType());
}
示例9: IsSimpleShift
private static bool IsSimpleShift(Type left, Type right) {
return TypeUtils.IsInteger(left)
&& TypeUtils.GetNonNullableType(right) == typeof(int)
&& (left.IsNullableType() == right.IsNullableType());
}
示例10: IsLiftingConditionalLogicalOperator
private static bool IsLiftingConditionalLogicalOperator(Type left, Type right, MethodInfo method, ExpressionType binaryType)
{
return right.IsNullableType() &&
left.IsNullableType() &&
method == null &&
(binaryType == ExpressionType.AndAlso || binaryType == ExpressionType.OrElse);
}
示例11: EmitLift
private void EmitLift(ExpressionType nodeType, Type resultType, MethodCallExpression mc, ParameterExpression[] paramList, Expression[] argList)
{
Debug.Assert(TypeUtils.AreEquivalent(resultType.GetNonNullableType(), mc.Type.GetNonNullableType()));
switch (nodeType)
{
default:
case ExpressionType.LessThan:
case ExpressionType.LessThanOrEqual:
case ExpressionType.GreaterThan:
case ExpressionType.GreaterThanOrEqual:
{
Label exit = _ilg.DefineLabel();
Label exitNull = _ilg.DefineLabel();
LocalBuilder anyNull = _ilg.DeclareLocal(typeof(bool));
for (int i = 0, n = paramList.Length; i < n; i++)
{
ParameterExpression v = paramList[i];
Expression arg = argList[i];
if (arg.Type.IsNullableType())
{
_scope.AddLocal(this, v);
EmitAddress(arg, arg.Type);
_ilg.Emit(OpCodes.Dup);
_ilg.EmitHasValue(arg.Type);
_ilg.Emit(OpCodes.Ldc_I4_0);
_ilg.Emit(OpCodes.Ceq);
_ilg.Emit(OpCodes.Stloc, anyNull);
_ilg.EmitGetValueOrDefault(arg.Type);
_scope.EmitSet(v);
}
else
{
_scope.AddLocal(this, v);
EmitExpression(arg);
if (!arg.Type.GetTypeInfo().IsValueType)
{
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Ldnull);
_ilg.Emit(OpCodes.Ceq);
_ilg.Emit(OpCodes.Stloc, anyNull);
}
_scope.EmitSet(v);
}
_ilg.Emit(OpCodes.Ldloc, anyNull);
_ilg.Emit(OpCodes.Brtrue, exitNull);
}
EmitMethodCallExpression(mc);
if (resultType.IsNullableType() && !TypeUtils.AreEquivalent(resultType, mc.Type))
{
ConstructorInfo ci = resultType.GetConstructor(new Type[] { mc.Type });
_ilg.Emit(OpCodes.Newobj, ci);
}
_ilg.Emit(OpCodes.Br_S, exit);
_ilg.MarkLabel(exitNull);
if (TypeUtils.AreEquivalent(resultType, mc.Type.GetNullableType()))
{
if (resultType.GetTypeInfo().IsValueType)
{
LocalBuilder result = GetLocal(resultType);
_ilg.Emit(OpCodes.Ldloca, result);
_ilg.Emit(OpCodes.Initobj, resultType);
_ilg.Emit(OpCodes.Ldloc, result);
FreeLocal(result);
}
else
{
_ilg.Emit(OpCodes.Ldnull);
}
}
else
{
switch (nodeType)
{
case ExpressionType.LessThan:
case ExpressionType.LessThanOrEqual:
case ExpressionType.GreaterThan:
case ExpressionType.GreaterThanOrEqual:
_ilg.Emit(OpCodes.Ldc_I4_0);
break;
default:
throw Error.UnknownLiftType(nodeType);
}
}
_ilg.MarkLabel(exit);
return;
}
case ExpressionType.Equal:
case ExpressionType.NotEqual:
{
if (TypeUtils.AreEquivalent(resultType, mc.Type.GetNullableType()))
{
goto default;
}
Label exit = _ilg.DefineLabel();
Label exitAllNull = _ilg.DefineLabel();
Label exitAnyNull = _ilg.DefineLabel();
LocalBuilder anyNull = _ilg.DeclareLocal(typeof(bool));
LocalBuilder allNull = _ilg.DeclareLocal(typeof(bool));
//.........这里部分代码省略.........
示例12: EmitLiftedRelational
private void EmitLiftedRelational(ExpressionType op, Type leftType, Type rightType, Type resultType, bool liftedToNull)
{
Debug.Assert(leftType.IsNullableType());
Label shortCircuit = _ilg.DefineLabel();
LocalBuilder locLeft = GetLocal(leftType);
LocalBuilder locRight = GetLocal(rightType);
// store values (reverse order since they are already on the stack)
_ilg.Emit(OpCodes.Stloc, locRight);
_ilg.Emit(OpCodes.Stloc, locLeft);
if (op == ExpressionType.Equal)
{
// test for both null -> true
_ilg.Emit(OpCodes.Ldloca, locLeft);
_ilg.EmitHasValue(leftType);
_ilg.Emit(OpCodes.Ldc_I4_0);
_ilg.Emit(OpCodes.Ceq);
_ilg.Emit(OpCodes.Ldloca, locRight);
_ilg.EmitHasValue(rightType);
_ilg.Emit(OpCodes.Ldc_I4_0);
_ilg.Emit(OpCodes.Ceq);
_ilg.Emit(OpCodes.And);
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Brtrue_S, shortCircuit);
_ilg.Emit(OpCodes.Pop);
// test for either is null -> false
_ilg.Emit(OpCodes.Ldloca, locLeft);
_ilg.EmitHasValue(leftType);
_ilg.Emit(OpCodes.Ldloca, locRight);
_ilg.EmitHasValue(rightType);
_ilg.Emit(OpCodes.And);
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Brfalse_S, shortCircuit);
_ilg.Emit(OpCodes.Pop);
}
else if (op == ExpressionType.NotEqual)
{
// test for both null -> false
_ilg.Emit(OpCodes.Ldloca, locLeft);
_ilg.EmitHasValue(leftType);
_ilg.Emit(OpCodes.Ldloca, locRight);
_ilg.EmitHasValue(rightType);
_ilg.Emit(OpCodes.Or);
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Brfalse_S, shortCircuit);
_ilg.Emit(OpCodes.Pop);
// test for either is null -> true
_ilg.Emit(OpCodes.Ldloca, locLeft);
_ilg.EmitHasValue(leftType);
_ilg.Emit(OpCodes.Ldc_I4_0);
_ilg.Emit(OpCodes.Ceq);
_ilg.Emit(OpCodes.Ldloca, locRight);
_ilg.EmitHasValue(rightType);
_ilg.Emit(OpCodes.Ldc_I4_0);
_ilg.Emit(OpCodes.Ceq);
_ilg.Emit(OpCodes.Or);
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Brtrue_S, shortCircuit);
_ilg.Emit(OpCodes.Pop);
}
else
{
// test for either is null -> false
_ilg.Emit(OpCodes.Ldloca, locLeft);
_ilg.EmitHasValue(leftType);
_ilg.Emit(OpCodes.Ldloca, locRight);
_ilg.EmitHasValue(rightType);
_ilg.Emit(OpCodes.And);
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Brfalse_S, shortCircuit);
_ilg.Emit(OpCodes.Pop);
}
// do op on values
_ilg.Emit(OpCodes.Ldloca, locLeft);
_ilg.EmitGetValueOrDefault(leftType);
_ilg.Emit(OpCodes.Ldloca, locRight);
_ilg.EmitGetValueOrDefault(rightType);
//RELEASING locLeft locRight
FreeLocal(locLeft);
FreeLocal(locRight);
EmitBinaryOperator(
op,
leftType.GetNonNullableType(),
rightType.GetNonNullableType(),
resultType.GetNonNullableType(),
liftedToNull: false
);
if (!liftedToNull)
{
_ilg.MarkLabel(shortCircuit);
}
//.........这里部分代码省略.........
示例13: EmitLiftedBinaryArithmetic
private void EmitLiftedBinaryArithmetic(ExpressionType op, Type leftType, Type rightType, Type resultType)
{
bool leftIsNullable = leftType.IsNullableType();
bool rightIsNullable = rightType.IsNullableType();
Debug.Assert(leftIsNullable || rightIsNullable);
Label labIfNull = _ilg.DefineLabel();
Label labEnd = _ilg.DefineLabel();
LocalBuilder locLeft = GetLocal(leftType);
LocalBuilder locRight = GetLocal(rightType);
LocalBuilder locResult = GetLocal(resultType);
// store values (reverse order since they are already on the stack)
_ilg.Emit(OpCodes.Stloc, locRight);
_ilg.Emit(OpCodes.Stloc, locLeft);
// test for null
// use short circuiting
if (leftIsNullable)
{
_ilg.Emit(OpCodes.Ldloca, locLeft);
_ilg.EmitHasValue(leftType);
_ilg.Emit(OpCodes.Brfalse_S, labIfNull);
}
if (rightIsNullable)
{
_ilg.Emit(OpCodes.Ldloca, locRight);
_ilg.EmitHasValue(rightType);
_ilg.Emit(OpCodes.Brfalse_S, labIfNull);
}
// do op on values
if (leftIsNullable)
{
_ilg.Emit(OpCodes.Ldloca, locLeft);
_ilg.EmitGetValueOrDefault(leftType);
}
else
{
_ilg.Emit(OpCodes.Ldloc, locLeft);
}
if (rightIsNullable)
{
_ilg.Emit(OpCodes.Ldloca, locRight);
_ilg.EmitGetValueOrDefault(rightType);
}
else
{
_ilg.Emit(OpCodes.Ldloc, locRight);
}
//RELEASING locLeft locRight
FreeLocal(locLeft);
FreeLocal(locRight);
EmitBinaryOperator(op, leftType.GetNonNullableType(), rightType.GetNonNullableType(), resultType.GetNonNullableType(), liftedToNull: false);
// construct result type
ConstructorInfo ci = resultType.GetConstructor(new Type[] { resultType.GetNonNullableType() });
_ilg.Emit(OpCodes.Newobj, ci);
_ilg.Emit(OpCodes.Stloc, locResult);
_ilg.Emit(OpCodes.Br_S, labEnd);
// if null then create a default one
_ilg.MarkLabel(labIfNull);
_ilg.Emit(OpCodes.Ldloca, locResult);
_ilg.Emit(OpCodes.Initobj, resultType);
_ilg.MarkLabel(labEnd);
_ilg.Emit(OpCodes.Ldloc, locResult);
//RELEASING locResult
FreeLocal(locResult);
}
示例14: EmitLiftedBinaryOp
private void EmitLiftedBinaryOp(ExpressionType op, Type leftType, Type rightType, Type resultType, bool liftedToNull)
{
Debug.Assert(leftType.IsNullableType() || rightType.IsNullableType());
switch (op)
{
case ExpressionType.And:
if (leftType == typeof(bool?))
{
EmitLiftedBooleanAnd();
}
else
{
EmitLiftedBinaryArithmetic(op, leftType, rightType, resultType);
}
break;
case ExpressionType.Or:
if (leftType == typeof(bool?))
{
EmitLiftedBooleanOr();
}
else
{
EmitLiftedBinaryArithmetic(op, leftType, rightType, resultType);
}
break;
case ExpressionType.ExclusiveOr:
case ExpressionType.Add:
case ExpressionType.AddChecked:
case ExpressionType.Subtract:
case ExpressionType.SubtractChecked:
case ExpressionType.Multiply:
case ExpressionType.MultiplyChecked:
case ExpressionType.Divide:
case ExpressionType.Modulo:
case ExpressionType.LeftShift:
case ExpressionType.RightShift:
EmitLiftedBinaryArithmetic(op, leftType, rightType, resultType);
break;
case ExpressionType.LessThan:
case ExpressionType.LessThanOrEqual:
case ExpressionType.GreaterThan:
case ExpressionType.GreaterThanOrEqual:
case ExpressionType.Equal:
case ExpressionType.NotEqual:
EmitLiftedRelational(op, leftType, rightType, resultType, liftedToNull);
break;
case ExpressionType.AndAlso:
case ExpressionType.OrElse:
default:
throw ContractUtils.Unreachable;
}
}
示例15: EmitUnliftedBinaryOp
private void EmitUnliftedBinaryOp(ExpressionType op, Type leftType, Type rightType)
{
Debug.Assert(!leftType.IsNullableType());
Debug.Assert(!rightType.IsNullableType());
if (op == ExpressionType.Equal || op == ExpressionType.NotEqual)
{
EmitUnliftedEquality(op, leftType);
return;
}
if (!leftType.GetTypeInfo().IsPrimitive)
{
throw Error.OperatorNotImplementedForType(op, leftType);
}
switch (op)
{
case ExpressionType.Add:
_ilg.Emit(OpCodes.Add);
break;
case ExpressionType.AddChecked:
if (leftType.IsFloatingPoint())
{
_ilg.Emit(OpCodes.Add);
}
else if (leftType.IsUnsigned())
{
_ilg.Emit(OpCodes.Add_Ovf_Un);
}
else
{
_ilg.Emit(OpCodes.Add_Ovf);
}
break;
case ExpressionType.Subtract:
_ilg.Emit(OpCodes.Sub);
break;
case ExpressionType.SubtractChecked:
if (leftType.IsFloatingPoint())
{
_ilg.Emit(OpCodes.Sub);
}
else if (leftType.IsUnsigned())
{
_ilg.Emit(OpCodes.Sub_Ovf_Un);
}
else
{
_ilg.Emit(OpCodes.Sub_Ovf);
}
break;
case ExpressionType.Multiply:
_ilg.Emit(OpCodes.Mul);
break;
case ExpressionType.MultiplyChecked:
if (leftType.IsFloatingPoint())
{
_ilg.Emit(OpCodes.Mul);
}
else if (leftType.IsUnsigned())
{
_ilg.Emit(OpCodes.Mul_Ovf_Un);
}
else
{
_ilg.Emit(OpCodes.Mul_Ovf);
}
break;
case ExpressionType.Divide:
if (leftType.IsUnsigned())
{
_ilg.Emit(OpCodes.Div_Un);
}
else
{
_ilg.Emit(OpCodes.Div);
}
break;
case ExpressionType.Modulo:
if (leftType.IsUnsigned())
{
_ilg.Emit(OpCodes.Rem_Un);
}
else
{
_ilg.Emit(OpCodes.Rem);
}
break;
case ExpressionType.And:
case ExpressionType.AndAlso:
_ilg.Emit(OpCodes.And);
break;
case ExpressionType.Or:
case ExpressionType.OrElse:
_ilg.Emit(OpCodes.Or);
break;
case ExpressionType.LessThan:
if (leftType.IsUnsigned())
{
_ilg.Emit(OpCodes.Clt_Un);
}
//.........这里部分代码省略.........