本文整理汇总了C#中Microsoft.CodeAnalysis.AdhocWorkspace.GetProject方法的典型用法代码示例。如果您正苦于以下问题:C# AdhocWorkspace.GetProject方法的具体用法?C# AdhocWorkspace.GetProject怎么用?C# AdhocWorkspace.GetProject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.AdhocWorkspace
的用法示例。
在下文中一共展示了AdhocWorkspace.GetProject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDocument
internal static Document CreateDocument(string code, string fileName,
Func<Solution, ProjectId, Solution> modifySolution)
{
var projectName = "Test";
var projectId = ProjectId.CreateNewId(projectName);
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, projectName, projectName, LanguageNames.CSharp)
.AddMetadataReference(projectId,
MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
.AddMetadataReference(projectId,
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location))
.AddMetadataReference(projectId,
MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location))
.AddMetadataReference(projectId,
MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location));
var documentId = DocumentId.CreateNewId(projectId);
solution = solution.AddDocument(documentId, fileName, SourceText.From(code));
if(modifySolution != null)
{
solution = modifySolution(solution, projectId);
}
return solution.GetProject(projectId).Documents.First();
}
示例2: CreateProject
private static Project CreateProject(string filePath)
{
var projectId = ProjectId.CreateNewId(TestProjectName);
var source = File.ReadAllText(filePath);
var fileName = Path.GetFileName(filePath);
var documentId = DocumentId.CreateNewId(projectId, fileName);
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
.AddDocument(documentId, fileName, SourceText.From(source));
return solution.GetProject(projectId);
}
示例3: CreateProject
static Project CreateProject(string source)
{
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
var projectId = ProjectId.CreateNewId();
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, "TestProject", "TestProject", LanguageNames.CSharp)
.WithProjectCompilationOptions(projectId, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location))
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")))
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(ConstAttribute).Assembly.Location));
var documentId = DocumentId.CreateNewId(projectId);
solution = solution.AddDocument(documentId, "Test.cs", SourceText.From(source));
return solution.GetProject(projectId);
}
示例4: Create
internal static Document Create(string code)
{
var projectName = "Test";
var projectId = ProjectId.CreateNewId(projectName);
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, projectName, projectName, LanguageNames.CSharp)
.WithProjectCompilationOptions(projectId, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddMetadataReference(projectId, MetadataReference.CreateFromAssembly(typeof(object).Assembly))
.AddMetadataReference(projectId, MetadataReference.CreateFromAssembly(typeof(Enumerable).Assembly))
.AddMetadataReference(projectId, MetadataReference.CreateFromAssembly(typeof(CSharpCompilation).Assembly))
.AddMetadataReference(projectId, MetadataReference.CreateFromAssembly(typeof(Compilation).Assembly))
.AddMetadataReference(projectId, MetadataReference.CreateFromAssembly(typeof(BusinessBase<>).Assembly));
var documentId = DocumentId.CreateNewId(projectId);
solution = solution.AddDocument(documentId, "Test.cs", SourceText.From(code));
return solution.GetProject(projectId).Documents.First();
}
示例5: CreateSolution
private Project CreateSolution(string source)
{
var testProjectName = "Test";
var projectId = ProjectId.CreateNewId(testProjectName);
var references = new[]
{
s_CorlibReference,
s_SystemCoreReference,
s_MSTestReference,
s_XunitReference
};
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, testProjectName, testProjectName, LanguageNames.CSharp)
.AddMetadataReferences(projectId, references);
var fileName = "File.cs";
var documentId = DocumentId.CreateNewId(projectId, fileName);
solution = solution.AddDocument(documentId, fileName, SourceText.From(source));
return solution.GetProject(projectId);
}
示例6: TestGetProjectForAssemblySymbol
public void TestGetProjectForAssemblySymbol()
{
var pid1 = ProjectId.CreateNewId("p1");
var pid2 = ProjectId.CreateNewId("p2");
var pid3 = ProjectId.CreateNewId("p3");
var did1 = DocumentId.CreateNewId(pid1);
var did2 = DocumentId.CreateNewId(pid2);
var did3 = DocumentId.CreateNewId(pid3);
var text1 = @"
Public Class A
End Class";
var text2 = @"
Public Class B
End Class
";
var text3 = @"
public class C : B {
}
";
var text4 = @"
public class C : A {
}
";
var solution = new AdhocWorkspace().CurrentSolution
.AddProject(pid1, "FooA", "Foo.dll", LanguageNames.VisualBasic)
.AddDocument(did1, "A.vb", text1)
.AddMetadataReference(pid1, s_mscorlib)
.AddProject(pid2, "FooB", "Foo2.dll", LanguageNames.VisualBasic)
.AddDocument(did2, "B.vb", text2)
.AddMetadataReference(pid2, s_mscorlib)
.AddProject(pid3, "Bar", "Bar.dll", LanguageNames.CSharp)
.AddDocument(did3, "C.cs", text3)
.AddMetadataReference(pid3, s_mscorlib)
.AddProjectReference(pid3, new ProjectReference(pid1))
.AddProjectReference(pid3, new ProjectReference(pid2));
var project3 = solution.GetProject(pid3);
var comp3 = project3.GetCompilationAsync().Result;
var classC = comp3.GetTypeByMetadataName("C");
var projectForBaseType = solution.GetProject(classC.BaseType.ContainingAssembly);
Assert.Equal(pid2, projectForBaseType.Id);
// switch base type to A then try again
var solution2 = solution.WithDocumentText(did3, SourceText.From(text4));
project3 = solution2.GetProject(pid3);
comp3 = project3.GetCompilationAsync().Result;
classC = comp3.GetTypeByMetadataName("C");
projectForBaseType = solution2.GetProject(classC.BaseType.ContainingAssembly);
Assert.Equal(pid1, projectForBaseType.Id);
}
示例7: CreateProject
private static Project CreateProject(string[] sources, string language = LanguageNames.VisualBasic)
#endif
{
string fileNamePrefix = DefaultFilePathPrefix;
string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;
var projectId = ProjectId.CreateNewId(debugName: TestProjectName);
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, language)
#if CSHARP
.WithProjectCompilationOptions(projectId, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
#elif VISUAL_BASIC
.WithProjectCompilationOptions(projectId, new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
#endif
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
#if CSHARP
.AddMetadataReference(projectId, CSharpSymbolsReference)
#elif VISUAL_BASIC
.AddMetadataReference(projectId, VisualBasicSymbolsReference)
#endif
.AddMetadataReference(projectId, CodeAnalysisReference)
.AddMetadataReference(projectId, FakeItEasyReference);
int count = 0;
foreach (var source in sources)
{
var newFileName = fileNamePrefix + count + "." + fileExt;
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, SourceText.From(source));
count++;
}
return solution.GetProject(projectId);
}
示例8: CreateSolution
/// <summary>
/// Creates a solution that will be used as parent for the sources that need to be checked.
/// </summary>
/// <param name="projectId">The project identifier to use.</param>
/// <param name="language">The language for which the solution is being created.</param>
/// <returns>The created solution.</returns>
protected virtual Solution CreateSolution(ProjectId projectId, string language)
{
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);
var additionalDiagnosticOptions = this.GetDisabledDiagnostics().Select(id => new KeyValuePair<string, ReportDiagnostic>(id, ReportDiagnostic.Suppress));
var newSpecificOptions = compilationOptions.SpecificDiagnosticOptions.AddRange(additionalDiagnosticOptions);
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(newSpecificOptions);
Solution solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, language)
.WithProjectCompilationOptions(projectId, compilationOptions)
.AddMetadataReference(projectId, MetadataReferences.CorlibReference)
.AddMetadataReference(projectId, MetadataReferences.SystemReference)
.AddMetadataReference(projectId, MetadataReferences.SystemCoreReference)
.AddMetadataReference(projectId, MetadataReferences.CSharpSymbolsReference)
.AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference);
var settings = this.GetSettings();
if (!string.IsNullOrEmpty(settings))
{
var documentId = DocumentId.CreateNewId(projectId);
solution = solution.AddAdditionalDocument(documentId, SettingsFileName, settings);
}
ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions;
return solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose));
}
示例9: CreateSolution
/// <summary>
/// Creates a solution that will be used as parent for the sources that need to be checked.
/// </summary>
/// <param name="projectId">The project identifier to use.</param>
/// <param name="language">The language for which the solution is being created.</param>
/// <returns>The created solution.</returns>
protected virtual Solution CreateSolution(ProjectId projectId, string language)
{
Solution solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, language)
.WithProjectCompilationOptions(projectId, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true))
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemReference)
.AddMetadataReference(projectId, SystemCoreReference)
.AddMetadataReference(projectId, CSharpSymbolsReference)
.AddMetadataReference(projectId, CodeAnalysisReference);
ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions;
return solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose));
}
示例10: CreateProject
protected Project CreateProject(Dictionary<string, string> sources)
{
string fileNamePrefix = DefaultFilePathPrefix;
string fileExt = CSharpDefaultFileExt;
var projectId = ProjectId.CreateNewId(debugName: TestProjectName);
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp);
foreach (var reference in References)
{
solution = solution.AddMetadataReference(projectId, reference);
}
int count = 0;
foreach (var source in sources)
{
var newFileName = source.Key;
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, SourceText.From(source.Value));
count++;
}
var project = solution.GetProject(projectId)
.WithCompilationOptions(CompilationOptions);
return project;
}
示例11: CreateProject
private Project CreateProject(IEnumerable<string> sourceCodes)
{
ProjectId projectId = ProjectId.CreateNewId(debugName: ADHOC_PROJECT_NAME);
Solution solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, ADHOC_PROJECT_NAME, ADHOC_PROJECT_NAME, LanguageNames.CSharp)
.AddMetadataReferences(projectId, new[]
{
CorlibReference,
SystemCoreReference,
CSharpSymbolsReference,
CodeAnalysisReference
});
string sourceFileName;
DocumentId documentId;
int index = 0;
foreach (string sourceCode in sourceCodes)
{
sourceFileName = $"Test{index++}.cs";
documentId = DocumentId.CreateNewId(projectId, debugName: sourceFileName);
solution = solution.AddDocument(documentId, sourceFileName, SourceText.From(sourceCode));
}
return solution.GetProject(projectId);
}
示例12: CreateProject
private static Project CreateProject(string[] sources, string language)
{
var fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;
var projectId = ProjectId.CreateNewId(TestProjectName);
var solution = new AdhocWorkspace().CurrentSolution.AddProject(projectId, TestProjectName, TestProjectName, language)
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
.AddMetadataReference(projectId, CSharpSymbolsReference)
.AddMetadataReference(projectId, CodeAnalysisReference)
.AddMetadataReference(projectId, OpenTKReference);
for (int i = 0; i < sources.Length; i++)
{
var newFileName = DefaultFilePathPrefix + i + "." + fileExt;
var documentId = DocumentId.CreateNewId(projectId, newFileName);
solution = solution.AddDocument(documentId, newFileName, SourceText.From(sources[i]));
}
return solution.GetProject(projectId);
}
示例13: PinvokeMethodReferences_VB
public void PinvokeMethodReferences_VB()
{
var tree = Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(
@"
Module Module1
Declare Function CreateDirectory Lib ""kernel32"" Alias ""CreateDirectoryA"" (ByVal lpPathName As String) As Integer
Private prop As Integer
Property Prop1 As Integer
Get
Return prop
End Get
Set(value As Integer)
CreateDirectory(""T"") ' Method Call 1
prop = value
prop = Nothing
End Set
End Property
Sub Main()
CreateDirectory(""T"") 'Method Call 2
NormalMethod() ' Method Call 1
NormalMethod() ' Method Call 2
End Sub
Sub NormalMethod()
End Sub
End Module
");
ProjectId prj1Id = ProjectId.CreateNewId();
DocumentId docId = DocumentId.CreateNewId(prj1Id);
Microsoft.CodeAnalysis.Solution sln = new AdhocWorkspace().CurrentSolution
.AddProject(prj1Id, "testDeclareReferences", "testAssembly", LanguageNames.VisualBasic)
.AddMetadataReference(prj1Id, MscorlibRef)
.AddDocument(docId, "testFile", tree.GetText());
Microsoft.CodeAnalysis.Project prj = sln.GetProject(prj1Id).WithCompilationOptions(new VisualBasic.VisualBasicCompilationOptions(OutputKind.ConsoleApplication, embedVbCoreRuntime: true));
tree = (SyntaxTree)prj.GetDocument(docId).GetSyntaxTreeAsync().Result;
Compilation comp = prj.GetCompilationAsync().Result;
SemanticModel semanticModel = comp.GetSemanticModel(tree);
SyntaxNode declareMethod = tree.GetRoot().DescendantNodes().OfType<Microsoft.CodeAnalysis.VisualBasic.Syntax.DeclareStatementSyntax>().FirstOrDefault();
SyntaxNode normalMethod = tree.GetRoot().DescendantNodes().OfType<Microsoft.CodeAnalysis.VisualBasic.Syntax.MethodStatementSyntax>().ToList()[1];
// declared method calls
var symbol = semanticModel.GetDeclaredSymbol(declareMethod);
var references = SymbolFinder.FindReferencesAsync(symbol, prj.Solution).Result;
Assert.Equal(expected: 2, actual: references.ElementAt(0).Locations.Count());
// normal method calls
symbol = semanticModel.GetDeclaredSymbol(normalMethod);
references = SymbolFinder.FindReferencesAsync(symbol, prj.Solution).Result;
Assert.Equal(expected: 2, actual: references.ElementAt(0).Locations.Count());
}
示例14: CreateSolution
/// <summary>
/// Creates a solution that will be used as parent for the sources that need to be checked.
/// </summary>
/// <param name="projectId">The project identifier to use.</param>
/// <param name="language">The language for which the solution is being created.</param>
/// <returns>The created solution.</returns>
protected virtual Solution CreateSolution(ProjectId projectId, string language)
{
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);
Solution solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, language)
.WithProjectCompilationOptions(projectId, compilationOptions)
.AddMetadataReference(projectId, MetadataReferences.CorlibReference)
.AddMetadataReference(projectId, MetadataReferences.SystemReference)
.AddMetadataReference(projectId, MetadataReferences.SystemCoreReference)
.AddMetadataReference(projectId, MetadataReferences.CSharpSymbolsReference)
.AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference);
var settings = this.GetSettings();
if (!string.IsNullOrEmpty(settings))
{
var documentId = DocumentId.CreateNewId(projectId);
solution = solution.AddAdditionalDocument(documentId, SettingsHelper.PublicApiFileName, settings);
}
ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions;
return solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose));
}
示例15: CreateProject
/// <summary>
/// Create a project using the inputted strings as sources.
/// </summary>
/// <param name="sources">Classes in the form of strings</param>
/// <param name="language">The language the source code is in</param>
/// <returns>A Project created out of the Documents created from the source strings</returns>
private static Project CreateProject(string[] sources)
{
string TestProjectName = "TestProject";
var projectId = ProjectId.CreateNewId(debugName: TestProjectName);
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
.AddMetadataReference(projectId, CSharpSymbolsReference)
.AddMetadataReference(projectId, CodeAnalysisReference)
.AddMetadataReference(projectId, EntityReference)
.AddMetadataReference(projectId, UtilitiesReference);
int count = 0;
foreach (var source in sources)
{
var newFileName = "Test" + count + ".cs";
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, SourceText.From(source));
count++;
}
return solution.GetProject(projectId);
}