Example #1
0
        public ReflectionProvider(IEnumerable <string> assemblyPaths)
        {
            compilation = CSharpCompilation.Create("C")
                          .AddReferences(assemblyPaths.Select(path =>
            {
                DocumentationProvider documentationProvider = DocumentationProvider.Default;

                // Try to find the documentation in the framework doc path
                string docPath = Path.ChangeExtension(path, ".xml");
                if (!File.Exists(docPath))
                {
                    docPath = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
                        "Reference Assemblies/Microsoft/Framework/.NETFramework/v4.X",
                        $"{Path.GetFileNameWithoutExtension(path)}.xml");
                }

                if (File.Exists(docPath))
                {
                    documentationProvider = XmlDocumentationProvider.CreateFromFile(docPath);
                }

                return(MetadataReference.CreateFromFile(path, documentation: documentationProvider));
            }));

            documentationUtil = new DocumentationUtil(compilation);
        }
Example #2
0
        public string GetMethodReturnDocumentation(MethodSpecifier methodSpecifier, int returnIndex)
        {
            MethodInfo methodInfo = GetMethodInfoFromSpecifier(methodSpecifier);

            if (methodInfo == null)
            {
                return(null);
            }

            return(DocumentationUtil.GetMethodReturnInfo(methodInfo));
        }
Example #3
0
        // Documentation

        public string GetMethodDocumentation(MethodSpecifier methodSpecifier)
        {
            MethodInfo methodInfo = GetMethodInfoFromSpecifier(methodSpecifier);

            if (methodInfo == null)
            {
                return(null);
            }

            return(DocumentationUtil.GetMethodSummary(methodInfo));
        }
Example #4
0
        public string GetMethodParameterDocumentation(MethodSpecifier methodSpecifier, int parameterIndex)
        {
            MethodInfo methodInfo = GetMethodInfoFromSpecifier(methodSpecifier);

            if (methodInfo == null)
            {
                return(null);
            }

            string parameterName = methodInfo.GetParameters()[parameterIndex].Name;

            return(DocumentationUtil.GetMethodParameterInfo(methodInfo, parameterName));
        }
Example #5
0
        /// <summary>
        /// Creates a ReflectionProvider given paths to assemblies and source files.
        /// </summary>
        /// <param name="assemblyPaths">Paths to assemblies.</param>
        /// <param name="sourcePaths">Paths to source files.</param>
        public ReflectionProvider(IEnumerable <string> assemblyPaths, IEnumerable <string> sourcePaths, IEnumerable <string> sources)
        {
            var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

            // Create assembly metadata references
            var assemblyReferences = assemblyPaths.Select(path =>
            {
                DocumentationProvider documentationProvider = DocumentationProvider.Default;

                // Try to find the documentation in the framework doc path
                string docPath = Path.ChangeExtension(path, ".xml");
                if (!File.Exists(docPath))
                {
                    docPath = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
                        "Reference Assemblies/Microsoft/Framework/.NETFramework/v4.X",
                        $"{Path.GetFileNameWithoutExtension(path)}.xml");
                }

                if (File.Exists(docPath))
                {
                    documentationProvider = XmlDocumentationProvider.CreateFromFile(docPath);
                }

                return(MetadataReference.CreateFromFile(path, documentation: documentationProvider));
            });

            // Create syntax trees from sources
            sources = sources.Concat(sourcePaths.Select(path => File.ReadAllText(path))).Distinct();
            var syntaxTrees = sources.Select(source => ParseSyntaxTree(source));

            compilation = CSharpCompilation.Create("C", syntaxTrees, assemblyReferences, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            // Try to compile, on success create a new compilation that references the created assembly instead of the sources.
            // The compilation will fail eg. if the sources have references to the not-yet-compiled assembly.
            (EmitResult compilationResults, Stream stream) = CompileInMemory(compilation);

            if (compilationResults.Success)
            {
                assemblyReferences = assemblyReferences.Concat(new[] { MetadataReference.CreateFromStream(stream) });
                compilation        = CSharpCompilation.Create("C", references: assemblyReferences);
            }

            extensionMethods = new List <IMethodSymbol>(GetValidTypes().SelectMany(t => t.GetMethods().Where(m => m.IsExtensionMethod)));

            documentationUtil = new DocumentationUtil(compilation);
        }