本文整理汇总了C#中CastExpression.ReplaceWith方法的典型用法代码示例。如果您正苦于以下问题:C# CastExpression.ReplaceWith方法的具体用法?C# CastExpression.ReplaceWith怎么用?C# CastExpression.ReplaceWith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CastExpression
的用法示例。
在下文中一共展示了CastExpression.ReplaceWith方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitCastExpression
public override void VisitCastExpression(CastExpression castExpression)
{
base.VisitCastExpression(castExpression);
var expression = castExpression.Expression;
if (expression is ParenthesizedExpression)
expression = (expression as ParenthesizedExpression).Expression;
object value = null;
if (expression is PrimitiveExpression)
value = (expression as PrimitiveExpression).Value;
else if (expression is UnaryOperatorExpression &&
(expression as UnaryOperatorExpression).Expression is PrimitiveExpression)
{
var primitive = (expression as UnaryOperatorExpression).Expression as PrimitiveExpression;
value = primitive.Value;
}
if (value != null)
{
var type = (castExpression.Type as PrimitiveType).KnownTypeCode;
if ((type == KnownTypeCode.Int16 && value is short) ||
(type == KnownTypeCode.Int32 && value is int) ||
(type == KnownTypeCode.Int64 && value is long) ||
(type == KnownTypeCode.UInt16 && value is ushort) ||
(type == KnownTypeCode.UInt32 && value is uint) ||
(type == KnownTypeCode.UInt64 && value is ulong) ||
(type == KnownTypeCode.Double && value is double) ||
(type == KnownTypeCode.Single && value is float) ||
(type == KnownTypeCode.String && value is string) ||
(type == KnownTypeCode.Boolean && value is bool) ||
(type == KnownTypeCode.Char && value is char) ||
(type == KnownTypeCode.Byte && value is byte) ||
(type == KnownTypeCode.SByte && value is sbyte) ||
(type == KnownTypeCode.Decimal && value is decimal))
{
castExpression.ReplaceWith(expression);
}
}
}
示例2: VisitCastExpression
public void VisitCastExpression(CastExpression node)
{
VisitChildren(node);
// Implement primitive casts
var result = resolver.Resolve(node) as ConversionResolveResult;
if (result != null) {
var expression = node.Expression;
var fromType = resolver.Resolve(expression).Type;
var isDynamic = fromType == SpecialType.Dynamic;
var toCode = TypeCode(result.Type);
expression.Remove();
// Integer cast
if (IsIntegerTypeCode(toCode) && (!IsIntegerTypeCode(TypeCode(fromType)) || isDynamic)) {
node.ReplaceWith(new BinaryOperatorExpression(expression,
BinaryOperatorType.BitwiseOr, new PrimitiveExpression(0)));
}
// Boolean cast
else if (isDynamic && toCode == KnownTypeCode.Boolean) {
node.ReplaceWith(new UnaryOperatorExpression(
UnaryOperatorType.Not, new UnaryOperatorExpression(
UnaryOperatorType.Not, expression)));
}
// Floating-point cast
else if (isDynamic && IsFloatingPointTypeCode(toCode)) {
node.ReplaceWith(new UnaryOperatorExpression(
UnaryOperatorType.Plus, expression));
}
// No cast needed
else {
node.ReplaceWith(expression);
}
}
}