本文整理汇总了C#中Microsoft.CodeAnalysis.Solution.GetProject方法的典型用法代码示例。如果您正苦于以下问题:C# Solution.GetProject方法的具体用法?C# Solution.GetProject怎么用?C# Solution.GetProject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.Solution
的用法示例。
在下文中一共展示了Solution.GetProject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindTarget
public async Task<Target> FindTarget(Solution solution)
{
if (solution == null)
{
return new Target();
}
var classDeclarationSyntaxes = solution.GetProjectDependencyGraph().GetTopologicallySortedProjects()
.Select(async projectId =>
{
var project = solution.GetProject(projectId);
var compilation = (await project.GetCompilationAsync());
return compilation;
})
.Select(compilationTask => compilationTask.Result)
.Where(compilation => GetClassDeclarationSyntaxes(compilation).Any())
.Select(compilation => new Target() { Compilation = compilation, Node = GetClassDeclarationSyntaxes(compilation).First() });
var foundNamespaceDeclarationSyntax = new Target();
if (classDeclarationSyntaxes.Any())
{
foundNamespaceDeclarationSyntax = classDeclarationSyntaxes.First();
}
return foundNamespaceDeclarationSyntax;
}
示例2: ProjectCodeProviderByNameAsync
async internal static Task<ProjectCodeProvider> ProjectCodeProviderByNameAsync(Solution solution, string name)
{
foreach (var id in solution.ProjectIds)
{
var project = solution.GetProject(id);
if (project.Name.Equals(name))
{
return new ProjectCodeProvider(project, await project.GetCompilationAsync());
}
}
Contract.Assert(false, "Can't find project named = " + name);
return null;
}
示例3: FindSource
public async Task<SyntaxNode> FindSource(Solution solution, string activeDocument, int cursorPosition)
{
if (solution == null || string.IsNullOrEmpty(activeDocument) || cursorPosition < 0)
{
return null;
}
var compilationTasks = solution.GetProjectDependencyGraph().GetTopologicallySortedProjects()
.Select(projectId =>
{
var project = solution.GetProject(projectId);
var compilation = project.GetCompilationAsync();
return compilation;
});
foreach (var task in compilationTasks)
{
task.Wait();
}
var invocationExpressions = compilationTasks
.Select(task => task.Result)
.SelectMany(compilation => compilation.SyntaxTrees)
.Where(compilation => FileNameComparer.SameFile(compilation.FilePath, activeDocument))
.Select(syntaxTree => syntaxTree.GetRoot())
.SelectMany(root => root.DescendantNodesAndSelf())
.Where(syntaxNode => syntaxNode.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.MethodDeclaration))
.SelectMany(methodDeclaration => methodDeclaration.DescendantNodesAndSelf())
.Where(IsSource)
.Where(syntaxNode => syntaxNode.Span.Contains(cursorPosition));
SyntaxNode foundInvocationExpression = null;
if (invocationExpressions.Any())
{
foundInvocationExpression = invocationExpressions.First();
}
return foundInvocationExpression;
}
示例4: InnerCalculate
private async Task<IProjectMetric> InnerCalculate(Project project, Task<Compilation> compilationTask, Solution solution)
{
if (project == null)
{
return null;
}
var compilation = await compilationTask.ConfigureAwait(false);
var metricsTask = _metricsCalculator.Calculate(project, solution);
IEnumerable<string> dependencies;
if (solution != null)
{
var dependencyGraph = solution.GetProjectDependencyGraph();
dependencies = dependencyGraph.GetProjectsThatThisProjectTransitivelyDependsOn(project.Id)
.Select<ProjectId, Project>(id => solution.GetProject(id))
.SelectMany(x => x.MetadataReferences.Select(y => y.Display).Concat(new[] { x.AssemblyName }));
}
else
{
dependencies = project.AllProjectReferences.SelectMany(x => x.Aliases)
.Concat(project.MetadataReferences.Select(y => y.Display));
}
var assemblyTypes = compilation.Assembly.TypeNames;
var metrics = (await metricsTask.ConfigureAwait(false)).AsArray();
var internalTypesUsed = from metric in metrics
from coupling in metric.ClassCouplings
where coupling.Assembly == project.AssemblyName
select coupling;
var relationalCohesion = (internalTypesUsed.Count() + 1.0) / assemblyTypes.Count;
return new ProjectMetric(project.Name, metrics, dependencies, relationalCohesion);
}
示例5: GetProjectDiagnosticsMappedToNewSolution
private static ImmutableDictionary<Project, ImmutableArray<Diagnostic>> GetProjectDiagnosticsMappedToNewSolution(ImmutableDictionary<Project, ImmutableArray<Diagnostic>> projectDiagnosticsToFixMap, Solution newSolution, string language)
{
ImmutableDictionary<Project, ImmutableArray<Diagnostic>>.Builder projectDiagsBuilder = null;
foreach (var kvp in projectDiagnosticsToFixMap)
{
if (kvp.Key.Language != language)
{
continue;
}
var project = newSolution.GetProject(kvp.Key.Id);
if (project != null)
{
projectDiagsBuilder = projectDiagsBuilder ?? ImmutableDictionary.CreateBuilder<Project, ImmutableArray<Diagnostic>>();
projectDiagsBuilder.Add(project, kvp.Value);
}
}
return projectDiagsBuilder != null ? projectDiagsBuilder.ToImmutable() : ImmutableDictionary<Project, ImmutableArray<Diagnostic>>.Empty;
}
示例6: RemoveNonExistingFiles
private static Solution RemoveNonExistingFiles(Solution solution)
{
foreach (var projectId in solution.ProjectIds.ToArray())
{
var project = solution.GetProject(projectId);
solution = RemoveNonExistingDocuments(project);
project = solution.GetProject(projectId);
solution = RemoveNonExistingReferences(project);
}
return solution;
}
示例7: DisambiguateSameNameLinkedFiles
private static Solution DisambiguateSameNameLinkedFiles(Solution solution)
{
foreach (var projectId in solution.ProjectIds.ToArray())
{
var project = solution.GetProject(projectId);
solution = DisambiguateSameNameLinkedFiles(project);
}
return solution;
}
示例8: GetPartialCompilationState
/// <summary>
/// Tries to get the latest snapshot of the compilation without waiting for it to be
/// fully built. This method takes advantage of the progress side-effect produced during
/// BuildCompilation. It will either return the already built compilation, any
/// in-progress compilation or any known old compilation in that order of preference.
/// The compilation state that is returned will have a compilation that is retained so
/// that it cannot disappear.
/// </summary>
private void GetPartialCompilationState(
Solution solution,
DocumentId id,
out ProjectState inProgressProject,
out Compilation inProgressCompilation,
CancellationToken cancellationToken)
{
var state = this.ReadState();
inProgressCompilation = state.Compilation.GetValue(cancellationToken);
// check whether we can bail out quickly for typing case
var inProgressState = state as InProgressState;
// all changes left for this document is modifying the given document.
// we can use current state as it is since we will replace the document with latest document anyway.
if (inProgressState != null &&
inProgressCompilation != null &&
inProgressState.IntermediateProjects.All(t => TouchDocumentActionForDocument(t, id)))
{
inProgressProject = this.ProjectState;
return;
}
inProgressProject = inProgressState != null ? inProgressState.IntermediateProjects.First().Item1 : this.ProjectState;
// if we already have a final compilation we are done.
if (inProgressCompilation != null && state is FinalState)
{
return;
}
// 1) if we have an in-progress compilation use it.
// 2) If we don't, then create a simple empty compilation/project.
// 3) then, make sure that all it's p2p refs and whatnot are correct.
if (inProgressCompilation == null)
{
inProgressProject = inProgressProject.RemoveAllDocuments();
inProgressCompilation = this.CreateEmptyCompilation(solution);
}
// first remove all project from the project and compilation.
inProgressProject = inProgressProject.WithProjectReferences(ImmutableList.Create<ProjectReference>());
// Now add in back a consistent set of project references. For project references
// try to get either a CompilationReference or a SkeletonReference. This ensures
// that the in-progress project only reports a reference to another project if it
// could actually get a reference to that project's metadata.
var metadataReferences = new List<MetadataReference>();
var newProjectReferences = new List<ProjectReference>();
metadataReferences.AddRange(this.ProjectState.MetadataReferences);
foreach (var projectReference in this.ProjectState.ProjectReferences)
{
var referencedProject = solution.GetProject(projectReference.ProjectId);
if (referencedProject != null)
{
if (referencedProject.IsSubmission)
{
var compilation = solution.GetCompilationAsync(projectReference.ProjectId, cancellationToken).WaitAndGetResult(cancellationToken);
inProgressCompilation = inProgressCompilation.WithPreviousSubmission(compilation);
}
else
{
// get the latest metadata for the partial compilation of the referenced project.
var metadata = solution.GetPartialMetadataReference(projectReference, this.ProjectState, cancellationToken);
if (metadata == null)
{
// if we failed to get the metadata, check to see if we previously had existing metadata and reuse it instead.
metadata = inProgressCompilation.References.FirstOrDefault(r => solution.GetProjectId(r) == projectReference.ProjectId);
}
if (metadata != null)
{
newProjectReferences.Add(projectReference);
metadataReferences.Add(metadata);
}
}
}
}
inProgressProject = inProgressProject.AddProjectReferences(newProjectReferences);
if (!Enumerable.SequenceEqual(inProgressCompilation.References, metadataReferences))
{
inProgressCompilation = inProgressCompilation.WithReferences(metadataReferences);
}
}
示例9: GetProjectsWithInstalledPackage
public IEnumerable<Project> GetProjectsWithInstalledPackage(Solution solution, string packageName, string version)
{
ThisCanBeCalledOnAnyThread();
var result = new List<Project>();
foreach (var kvp in this._projectToInstalledPackageAndVersion)
{
var installedPackageAndVersion = kvp.Value;
if (installedPackageAndVersion != null)
{
string installedVersion;
if (installedPackageAndVersion.TryGetValue(packageName, out installedVersion) && installedVersion == version)
{
var project = solution.GetProject(kvp.Key);
if (project != null)
{
result.Add(project);
}
}
}
}
return result;
}
示例10: GetObservedCompilation
private ObjectReference GetObservedCompilation(Solution solution, ProjectId projectId)
{
var observed = solution.GetProject(projectId).GetCompilationAsync().Result;
return new ObjectReference(observed);
}
示例11: RemoveTablePreprocessorSymbol
/// <summary>
/// Remove the added table preprocessor symbol. Don't want that saved into the project
/// file as a change.
/// </summary>
private Solution RemoveTablePreprocessorSymbol(Solution newSolution, Solution oldSolution)
{
var projectIds = newSolution.ProjectIds;
foreach (var projectId in projectIds)
{
var oldProject = oldSolution.GetProject(projectId);
var newProject = newSolution.GetProject(projectId);
newSolution = newProject.WithParseOptions(oldProject.ParseOptions).Solution;
}
return newSolution;
}
示例12: AddTablePreprocessorSymbol
private Solution AddTablePreprocessorSymbol(Solution solution)
{
var projectIds = solution.ProjectIds;
foreach (var projectId in projectIds)
{
var project = solution.GetProject(projectId);
var parseOptions = project.ParseOptions as CSharpParseOptions;
if (parseOptions != null)
{
var list = new List<string>();
list.AddRange(parseOptions.PreprocessorSymbolNames);
list.Add(TablePreprocessorSymbolName);
parseOptions = parseOptions.WithPreprocessorSymbols(list);
solution = project.WithParseOptions(parseOptions).Solution;
}
}
return solution;
}
示例13: GetProviderContainingEntryPointAsync
internal static async Task<Tuple<ProjectCodeProvider, IMethodSymbol, SyntaxTree>> GetProviderContainingEntryPointAsync(Solution solution, CancellationToken cancellationToken = default(CancellationToken))
{
var projectIDs = solution.GetProjectDependencyGraph().GetTopologicallySortedProjects(cancellationToken);
var continuations = new BlockingCollection<Task<Tuple<ProjectCodeProvider, IMethodSymbol>>>();
foreach (var projectId in projectIDs)
{
var project = solution.GetProject(projectId);
var pair = await ProjectCodeProvider.GetProviderContainingEntryPointAsync(project);
if (pair != null)
{
return pair;
}
}
//foreach (var continuation in continuations) {
// var pair = await continuation;
// if (pair != null)
// {
// return pair;
// }
//}
return null;
}
示例14: FinalizeCompilationAsync
/// <summary>
/// Add all appropriate references to the compilation and set it as our final compilation
/// state.
/// </summary>
private async Task<CompilationInfo> FinalizeCompilationAsync(
Solution solution,
Compilation compilation,
CancellationToken cancellationToken)
{
try
{
bool hasCompleteReferences = true;
var newReferences = new List<MetadataReference>();
newReferences.AddRange(this.ProjectState.MetadataReferences);
foreach (var projectReference in this.ProjectState.ProjectReferences)
{
var referencedProject = solution.GetProject(projectReference.ProjectId);
// Even though we're creating a final compilation (vs. an in progress compilation),
// it's possible that the target project has been removed.
if (referencedProject != null)
{
// If both projects are submissions, we'll count this as a previous submission link
// instead of a regular metadata reference
if (referencedProject.IsSubmission)
{
// if the referenced project is a submission project must be a submission as well:
Debug.Assert(this.ProjectState.IsSubmission);
var previousSubmissionCompilation =
await solution.GetCompilationAsync(projectReference.ProjectId, cancellationToken).ConfigureAwait(false);
compilation = compilation.WithScriptCompilationInfo(
compilation.ScriptCompilationInfo.WithPreviousScriptCompilation(previousSubmissionCompilation));
}
else
{
var metadataReference = await solution.GetMetadataReferenceAsync(
projectReference, this.ProjectState, cancellationToken).ConfigureAwait(false);
// A reference can fail to be created if a skeleton assembly could not be constructed.
if (metadataReference != null)
{
newReferences.Add(metadataReference);
}
else
{
hasCompleteReferences = false;
}
}
}
}
if (!Enumerable.SequenceEqual(compilation.ExternalReferences, newReferences))
{
compilation = compilation.WithReferences(newReferences);
}
this.WriteState(new FinalState(State.CreateValueSource(compilation, solution.Services), hasCompleteReferences), solution);
return new CompilationInfo(compilation, hasCompleteReferences);
}
catch (Exception e) when (FatalError.ReportUnlessCanceled(e))
{
throw ExceptionUtilities.Unreachable;
}
}
示例15: CreateSubmissionProject
private Project CreateSubmissionProject(Solution solution)
{
string name = "Program" + _documentNumber++;
ProjectId id = ProjectId.CreateNewId(name);
solution = solution.AddProject(ProjectInfo.Create(id, VersionStamp.Create(), name, name, LanguageNames.CSharp,
parseOptions: _parseOptions,
compilationOptions: _compilationOptions.WithScriptClassName(name),
metadataReferences: _references));
//if (_previousProjectId != null)
//{
// solution = solution.AddProjectReference(id, new ProjectReference(_previousProjectId));
//}
return solution.GetProject(id);
}