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


C# ClassDeclarationSyntax.GetLocation方法代码示例

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


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

示例1: VisitClassDeclaration

        /// <summary>
        /// creates a new class declaration where members are delegated to the mixin
        /// reference
        /// </summary>
        /// <param name="classDeclaration"></param>
        /// <returns></returns>
        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax classDeclaration)
        {
            var mixinName = _mixin.Name;
            // currently, only three types of member forwardings are possible,
            // there is a strategy for every forwarding implementation
            var implementationStrategies = CreateStrategies(_mixin, _semantic, _settings);
            // needed to evaluate whether type names can be reduced (depends on the using statements in the file)
            var positionOfClassInSourceFile = classDeclaration.GetLocation().SourceSpan.Start;
           
            // generate the members that should be implemented            
            var membersToAdd = _members
                .Select(x => implementationStrategies[x.GetType()].ImplementMember(x, positionOfClassInSourceFile))
                .Where(x => x != null).ToArray();

            // add regions if there is something to generate
            if (_settings.CreateRegions)
            {
                var regionCaption = $" mixin {mixinName}";
                // if there is already a region, add members to this one,
                // otherwise create a new one
                if (classDeclaration.HasRegion(regionCaption))
                    return classDeclaration.AddMembersIntoRegion(regionCaption, membersToAdd);
                else
                    membersToAdd.AddRegionAround(regionCaption);
            }

            // return a new class node with the additional members
            // problem with AddMembers is that it adds the new code
            // after the last syntax node, so when a region exists in the class
            // the code will be added before the region end, this leads to the bug
            // https://github.com/pgenfer/mixinSharp/issues/9
            // where the newly created region is nested into the old one.
            // a solution is to ensure that the members are added after any endregion directive

            // check if there is an end region in the file
            var lastEndRegion = classDeclaration.GetLastElementInClass<EndRegionDirectiveTriviaSyntax>();
            // only interesting if there is an end region directive at all
            if(lastEndRegion != null)
            {
                var lastSyntaxNode = classDeclaration.GetLastElementInClass<SyntaxNode>(false);
                if (lastSyntaxNode != lastEndRegion && lastSyntaxNode.SpanStart < lastEndRegion.Span.End)
                {
                    // special case here: there is an end region directive at the end of the class
                    // so we must add our members AFTER this endregion (by removing it and adding it before the first member)
                    if (membersToAdd.Length > 0)
                    {
                        var newClassDeclaration = classDeclaration.RemoveNode(lastEndRegion, SyntaxRemoveOptions.AddElasticMarker);
                        membersToAdd[0] = membersToAdd[0].WithLeadingTrivia(
                            new SyntaxTriviaList()
                            .Add(Trivia(EndRegionDirectiveTrivia(true)))
                            .Add(EndOfLine(NewLine))
                            .AddRange(membersToAdd[0].GetLeadingTrivia()));
                        return newClassDeclaration.AddMembers(membersToAdd);
                    }
                }
            }

            return classDeclaration.AddMembers(membersToAdd);
        }       
开发者ID:pgenfer,项目名称:mixinSharp,代码行数:65,代码来源:IncludeMixinSyntaxWriter.cs

示例2: VisitClassDeclaration

        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            // needed to evaluate whether type names can be reduced (depends on the using statements in the file)
            var positionOfClassInSourceFile = node.GetLocation().SourceSpan.Start;
            // strategy to implement constructor injection
            _injectMixinIntoConstructor = new InjectConstructorImplementationStrategy(
                _mixin, _semantic, positionOfClassInSourceFile);

            // base handling will call other visitors
            var classDeclaration = (ClassDeclarationSyntax)base.VisitClassDeclaration(node);

            // create a new constructor and add it to the class declaration
            if (!_SourceClassHasConstructor)
                classDeclaration = classDeclaration.AddMembers(
                    _injectMixinIntoConstructor.CreateNewConstructor(classDeclaration.Identifier.Text));

            return classDeclaration;
        }
开发者ID:pgenfer,项目名称:mixinSharp,代码行数:18,代码来源:ConstructorInjectionSyntaxWriter.cs


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