/// <summary>
        /// Initializes a new instance of the <see cref="T:XmlDocumentParser.EasyCs.CSharpDocumentGenerator"/> class.
        /// </summary>
        /// <param name="csProjDirPath">Cs proj dir path.</param>
        /// <param name="compileType">Type of project.</param>
        public CSharpDocumentGenerator(CsprojAnalyzer csprojAnalyzer)
        {
            if (!Directory.Exists(csprojAnalyzer.CsprojParentPath))
            {
                return;
            }

            var csFilesInfo = csprojAnalyzer.GetCsFiles();
            var textArray   = from file in csFilesInfo.SourceFiles select File.ReadAllText(file);

            var metadataReferences = new List <MetadataReference>
            {
                { csFilesInfo.References, (item) => MetadataReference.CreateFromFile(item.Location) }
            };

            Analyze(textArray, metadataReferences);
        }
コード例 #2
0
        /// <summary>
        /// Parse C# codes in csproj files.
        /// </summary>
        /// <param name="csProjDirPath">Directory path included csproj file.</param>
        /// <param name="compileType">Type of project.</param>
        public void Parse(CsprojAnalyzer csprojAnalyzer)
        {
            if (!Directory.Exists(csprojAnalyzer.CsprojParentPath))
            {
                return;
            }

            var csFilesInfo = csprojAnalyzer.GetCsFiles();
            var syntaxTrees = new List <SyntaxTree>();
            int index       = 0;

            foreach (var tuple in csFilesInfo.SourceFiles.Select((v, i) => new { Value = v, Index = i }))
            {
                var text = File.ReadAllText(tuple.Value).UnifiedNewLine();

                var namespaceItem = GetNamespace(text);

                syntaxTrees.Add(CSharpSyntaxTree.ParseText(text, CSharpParseOptions.Default, tuple.Value));

                AnalysisProgress?.Invoke(this, new CSharpParseProgressEventArgs(
                                             CSharpParseProgressEventArgs.ParseType.SyntacticAnalysis, csFilesInfo.SourceFiles.Length * 2, ++index, tuple.Value));
            }

            var metadataReferences = new List <MetadataReference>
            {
                { csFilesInfo.References, (item) => MetadataReference.CreateFromFile(item.Location) }
            };

            var compilation = CSharpCompilation.Create("sample", syntaxTrees, metadataReferences);

            foreach (var tuple in syntaxTrees.Select((v, i) => new { Value = v, Index = i }))
            {
                RoslynAnalyze(tuple.Value, compilation);

                AnalysisProgress?.Invoke(this, new CSharpParseProgressEventArgs(
                                             CSharpParseProgressEventArgs.ParseType.CodeAnalysis, csFilesInfo.SourceFiles.Length * 2, ++index, tuple.Value.FilePath));
            }

            CodeAnalysisCompleted?.Invoke(this, new EventArgs());
        }