当前位置: 首页>>代码示例>>C#>>正文


C# CastExpression.ReplaceWith方法代码示例

本文整理汇总了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);
                }
            }
        }
开发者ID:TreeSeed,项目名称:Tychaia,代码行数:40,代码来源:RemoveRedundantPrimitiveCastsVisitor.cs

示例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);
                }
            }
        }
开发者ID:evanw,项目名称:minisharp,代码行数:38,代码来源:Lower.cs


注:本文中的CastExpression.ReplaceWith方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。