Exemplo n.º 1
0
        private MetadataReference CreateMetadataReference(string assemblyDisplayName, bool includeDocumentation)
        {
            string assemblyFullPath = FileResolver.Default.ResolveAssemblyName(assemblyDisplayName);
            string xmlFile          = Path.Combine(_systemXmlFilesDir, assemblyDisplayName + ".xml");

            RoslynDocumentationProvider documentationProvider = null;

            if (includeDocumentation)
            {
                documentationProvider = new RoslynDocumentationProvider(xmlFile);
            }

            return(new MetadataFileReference(assemblyFullPath, MetadataReferenceProperties.Assembly, documentationProvider));
        }
Exemplo n.º 2
0
        private ISemanticModel GetSemanticModelFromSyntaxTree(CommonSyntaxTree syntaxTree, string code, bool includeDocumentation = false)
        {
            _systemXmlFilesDir = GetSystemXmlFilesDir();

            MetadataReference mscorlib = CreateMetadataReference("mscorlib", includeDocumentation);
            var metaDllReferences      = new List <MetadataReference> {
                mscorlib
            };

            if (this.Language == Language.VbNet)
            {
                //Need to add vb or getting error
                //Requested operation is not available because the runtime library function 'Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute..ctor' is not defined.
                MetadataReference vb = CreateMetadataReference("Microsoft.VisualBasic", includeDocumentation);
                metaDllReferences.Add(vb);
            }


            //Eric: this doesn't seem to work so using mscorlib only for now.

            List <string> gacDlls = GetGacDlls(code);

            foreach (string dllName in gacDlls)
            {
                string dllNameWithoutExtension = dllName.Substring(0, dllName.Length - 4);                 //remove .dll

                MetadataReference metaRef = CreateMetadataReference(dllNameWithoutExtension, includeDocumentation);
                metaDllReferences.Add(metaRef);
            }

            Dictionary <string, string> nugGetXmlFileNameToPath = new Dictionary <string, string>();

            if (includeDocumentation)
            {
                foreach (var nuGetxmlFilePath in NuGetXmlFileReferences)
                {
                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(nuGetxmlFilePath);
                    nugGetXmlFileNameToPath[fileNameWithoutExtension] = nuGetxmlFilePath;
                }
            }


            foreach (var path in GetNonGacDlls())
            {
                string fileName = Path.GetFileNameWithoutExtension(path);

                RoslynDocumentationProvider documentationProvider = null;
                if (includeDocumentation && nugGetXmlFileNameToPath.ContainsKey(fileName))
                {
                    documentationProvider = new RoslynDocumentationProvider(nugGetXmlFileNameToPath[fileName]);
                }

                var reference = new MetadataFileReference(path, MetadataReferenceProperties.Assembly, documentationProvider);
                metaDllReferences.Add(reference);
            }



            //http://msdn.microsoft.com/en-us/vstudio/hh500769.aspx#Toc306015688

            CommonCompilation compilation = this.CreateCompilation("Compilation_For_AutoComplete",
                                                                   syntaxTrees: new[] { syntaxTree },
                                                                   matadataReferences: metaDllReferences);

            ISemanticModel semanticModel = compilation.GetSemanticModel(syntaxTree);

            return(semanticModel);
        }