本文整理汇总了C#中Microsoft.CodeAnalysis.Project.AddDocument方法的典型用法代码示例。如果您正苦于以下问题:C# Project.AddDocument方法的具体用法?C# Project.AddDocument怎么用?C# Project.AddDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.Project
的用法示例。
在下文中一共展示了Project.AddDocument方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CSharpCompilerModule
/// <summary>
/// Initializes a new instance of the <see cref="CsvCompilerModule"/> class.
/// </summary>
public CSharpCompilerModule(Bootstrapper bootstrapper)
: base(bootstrapper)
{
compiler = new CSharpCompiler();
workspace = new AdhocWorkspace();
project = workspace.AddProject("CSharpProject", LanguageNames.CSharp);
document = project.AddDocument("current", "");
context = new CompilationContext(workspace, document);
}
示例2: GetDocumentFromMetadata
public static Task<Document> GetDocumentFromMetadata(Project project, ISymbol symbol, CancellationToken cancellationToken = new CancellationToken())
{
var filePath = GetFilePathForSymbol(project, symbol);
var topLevelSymbol = GetTopLevelContainingNamedType(symbol);
var temporaryDocument = project.AddDocument(filePath, string.Empty);
object service = Activator.CreateInstance(_CSharpMetadataAsSourceService.Value, new object[] { temporaryDocument.Project.LanguageServices });
var method = _CSharpMetadataAsSourceService.Value.GetMethod("AddSourceToAsync");
return (Task<Document>)method.Invoke(service, new object[] { temporaryDocument, topLevelSymbol, cancellationToken });
}
示例3: AddPathToProject
private static void AddPathToProject(ref Solution solution, ref Project project, string fileName, string contents)
{
var document = GetExistingDocument(project, fileName);
if (document != null)
{
project = project.RemoveDocument(document.Id);
solution = project.Solution;
}
Console.WriteLine($"\t - Adding {fileName} to project");
document = project.AddDocument(fileName,
contents,
null,
Path.Combine(Path.GetDirectoryName(project.FilePath), fileName));
project = document.Project;
solution = project.Solution;
}
示例4: CompileHandlebarsFiles
private static Workspace CompileHandlebarsFiles(Project project, Workspace workspace, List<string> hbsFiles, CompilerOptions options)
{
bool successFullCompilation = true;
while(hbsFiles.Any() && successFullCompilation)
{
successFullCompilation = false;
var nextRound = new List<string>();
foreach(var file in hbsFiles)
{
var fileInfo = new FileInfo(file);
string @namespace;
bool compiledVersionExists = File.Exists($"{file}.cs");
bool compiledVersionIsOlder = true;
if (compiledVersionExists)
{//Compiled Version already exists
var compiledFileInfo = new FileInfo($"{file}.cs");
compiledVersionIsOlder = (fileInfo.LastWriteTimeUtc > compiledFileInfo.LastWriteTimeUtc);
@namespace = DetermineNamespace(compiledFileInfo);
} else
{
@namespace = DetermineNamespace(fileInfo, project);
}
if (compiledVersionIsOlder || options.ForceRecompilation)
{
string content = File.ReadAllText(file);
string name = Path.GetFileNameWithoutExtension(file);
var compilationResult = CompileHandlebarsTemplate(content, @namespace, name, project, options);
if (!options.DryRun)
{
if (compilationResult?.Item2?.Any() ?? false)
{//Errors occured
if (compilationResult.Item2.OfType<HandlebarsTypeError>().Any(x => x.Kind == HandlebarsTypeErrorKind.UnknownPartial))
{//Unresolvable Partial... could be due to compiling sequence
//Console.WriteLine($"Unresolved partial call for template '{name}'. Try again!");
nextRound.Add(file);
}
else
foreach (var error in compilationResult.Item2)
PrintError(error);
}
else
{
successFullCompilation = true;
//Check if template already exits
var doc = project.Documents.FirstOrDefault(x => x.Name.Equals(string.Concat(name, ".hbs.cs")));
if (doc != null)
{//And change it if it does
project = doc.WithSyntaxRoot(CSharpSyntaxTree.ParseText(SourceText.From(compilationResult.Item1)).GetRoot()).Project;
}
else
{//Otherwise add a new document
project = project.AddDocument(string.Concat(name, ".hbs.cs"), SourceText.From(compilationResult.Item1), GetFolderStructureForFile(fileInfo, project)).Project;
}
try {
workspace.TryApplyChanges(project.Solution);
project = workspace.CurrentSolution.Projects.First(x => x.Id.Equals(project.Id));
} catch(NotSupportedException)
{//ProjectJsonWorkspace does not support adding documents (as of 2016-02-17). So just add it manually
File.WriteAllText($"{file}.cs", compilationResult.Item1);
}
}
}
}
}
hbsFiles = nextRound;
}
return workspace;
}
示例5: MoveTypeNodes
private static Project MoveTypeNodes(SemanticModel model, ImmutableArray<TypeToRemove> typesToRemove,
Func<string, string> typeFolderGenerator, Project project, CancellationToken token)
{
var projectName = project.Name;
foreach (var typeToRemove in typesToRemove)
{
token.ThrowIfCancellationRequested();
var fileName = $"{typeToRemove.Symbol.Name}.cs";
var containingNamespace = typeToRemove.Symbol.GetContainingNamespace();
var typeFolder = typeFolderGenerator(containingNamespace).Replace(
projectName, string.Empty);
if (typeFolder.StartsWith("\\"))
{
typeFolder = typeFolder.Remove(0, 1);
}
project = project.AddDocument(fileName,
typeToRemove.Declaration.GetCompilationUnitForType(model, containingNamespace),
folders: !string.IsNullOrWhiteSpace(typeFolder) ?
new[] { typeFolder } : null).Project;
}
return project;
}
示例6: AddDocument
private static Project AddDocument(
Project project,
KeyValuePair<INamedTypeSymbol, string> symbolAndText,
HashSet<string> existingFileNames)
{
var symbol = symbolAndText.Key;
var text = symbolAndText.Value;
var sanitizedTypeName = Paths.SanitizeFileName(symbol.Name);
if (symbol.IsGenericType)
{
sanitizedTypeName = sanitizedTypeName + "`" + symbol.TypeParameters.Length;
}
var fileName = sanitizedTypeName + ".cs";
var folders = GetFolderChain(symbol);
if (folders == null)
{
// There was an unutterable namespace name - abort the entire document
return null;
}
var foldersString = string.Join(".", folders ?? Enumerable.Empty<string>());
var fileNameAndFolders = foldersString + fileName;
int index = 1;
while (!existingFileNames.Add(fileNameAndFolders))
{
fileName = sanitizedTypeName + index + ".cs";
fileNameAndFolders = foldersString + fileName;
index++;
}
project = project.AddDocument(fileName, text, folders, fileName).Project;
return project;
}
示例7: AddDocument
protected override Project AddDocument(Project fromProject)
=> fromProject.AddDocument(DocumentInfo.Name, Text, DocumentInfo.Folders, DocumentInfo.FilePath).Project;