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


C# TypeSyntax.IsEquivalentTo方法代码示例

本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax.IsEquivalentTo方法的典型用法代码示例。如果您正苦于以下问题:C# TypeSyntax.IsEquivalentTo方法的具体用法?C# TypeSyntax.IsEquivalentTo怎么用?C# TypeSyntax.IsEquivalentTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax的用法示例。


在下文中一共展示了TypeSyntax.IsEquivalentTo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MatchWhereSelect

        //		internal static readonly AstNode wherePatternCase1 =
        //			new InvocationExpression(
        //				new MemberReferenceExpression(
        //					new InvocationExpression(
        //						new MemberReferenceExpression(new AnyNode("target"), "Where"),
        //						new LambdaExpression {
        //							Parameters = { PatternHelper.NamedParameter ("param1", PatternHelper.AnyType ("paramType", true), Pattern.AnyString) },
        //							Body = PatternHelper.OptionalParentheses (new IsExpression(new AnyNode("expr1"), new AnyNode("type")))
        //						}
        //					), "Select"),
        //				new LambdaExpression {
        //					Parameters = { PatternHelper.NamedParameter ("param2", PatternHelper.AnyType ("paramType", true), Pattern.AnyString) },
        //					Body = PatternHelper.OptionalParentheses (new AsExpression(PatternHelper.OptionalParentheses (new AnyNode("expr2")), new Backreference("type")))
        //				}
        //		);
        //
        //		internal static readonly AstNode wherePatternCase2 =
        //			new InvocationExpression(
        //				new MemberReferenceExpression(
        //					new InvocationExpression(
        //						new MemberReferenceExpression(new AnyNode("target"), "Where"),
        //						new LambdaExpression {
        //							Parameters = { PatternHelper.NamedParameter ("param1", PatternHelper.AnyType ("paramType", true), Pattern.AnyString) },
        //							Body = PatternHelper.OptionalParentheses (new IsExpression(PatternHelper.OptionalParentheses (new AnyNode("expr1")), new AnyNode("type")))
        //						}
        //					), "Select"),
        //				new LambdaExpression {
        //					Parameters = { PatternHelper.NamedParameter ("param2", PatternHelper.AnyType ("paramType", true), Pattern.AnyString) },
        //					Body = PatternHelper.OptionalParentheses (new CastExpression(new Backreference("type"), PatternHelper.OptionalParentheses (new AnyNode("expr2"))))
        //				}
        //		);
        static bool MatchWhereSelect(InvocationExpressionSyntax selectInvoke, out ExpressionSyntax target, out TypeSyntax type)
        {
            target = null;
            type = null;

            if (selectInvoke.ArgumentList.Arguments.Count != 1)
                return false;
            var anyInvokeBase = selectInvoke.Expression as MemberAccessExpressionSyntax;
            if (anyInvokeBase == null || anyInvokeBase.Name.Identifier.Text != "Select")
                return false;
            var whereInvoke = anyInvokeBase.Expression as InvocationExpressionSyntax;
            if (whereInvoke == null || whereInvoke.ArgumentList.Arguments.Count != 1)
                return false;
            var baseMember = whereInvoke.Expression as MemberAccessExpressionSyntax;
            if (baseMember == null || baseMember.Name.Identifier.Text != "Where")
                return false;
            target = baseMember.Expression;

            ParameterSyntax param1, param2;
            ExpressionSyntax expr1, expr2;
            if (!ExtractLambda(whereInvoke.ArgumentList.Arguments[0], out param1, out expr1))
                return false;
            if (!ExtractLambda(selectInvoke.ArgumentList.Arguments[0], out param2, out expr2))
                return false;
            if (!expr1.IsKind(SyntaxKind.IsExpression))
                return false;
            type = (expr1 as BinaryExpressionSyntax)?.Right as TypeSyntax;
            if (type == null)
                return false;
            if (expr2.IsKind(SyntaxKind.AsExpression))
            {
                if (!CompareNames(param2, (expr2 as BinaryExpressionSyntax).Left as IdentifierNameSyntax))
                    return false;
                if (!type.IsEquivalentTo((expr2 as BinaryExpressionSyntax)?.Right))
                    return false;
            }
            else if (expr2.IsKind(SyntaxKind.CastExpression))
            {
                if (!CompareNames(param2, (expr2 as CastExpressionSyntax)?.Expression.SkipParens() as IdentifierNameSyntax))
                    return false;
                if (!type.IsEquivalentTo((expr2 as CastExpressionSyntax)?.Type))
                    return false;
            }
            else
                return false;

            if (!CompareNames(param1, (expr1 as BinaryExpressionSyntax)?.Left as IdentifierNameSyntax))
                return false;


            return target != null;
        }
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:83,代码来源:ReplaceWithOfTypeAnalyzer.cs


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