本文整理汇总了C#中Microsoft.CodeAnalysis.AdhocWorkspace.GetDocument方法的典型用法代码示例。如果您正苦于以下问题:C# AdhocWorkspace.GetDocument方法的具体用法?C# AdhocWorkspace.GetDocument怎么用?C# AdhocWorkspace.GetDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.AdhocWorkspace
的用法示例。
在下文中一共展示了AdhocWorkspace.GetDocument方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateSolutionDocument
private Document CreateSolutionDocument(string sourceText)
{
var pid = ProjectId.CreateNewId();
var did = DocumentId.CreateNewId(pid);
var solution = new AdhocWorkspace().CurrentSolution
.AddProject(pid, "test", "test", LanguageNames.CSharp)
.AddMetadataReference(pid, TestReferences.NetFx.v4_0_30319.mscorlib)
.AddDocument(did, "foo.cs", SourceText.From(sourceText));
return solution.GetDocument(did);
}
示例2: CreateAnalysisContextAsync
private static async Task<SyntaxTreeAnalysisContext> CreateAnalysisContextAsync(string stylecopJSON)
{
var projectId = ProjectId.CreateNewId();
var documentId = DocumentId.CreateNewId(projectId);
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
.AddDocument(documentId, "Test0.cs", SourceText.From(string.Empty));
var document = solution.GetDocument(documentId);
var syntaxTree = await document.GetSyntaxTreeAsync().ConfigureAwait(false);
var stylecopJSONFile = new AdditionalTextHelper("stylecop.json", stylecopJSON);
var additionalFiles = ImmutableArray.Create<AdditionalText>(stylecopJSONFile);
var analyzerOptions = new AnalyzerOptions(additionalFiles);
return new SyntaxTreeAnalysisContext(syntaxTree, analyzerOptions, rd => { }, isd => { return true; }, CancellationToken.None);
}
示例3: TestWorkspaceDiagnosticHasDebuggerText
public void TestWorkspaceDiagnosticHasDebuggerText()
{
var solution = new AdhocWorkspace().CurrentSolution;
WorkspaceDiagnostic diagnostic = null;
solution.Workspace.WorkspaceFailed += (sender, args) =>
{
diagnostic = args.Diagnostic;
};
ProjectId pid = ProjectId.CreateNewId();
DocumentId did = DocumentId.CreateNewId(pid);
solution = solution.AddProject(pid, "foo", "foo", LanguageNames.CSharp)
.AddDocument(did, "x", new FileTextLoader(@"C:\doesnotexist.cs", Encoding.UTF8));
var doc = solution.GetDocument(did);
var text = doc.GetTextAsync().Result;
WaitFor(() => diagnostic != null, TimeSpan.FromSeconds(5));
Assert.NotNull(diagnostic);
var dd = diagnostic as DocumentDiagnostic;
Assert.NotNull(dd);
Assert.Equal(dd.GetDebuggerDisplay(), string.Format("[{0}] {1}", dd.Kind.ToString(), dd.Message));
}
示例4: TestDocumentFileAccessFailureMissingFile
public void TestDocumentFileAccessFailureMissingFile()
{
var solution = new AdhocWorkspace().CurrentSolution;
WorkspaceDiagnostic diagnostic = null;
solution.Workspace.WorkspaceFailed += (sender, args) =>
{
diagnostic = args.Diagnostic;
};
ProjectId pid = ProjectId.CreateNewId();
DocumentId did = DocumentId.CreateNewId(pid);
solution = solution.AddProject(pid, "foo", "foo", LanguageNames.CSharp)
.AddDocument(did, "x", new FileTextLoader(@"C:\doesnotexist.cs", Encoding.UTF8));
var doc = solution.GetDocument(did);
var text = doc.GetTextAsync().Result;
WaitFor(() => diagnostic != null, TimeSpan.FromSeconds(5));
Assert.NotNull(diagnostic);
var dd = diagnostic as DocumentDiagnostic;
Assert.NotNull(dd);
Assert.Equal(did, dd.DocumentId);
Assert.Equal(WorkspaceDiagnosticKind.Failure, dd.Kind);
}
示例5: TestTrackNodesWithDocument
public void TestTrackNodesWithDocument()
{
var pid = ProjectId.CreateNewId();
var did = DocumentId.CreateNewId(pid);
var sourceText = @"public class C { void M() { } }";
var sol = new AdhocWorkspace().CurrentSolution
.AddProject(pid, "proj", "proj", LanguageNames.CSharp)
.AddDocument(did, "doc", sourceText);
var doc = sol.GetDocument(did);
// find initial nodes of interest
var root = doc.GetSyntaxRootAsync().Result;
var classDecl = root.DescendantNodes().OfType<ClassDeclarationSyntax>().First();
var methodDecl = classDecl.DescendantNodes().OfType<MethodDeclarationSyntax>().First();
// track these nodes
var trackedRoot = root.TrackNodes(classDecl, methodDecl);
// use some fancy document centric rewrites
var comp = doc.Project.GetCompilationAsync().Result;
var gen = SyntaxGenerator.GetGenerator(doc);
var cgenField = gen.FieldDeclaration("X", gen.TypeExpression(SpecialType.System_Int32), Accessibility.Private);
var currentClassDecl = trackedRoot.GetCurrentNodes(classDecl).First();
var classDeclWithField = gen.InsertMembers(currentClassDecl, 0, new[] { cgenField });
// we can find related bits even from sub-tree fragments
var latestMethod = classDeclWithField.GetCurrentNodes(methodDecl).First();
Assert.NotNull(latestMethod);
Assert.NotEqual(latestMethod, methodDecl);
trackedRoot = trackedRoot.ReplaceNode(currentClassDecl, classDeclWithField);
// put back into document (branch solution, etc)
doc = doc.WithSyntaxRoot(trackedRoot);
// re-get root of new document
var root2 = doc.GetSyntaxRootAsync().Result;
Assert.NotEqual(trackedRoot, root2);
// we can still find the tracked node in the new document
var finalClassDecl = root2.GetCurrentNodes(classDecl).First();
Assert.Equal("public class C\r\n{\r\n private int X;\r\n void M()\r\n {\r\n }\r\n}", finalClassDecl.NormalizeWhitespace().ToString());
// and other tracked nodes too
var finalMethodDecl = root2.GetCurrentNodes(methodDecl).First();
Assert.NotNull(finalMethodDecl);
Assert.NotEqual(finalMethodDecl, methodDecl);
}
示例6: UseServices
public void UseServices()
{
var source = @"using System.Diagnostics;
using System;
using System.IO;
namespace NS
{
public class C
{
}
}
class Program
{
public static void Main()
{
System.Int32 i = 0; System.Console.WriteLine(i.ToString());
Process p = Process.GetCurrentProcess();
Console.WriteLine(p.Id);
}
}";
var projectId = ProjectId.CreateNewId();
var documentId = DocumentId.CreateNewId(projectId);
var solution = new AdhocWorkspace().CurrentSolution
.AddProject(projectId, "MyProject", "MyProject", LanguageNames.CSharp)
.AddMetadataReference(projectId, Mscorlib)
.AddMetadataReference(projectId, AppDomain.CurrentDomain.GetAssemblies()
.Where(a => string.Compare(a.GetName().Name, "System", StringComparison.OrdinalIgnoreCase) == 0)
.Select(a => MetadataReference.CreateFromFile(a.Location)).Single())
.AddDocument(documentId, "MyFile.cs", source);
var document = solution.GetDocument(documentId);
// Format the document.
document = Formatter.FormatAsync(document).Result;
Assert.Equal(@"using System.Diagnostics;
using System;
using System.IO;
namespace NS
{
public class C
{
}
}
class Program
{
public static void Main()
{
System.Int32 i = 0; System.Console.WriteLine(i.ToString());
Process p = Process.GetCurrentProcess();
Console.WriteLine(p.Id);
}
}", document.GetSyntaxRootAsync().Result.ToString());
// Simplify names used in the document i.e. remove unnecessary namespace qualifiers.
var newRoot = (SyntaxNode)document.GetSyntaxRootAsync().Result;
newRoot = new SimplifyNamesAnnotionRewriter().Visit(newRoot);
document = document.WithSyntaxRoot(newRoot);
document = Simplifier.ReduceAsync(document).Result;
Assert.Equal(@"using System.Diagnostics;
using System;
using System.IO;
namespace NS
{
public class C
{
}
}
class Program
{
public static void Main()
{
int i = 0; Console.WriteLine(i.ToString());
Process p = Process.GetCurrentProcess();
Console.WriteLine(p.Id);
}
}", document.GetSyntaxRootAsync().Result.ToString());
}
示例7: GetTypeForExpressions
public void GetTypeForExpressions()
{
var source = @"
using System;
class Program
{
public void M(short[] s)
{
var d = 1.0;
Console.WriteLine(s[0] + d);
}
public static void Main()
{
}
}";
ProjectId projectId = ProjectId.CreateNewId();
DocumentId documentId = DocumentId.CreateNewId(projectId);
var solution = new AdhocWorkspace().CurrentSolution
.AddProject(projectId, "MyProject", "MyProject", LanguageNames.CSharp)
.AddMetadataReference(projectId, Mscorlib)
.AddDocument(documentId, "MyFile.cs", source);
var document = solution.GetDocument(documentId);
var model = (SemanticModel)document.GetSemanticModelAsync().Result;
// Get BinaryExpressionSyntax corresponding to the expression 's[0] + d' above.
BinaryExpressionSyntax addExpression = document.GetSyntaxRootAsync().Result
.DescendantNodes().OfType<BinaryExpressionSyntax>().Single();
// Get TypeSymbol corresponding to expression 's[0] + d' above.
TypeInfo expressionTypeInfo = model.GetTypeInfo(addExpression);
var expressionType = expressionTypeInfo.Type;
Assert.Equal(SpecialType.System_Double, expressionType.SpecialType);
Assert.Equal("double", expressionType.ToDisplayString());
Assert.Equal(SpecialType.System_Double, expressionTypeInfo.ConvertedType.SpecialType);
var conversion = model.GetConversion(addExpression);
Assert.True(conversion.IsIdentity);
// Get IdentifierNameSyntax corresponding to the variable 'd' in expression 's[0] + d' above.
var identifier = (IdentifierNameSyntax)addExpression.Right;
// Use GetTypeInfo() to get TypeSymbol corresponding to variable 'd' above.
TypeInfo variableTypeInfo = model.GetTypeInfo(identifier);
var variableType = variableTypeInfo.Type;
Assert.Equal(SpecialType.System_Double, variableType.SpecialType);
Assert.Equal("double", variableType.ToDisplayString());
Assert.Equal(SpecialType.System_Double, variableTypeInfo.ConvertedType.SpecialType);
conversion = model.GetConversion(identifier);
Assert.True(conversion.IsIdentity);
// Alternately, use GetSymbolInfo() to get TypeSymbol corresponding to variable 'd' above.
variableType = ((ILocalSymbol)model.GetSymbolInfo(identifier).Symbol).Type;
Assert.Equal(SpecialType.System_Double, variableType.SpecialType);
Assert.Equal("double", variableType.ToDisplayString());
// Get ElementAccessExpressionSyntax corresponding to 's[0]' in expression 's[0] + d' above.
var elementAccess = (ElementAccessExpressionSyntax)addExpression.Left;
// Use GetTypeInfo() to get TypeSymbol corresponding to 's[0]' above.
expressionTypeInfo = model.GetTypeInfo(elementAccess);
expressionType = expressionTypeInfo.Type;
Assert.Equal(SpecialType.System_Int16, expressionType.SpecialType);
Assert.Equal("short", expressionType.ToDisplayString());
Assert.Equal(SpecialType.System_Double, expressionTypeInfo.ConvertedType.SpecialType);
conversion = model.GetConversion(elementAccess);
Assert.True(conversion.IsImplicit && conversion.IsNumeric);
// Get IdentifierNameSyntax corresponding to the parameter 's' in expression 's[0] + d' above.
identifier = (IdentifierNameSyntax)elementAccess.Expression;
// Use GetTypeInfo() to get TypeSymbol corresponding to parameter 's' above.
variableTypeInfo = model.GetTypeInfo(identifier);
variableType = variableTypeInfo.Type;
Assert.Equal("short[]", variableType.ToDisplayString());
Assert.Equal("short[]", variableTypeInfo.ConvertedType.ToDisplayString());
conversion = model.GetConversion(identifier);
Assert.True(conversion.IsIdentity);
// Alternately, use GetSymbolInfo() to get TypeSymbol corresponding to parameter 's' above.
variableType = ((IParameterSymbol)model.GetSymbolInfo(identifier).Symbol).Type;
Assert.Equal("short[]", variableType.ToDisplayString());
Assert.Equal(SpecialType.System_Int16, ((IArrayTypeSymbol)variableType).ElementType.SpecialType);
}
示例8: GetFullyQualifiedName
public void GetFullyQualifiedName()
{
var source = @"
using System;
using Alias=NS.C<int>;
namespace NS
{
public class C<T>
{
public struct S<U>
{
}
}
}
class Program
{
public static void Main()
{
Alias.S<long> s = new Alias.S<long>(); Console.WriteLine(s.ToString());
}
}";
var projectId = ProjectId.CreateNewId();
var documentId = DocumentId.CreateNewId(projectId);
var solution = new AdhocWorkspace().CurrentSolution
.AddProject(projectId, "MyProject", "MyProject", LanguageNames.CSharp)
.AddMetadataReference(projectId, Mscorlib)
.AddDocument(documentId, "MyFile.cs", source);
var document = solution.GetDocument(documentId);
var root = document.GetSyntaxRootAsync().Result;
var model = (SemanticModel)document.GetSemanticModelAsync().Result;
// Get StructDeclarationSyntax corresponding to 'struct S' above.
StructDeclarationSyntax structDeclaration = root.DescendantNodes()
.OfType<StructDeclarationSyntax>().Single();
// Get TypeSymbol corresponding to 'struct S' above.
var structType = model.GetDeclaredSymbol(structDeclaration);
// Use ToDisplayString() to get fully qualified name.
Assert.Equal("NS.C<T>.S<U>", structType.ToDisplayString());
Assert.Equal("global::NS.C<T>.S<U>", structType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
// Get VariableDeclaratorSyntax corresponding to 'Alias.S<long> s = ...' above.
VariableDeclaratorSyntax variableDeclarator = root.DescendantNodes()
.OfType<VariableDeclaratorSyntax>().Single();
// Get TypeSymbol corresponding to above VariableDeclaratorSyntax.
var variableType = ((ILocalSymbol)model.GetDeclaredSymbol(variableDeclarator)).Type;
Assert.False(variableType.Equals(structType)); // Type of variable is a closed generic type while that of the struct is an open generic type.
Assert.True(variableType.OriginalDefinition.Equals(structType)); // OriginalDefinition for a closed generic type points to corresponding open generic type.
Assert.Equal("NS.C<int>.S<long>", variableType.ToDisplayString());
Assert.Equal("global::NS.C<int>.S<long>", variableType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
}