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


C# ClassDeclarationSyntax.TrackNodes方法代码示例

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


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

示例1: ProcessApplyToClassDeclaration

            public override ClassDeclarationSyntax ProcessApplyToClassDeclaration(ClassDeclarationSyntax applyTo)
            {
                applyTo = base.ProcessApplyToClassDeclaration(applyTo);

                if (this.applyTo.IsRecursiveParentOrDerivative)
                {
                    // Add the lookupTable parameter to the constructor's signature.
                    var origCtor = applyTo.Members.OfType<ConstructorDeclarationSyntax>().Single();
                    var alteredCtor = origCtor.AddParameterListParameters(SyntaxFactory.Parameter(LookupTableFieldName.Identifier).WithType(Syntax.OptionalOf(this.lookupTableType)));

                    // If this type isn't itself the recursive parent then we derive from it. And we must propagate the value to the chained base type.
                    if (!this.applyTo.IsRecursiveParent)
                    {
                        Assumes.NotNull(alteredCtor.Initializer); // we expect a chained call to the base constructor.
                        alteredCtor = alteredCtor.WithInitializer(
                            alteredCtor.Initializer.AddArgumentListArguments(
                                SyntaxFactory.Argument(SyntaxFactory.NameColon(LookupTableFieldName), NoneToken, LookupTableFieldName)));
                    }

                    // Apply the updated constructor back to the generated type.
                    applyTo = applyTo.ReplaceNode(origCtor, alteredCtor);

                    // Search for invocations of the constructor that we now have to update.
                    var invocations = (
                        from n in applyTo.DescendantNodes()
                        let ctorInvocation = n as ObjectCreationExpressionSyntax
                        let instantiatedTypeName = ctorInvocation?.Type?.ToString()
                        where instantiatedTypeName == this.applyTo.TypeSyntax.ToString() || instantiatedTypeName == this.applyTo.TypeSymbol.Name
                        select ctorInvocation).ToImmutableArray();
                    var trackedTree = applyTo.TrackNodes(invocations);

                    var recursiveField = this.applyTo.RecursiveParent.RecursiveField;
                    foreach (var ctorInvocation in invocations)
                    {
                        var currentInvocation = trackedTree.GetCurrentNode(ctorInvocation);

                        ExpressionSyntax lookupTableValue = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression);
                        var containingMethod = currentInvocation.FirstAncestorOrSelf<MethodDeclarationSyntax>();
                        if (containingMethod != null)
                        {
                            if (containingMethod.ParameterList.Parameters.Any(p => p.Identifier.ToString() == recursiveField.Name))
                            {
                                // We're in a method that accepts the recursive field as a parameter.
                                // The value we want to pass in for the lookup table is:
                                // (children.IsDefined && children.Value != this.Children) ? default(Optional<ImmutableDictionary<uint, KeyValuePair<RecursiveType, uint>>>) : Optional.For(this.lookupTable);
                                lookupTableValue = SyntaxFactory.ConditionalExpression(
                                    SyntaxFactory.ParenthesizedExpression(
                                        SyntaxFactory.BinaryExpression(
                                            SyntaxKind.LogicalAndExpression,
                                            Syntax.OptionalIsDefined(recursiveField.NameAsField),
                                            SyntaxFactory.BinaryExpression(
                                                SyntaxKind.NotEqualsExpression,
                                                Syntax.OptionalValue(recursiveField.NameAsField),
                                                Syntax.ThisDot(recursiveField.NameAsProperty)))),
                                    SyntaxFactory.DefaultExpression(Syntax.OptionalOf(this.lookupTableType)),
                                    Syntax.OptionalFor(Syntax.ThisDot(LookupTableFieldName)));
                            }
                        }

                        var alteredInvocation = currentInvocation.AddArgumentListArguments(
                            SyntaxFactory.Argument(SyntaxFactory.NameColon(LookupTableFieldName), NoneToken, lookupTableValue));

                        trackedTree = trackedTree.ReplaceNode(currentInvocation, alteredInvocation);
                    }

                    applyTo = trackedTree;
                }

                return applyTo;
            }
开发者ID:xiexin36,项目名称:ImmutableObjectGraph,代码行数:70,代码来源:CodeGen+FastSpineGen.cs


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