C#如何抽取cs文件中各个函数的摘要信息和“#region '功能简短说明'”行。

解决方案 »

  1.   

    “#region '功能简短说明'”行
    这个没法实现吧,因为#region事实上也就是一些注释性的东西C#如何抽取cs文件中各个函数的摘要信息
    这个可以实现:
    1、到http://www.codeplex.com/csparser下载一个release,在你的项目上引用其中的CSParser.dll文件
    2、在你的项目中,添加一个cs文件,写入下面的代码:    using Mono.CSharp;    public sealed class CSharpCodeVisualizer
        {
            private CodeCompileUnit codeCompileUnit__;        /// <summary>
            /// Parses the code and generate the compiled units.
            /// </summary>
            /// <param name="_code">The source code that is being parsed.</param>
            /// <returns>Zero if success, otherwise a non-zero value would return.</returns>
            /// <res>
            /// For more information about compiled units, please refer to <see cref="System.CodeDom.CodeCompileUnit"/>.
            /// </res>
            /// <seealso cref="System.CodeDom.CodeCompileUnit"/>
            public int Parse(string _code)
            {
                MemoryStream memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(_code));
                CSharpParser parser = new CSharpParser(null, memoryStream, null);
                int ret = parser.parse();
                codeCompileUnit__ = parser.Builder.CurrCompileUnit;
                return ret;
            }        /// <summary>
            /// Gets the compiled unit information after the calling of <see cref="Parse"/> method.
            /// </summary>
            public CodeCompileUnit CodeCompileUnit
            {
                get { return codeCompileUnit__; }
            }
        }3、调用上面的方法:TextReader reader = new TextReader("c:\\abc.cs");
    string code = reader.ReadToEnd();
    CSharpCodeVisualizer visualizer = new CSharpCodeVisualizer();
    visualizer.Parse(code);
    foreach (CodeNamespace ns in visualizer.CodeCompileUnit.Namespaces)
    {
         // ns就是你代码中的namespaces
        foreach (CodeTypeDeclaration typeDeclaration in ns.Types)
        {
            // typeDeclaration就是你代码中的所有类型(包括class、interface、enum等)
             foreach (CodeTypeMember member in typeDeclaration.Members)
            {
                // member就是每个类型中的成员,有property、method等,你自己可以通过member中的属性进行判断
            }
        }
    }
    希望这些对你有帮助!
      

  2.   


    先生,C#或net开发环境自身没有这个功能吗
      

  3.   


    晕死,我自己需求还没搞清楚。哈哈。VS不是有个Class Explorer么?里面不是什么都有了?
      

  4.   

    1.C#如何抽取cs文件中各个函数
    你指的是什么?

    /// <summary>
    /// Gets the compiled unit information after the calling of <see cref="Parse"/> method.
    /// </summary>来对一个方法进行说明的文字吗
    2.#region 说明性文字
    这里可以是任何东西,方法,属性.....
    #endregion
      

  5.   

    vs自己是有的,在编译的选项里,选择输出xml那个东东就成,他会在编译的生成一份说明文档
      

  6.   

    项目-工程属性-生成-把输出栏下的“输出xml”勾上
      

  7.   

    就是编译的时候能够自动生成的xml文件,用于生成class library文档的。
      

  8.   

    就是编译的时候能够自动生成的xml文件,用于生成class library文档的