代码之家  ›  专栏  ›  技术社区  ›  Master Morality

vs2010自动化:获取EnvDTE.codelement的文本值

  •  7
  • Master Morality  · 技术社区  · 15 年前

    EnvDTE ,以及 EnvDTE.CodeModel API,我想知道是否有一种方法可以获得由 CodeElement

    假设我有一个 CodeAttribute ,有什么方法可以得到 string 关于什么 表示,即。 [MyAttribute(value="myvalue")] .

    我知道可以使用 码元 ,至少在某些情况下是这样,但对于某些事情来说,仅仅获取文本似乎更容易。

    谢谢!

    3 回复  |  直到 15 年前
        1
  •  4
  •   JaredPar    12 年前

    这个 CodeElement StartPoint EndPoint 表示缓冲区中元素的开始和结束。它们包含行号/列,可以传递给 IVsTextLines.GetLineText

    为了得到 IVsTextLines 对于给定的 码元

    CodeElement ce = ...;
    TextDocument td = ce.StartPoint.Parent;
    IVsTextLines lines = td as IVsTextLines;
    
        2
  •  3
  •   Maslow    14 年前
      void WriteMapping(CodeProperty codeProperty)
     {
       WriteLine("");
       WriteLine("///CodeProperty");
       WriteLine("///<summary>");
       WriteLine("///"+codeProperty.FullName);
       WriteLine("///</summary>");
       if(codeProperty.Getter==null && codeProperty.Setter==null)
           return;
       if(codeProperty.Attributes!=null){
           foreach(CodeAttribute a in codeProperty.Attributes)
            {
                Write("["+a.FullName);
                if(a.Children!=null && a.Children.Count>0)
                {
                    var start=a.Children.Cast<CodeElement>().First().GetStartPoint();
                    var finish= a.GetEndPoint();
                    string allArguments=start.CreateEditPoint().GetText(finish);
    
                    Write("("+allArguments);
                }
        WriteLine("]");
            }
            }
       Write("public "+GetFullName(codeProperty.Type) +" "+codeProperty.Prototype);
    
        Write(" {");
        //if(codeProperty.Getter!=null && codeProperty.Getter.Access!=vsCMAccess.vsCMAccessPrivate)
            Write("get;");
        //if(codeProperty.Setter!=null)
            Write("set;");
        WriteLine("}");
    
       }
    
        3
  •  2
  •   Alfero Chingono    12 年前

    除了@JaredPar的答案外,另一种方法是:

    public string GetText(CodeAttribute attribute)
    {
        return attribute.StartPoint.CreateEditPoint().GetText(attribute.EndPoint);
    } 
    

    资料来源: http://msdn.microsoft.com/en-us/library/envdte.editpoint.gettext.aspx

    推荐文章