本文整理汇总了C#中Type.GetNonNullableType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetNonNullableType方法的具体用法?C# Type.GetNonNullableType怎么用?C# Type.GetNonNullableType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.GetNonNullableType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: GetConvertedValue
/// <summary>
/// Converts a value to the provided targetType
/// </summary>
/// <param name="culture">Culture used for the conversion</param>
/// <param name="targetType">Destination type</param>
/// <param name="value">Value to convert</param>
/// <returns>The converted value</returns>
/// <exception cref="ArgumentException">Thrown when conversion fails.</exception>
public static object GetConvertedValue(CultureInfo culture, Type targetType, object value)
{
Debug.Assert(targetType != null, "Unexpected null targetType");
if (value == null)
{
return null;
}
Type nonNullableTargetType = targetType.GetNonNullableType();
if (value.GetType() != nonNullableTargetType)
{
try
{
// Don't parse a value that is already an enum, because that could
// implicitly convert from one enum type to another if they have
// members with the same name.
if (nonNullableTargetType.IsEnum && !value.GetType().IsEnumType())
{
return Enum.Parse(nonNullableTargetType, value.ToString(), true /*ignoreCase*/);
}
else if (value is IConvertible)
{
return System.Convert.ChangeType(value, nonNullableTargetType, culture);
}
}
catch (Exception ex)
{
if (ex.IsConversionException())
{
throw new ArgumentException(
string.Format(
CultureInfo.InvariantCulture,
CommonResources.CannotConvertValue,
value.GetType().GetTypeName(),
nonNullableTargetType.GetTypeName()),
ex);
}
else
{
throw;
}
}
}
return value;
}
示例3: Create
public static Instruction Create(Type type, bool liftedToNull)
{
// Boxed enums can be unboxed as their underlying types:
Type underlyingType = type.GetTypeInfo().IsEnum ? Enum.GetUnderlyingType(type) : type.GetNonNullableType();
if (liftedToNull)
{
switch (underlyingType.GetTypeCode())
{
case TypeCode.Boolean: return s_booleanLiftedToNull ?? (s_booleanLiftedToNull = new EqualBooleanLiftedToNull());
case TypeCode.SByte: return s_SByteLiftedToNull ?? (s_SByteLiftedToNull = new EqualSByteLiftedToNull());
case TypeCode.Byte: return s_byteLiftedToNull ?? (s_byteLiftedToNull = new EqualByteLiftedToNull());
case TypeCode.Char: return s_charLiftedToNull ?? (s_charLiftedToNull = new EqualCharLiftedToNull());
case TypeCode.Int16: return s_int16LiftedToNull ?? (s_int16LiftedToNull = new EqualInt16LiftedToNull());
case TypeCode.Int32: return s_int32LiftedToNull ?? (s_int32LiftedToNull = new EqualInt32LiftedToNull());
case TypeCode.Int64: return s_int64LiftedToNull ?? (s_int64LiftedToNull = new EqualInt64LiftedToNull());
case TypeCode.UInt16: return s_UInt16LiftedToNull ?? (s_UInt16LiftedToNull = new EqualUInt16LiftedToNull());
case TypeCode.UInt32: return s_UInt32LiftedToNull ?? (s_UInt32LiftedToNull = new EqualUInt32LiftedToNull());
case TypeCode.UInt64: return s_UInt64LiftedToNull ?? (s_UInt64LiftedToNull = new EqualUInt64LiftedToNull());
case TypeCode.Single: return s_singleLiftedToNull ?? (s_singleLiftedToNull = new EqualSingleLiftedToNull());
default:
Debug.Assert(underlyingType.GetTypeCode() == TypeCode.Double);
return s_doubleLiftedToNull ?? (s_doubleLiftedToNull = new EqualDoubleLiftedToNull());
}
}
else
{
switch (underlyingType.GetTypeCode())
{
case TypeCode.Boolean: return s_boolean ?? (s_boolean = new EqualBoolean());
case TypeCode.SByte: return s_SByte ?? (s_SByte = new EqualSByte());
case TypeCode.Byte: return s_byte ?? (s_byte = new EqualByte());
case TypeCode.Char: return s_char ?? (s_char = new EqualChar());
case TypeCode.Int16: return s_int16 ?? (s_int16 = new EqualInt16());
case TypeCode.Int32: return s_int32 ?? (s_int32 = new EqualInt32());
case TypeCode.Int64: return s_int64 ?? (s_int64 = new EqualInt64());
case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new EqualUInt16());
case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new EqualUInt32());
case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new EqualUInt64());
case TypeCode.Single: return s_single ?? (s_single = new EqualSingle());
case TypeCode.Double: return s_double ?? (s_double = new EqualDouble());
default:
// Nullable only valid if one operand is constant null, so this assert is slightly too broad.
Debug.Assert(type.IsNullableOrReferenceType());
return s_reference ?? (s_reference = new EqualReference());
}
}
}
示例4: Create
public static Instruction Create(Type type)
{
Debug.Assert(type.IsArithmetic());
switch (type.GetNonNullableType().GetTypeCode())
{
case TypeCode.Int16: return s_int16 ?? (s_int16 = new SubInt16());
case TypeCode.Int32: return s_int32 ?? (s_int32 = new SubInt32());
case TypeCode.Int64: return s_int64 ?? (s_int64 = new SubInt64());
case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new SubUInt16());
case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new SubUInt32());
case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new SubUInt64());
case TypeCode.Single: return s_single ?? (s_single = new SubSingle());
case TypeCode.Double: return s_double ?? (s_double = new SubDouble());
default:
throw ContractUtils.Unreachable;
}
}
示例5: VerifyOpTrueFalse
private static void VerifyOpTrueFalse(ExpressionType nodeType, Type left, MethodInfo opTrue, string paramName)
{
ParameterInfo[] pmsOpTrue = opTrue.GetParametersCached();
if (pmsOpTrue.Length != 1)
throw Error.IncorrectNumberOfMethodCallArguments(opTrue, paramName);
if (!ParameterIsAssignable(pmsOpTrue[0], left))
{
if (!(left.IsNullableType() && ParameterIsAssignable(pmsOpTrue[0], left.GetNonNullableType())))
throw Error.OperandTypesDoNotMatchParameters(nodeType, opTrue.Name);
}
}
示例6: GetUserDefinedBinaryOperator
private static MethodInfo GetUserDefinedBinaryOperator(ExpressionType binaryType, Type leftType, Type rightType, string name)
{
// This algorithm is wrong, we should be checking for uniqueness and erroring if
// it is defined on both types.
Type[] types = new Type[] { leftType, rightType };
Type nnLeftType = leftType.GetNonNullableType();
Type nnRightType = rightType.GetNonNullableType();
MethodInfo method = nnLeftType.GetAnyStaticMethodValidated(name, types);
if (method == null && !TypeUtils.AreEquivalent(leftType, rightType))
{
method = nnRightType.GetAnyStaticMethodValidated(name, types);
}
if (IsLiftingConditionalLogicalOperator(leftType, rightType, method, binaryType))
{
method = GetUserDefinedBinaryOperator(binaryType, nnLeftType, nnRightType, name);
}
return method;
}
示例7: ValidateCoalesceArgTypes
private static Type ValidateCoalesceArgTypes(Type left, Type right)
{
Type leftStripped = left.GetNonNullableType();
if (left.GetTypeInfo().IsValueType && !left.IsNullableType())
{
throw Error.CoalesceUsedOnNonNullType();
}
else if (left.IsNullableType() && right.IsImplicitlyConvertibleTo(leftStripped))
{
return leftStripped;
}
else if (right.IsImplicitlyConvertibleTo(left))
{
return left;
}
else if (leftStripped.IsImplicitlyConvertibleTo(right))
{
return right;
}
else
{
throw Error.ArgumentTypesMustMatch();
}
}
示例8: Create
public static Instruction Create(Type type, bool liftedToNull = false)
{
Debug.Assert(!type.GetTypeInfo().IsEnum);
if (liftedToNull)
{
switch (type.GetNonNullableType().GetTypeCode())
{
case TypeCode.SByte: return s_liftedToNullSByte ?? (s_liftedToNullSByte = new LessThanOrEqualSByte(null));
case TypeCode.Byte: return s_liftedToNullByte ?? (s_liftedToNullByte = new LessThanOrEqualByte(null));
case TypeCode.Char: return s_liftedToNullChar ?? (s_liftedToNullChar = new LessThanOrEqualChar(null));
case TypeCode.Int16: return s_liftedToNullInt16 ?? (s_liftedToNullInt16 = new LessThanOrEqualInt16(null));
case TypeCode.Int32: return s_liftedToNullInt32 ?? (s_liftedToNullInt32 = new LessThanOrEqualInt32(null));
case TypeCode.Int64: return s_liftedToNullInt64 ?? (s_liftedToNullInt64 = new LessThanOrEqualInt64(null));
case TypeCode.UInt16: return s_liftedToNullUInt16 ?? (s_liftedToNullUInt16 = new LessThanOrEqualUInt16(null));
case TypeCode.UInt32: return s_liftedToNullUInt32 ?? (s_liftedToNullUInt32 = new LessThanOrEqualUInt32(null));
case TypeCode.UInt64: return s_liftedToNullUInt64 ?? (s_liftedToNullUInt64 = new LessThanOrEqualUInt64(null));
case TypeCode.Single: return s_liftedToNullSingle ?? (s_liftedToNullSingle = new LessThanOrEqualSingle(null));
case TypeCode.Double: return s_liftedToNullDouble ?? (s_liftedToNullDouble = new LessThanOrEqualDouble(null));
default:
throw Error.ExpressionNotSupportedForType("LessThanOrEqual", type);
}
}
else
{
switch (type.GetNonNullableType().GetTypeCode())
{
case TypeCode.SByte: return s_SByte ?? (s_SByte = new LessThanOrEqualSByte(Utils.BoxedFalse));
case TypeCode.Byte: return s_byte ?? (s_byte = new LessThanOrEqualByte(Utils.BoxedFalse));
case TypeCode.Char: return s_char ?? (s_char = new LessThanOrEqualChar(Utils.BoxedFalse));
case TypeCode.Int16: return s_int16 ?? (s_int16 = new LessThanOrEqualInt16(Utils.BoxedFalse));
case TypeCode.Int32: return s_int32 ?? (s_int32 = new LessThanOrEqualInt32(Utils.BoxedFalse));
case TypeCode.Int64: return s_int64 ?? (s_int64 = new LessThanOrEqualInt64(Utils.BoxedFalse));
case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new LessThanOrEqualUInt16(Utils.BoxedFalse));
case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new LessThanOrEqualUInt32(Utils.BoxedFalse));
case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new LessThanOrEqualUInt64(Utils.BoxedFalse));
case TypeCode.Single: return s_single ?? (s_single = new LessThanOrEqualSingle(Utils.BoxedFalse));
case TypeCode.Double: return s_double ?? (s_double = new LessThanOrEqualDouble(Utils.BoxedFalse));
default:
throw Error.ExpressionNotSupportedForType("LessThanOrEqual", type);
}
}
}
示例9: 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);
}
示例10: 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);
}
//.........这里部分代码省略.........
示例11: Create
public static Instruction Create(Type type)
{
switch (type.GetNonNullableType().GetTypeCode())
{
case TypeCode.Boolean: return s_Boolean ?? (s_Boolean = new NotBoolean());
case TypeCode.Int64: return s_Int64 ?? (s_Int64 = new NotInt64());
case TypeCode.Int32: return s_Int32 ?? (s_Int32 = new NotInt32());
case TypeCode.Int16: return s_Int16 ?? (s_Int16 = new NotInt16());
case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new NotUInt64());
case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new NotUInt32());
case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new NotUInt16());
case TypeCode.Byte: return s_Byte ?? (s_Byte = new NotByte());
case TypeCode.SByte: return s_SByte ?? (s_SByte = new NotSByte());
default:
throw Error.ExpressionNotSupportedForType("Not", type);
}
}
示例12: Create
public static Instruction Create(Type type)
{
Debug.Assert(!type.GetTypeInfo().IsEnum);
switch (type.GetNonNullableType().GetTypeCode())
{
case TypeCode.Int16: return s_Int16 ?? (s_Int16 = new DivInt16());
case TypeCode.Int32: return s_Int32 ?? (s_Int32 = new DivInt32());
case TypeCode.Int64: return s_Int64 ?? (s_Int64 = new DivInt64());
case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new DivUInt16());
case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new DivUInt32());
case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new DivUInt64());
case TypeCode.Single: return s_Single ?? (s_Single = new DivSingle());
case TypeCode.Double: return s_Double ?? (s_Double = new DivDouble());
default:
throw Error.ExpressionNotSupportedForType("Div", type);
}
}
示例13: 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));
//.........这里部分代码省略.........
示例14: Create
public static Instruction Create(Type type)
{
Debug.Assert(!type.GetTypeInfo().IsEnum);
switch (type.GetNonNullableType().GetTypeCode())
{
case TypeCode.SByte: return s_SByte ?? (s_SByte = new OnesComplementSByte());
case TypeCode.Int16: return s_Int16 ?? (s_Int16 = new OnesComplementInt16());
case TypeCode.Int32: return s_Int32 ?? (s_Int32 = new OnesComplementInt32());
case TypeCode.Int64: return s_Int64 ?? (s_Int64 = new OnesComplementInt64());
case TypeCode.Byte: return s_Byte ?? (s_Byte = new OnesComplementByte());
case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new OnesComplementUInt16());
case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new OnesComplementUInt32());
case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new OnesComplementUInt64());
default:
throw Error.ExpressionNotSupportedForType("OnesComplement", type);
}
}
示例15: CreateValueExpression
private static Expression CreateValueExpression(Type targetType, object value, CultureInfo culture)
{
if (((targetType != typeof(string)) && (!targetType.IsValueType || targetType.IsNullableType())) &&
(string.Compare(value as string, "null", StringComparison.OrdinalIgnoreCase) == 0))
{
value = null;
}
if (value != null)
{
Type nonNullableTargetType = targetType.GetNonNullableType();
if (value.GetType() != nonNullableTargetType)
{
if (nonNullableTargetType.IsEnum)
{
value = Enum.Parse(nonNullableTargetType, value.ToString(), true);
}
else if (nonNullableTargetType == typeof(Guid))
{
value = new Guid(value.ToString());
}
else if (value is IConvertible)
{
value = Convert.ChangeType(value, nonNullableTargetType, culture);
}
}
}
return CreateConstantExpression(value);
}