本文整理汇总了C#中Compilation.WithOptions方法的典型用法代码示例。如果您正苦于以下问题:C# Compilation.WithOptions方法的具体用法?C# Compilation.WithOptions怎么用?C# Compilation.WithOptions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Compilation
的用法示例。
在下文中一共展示了Compilation.WithOptions方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDiagnostics
public IEnumerable<Diagnostic> GetDiagnostics(Compilation compilation)
{
if (diagnosticAnalyzers.IsDefaultOrEmpty)
{
return new Diagnostic[0];
}
var compilationOptions = compilation.Language == LanguageNames.CSharp
? (CompilationOptions)new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
: new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(
diagnosticAnalyzers.SelectMany(analyzer => analyzer.SupportedDiagnostics)
.Select(diagnostic =>
new KeyValuePair<string, ReportDiagnostic>(diagnostic.Id, ReportDiagnostic.Warn)));
var modifiedCompilation = compilation.WithOptions(compilationOptions);
using (var tokenSource = new CancellationTokenSource())
{
var compilationWithAnalyzer = modifiedCompilation.WithAnalyzers(diagnosticAnalyzers, null,
tokenSource.Token);
return compilationWithAnalyzer.GetAnalyzerDiagnosticsAsync().Result;
}
}
示例2: GetDiagnostics
private static IEnumerable<Diagnostic> GetDiagnostics(Compilation compilation,
DiagnosticAnalyzer diagnosticAnalyzer)
{
using (var tokenSource = new CancellationTokenSource())
{
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(
diagnosticAnalyzer.SupportedDiagnostics
.Select(diagnostic =>
new KeyValuePair<string, ReportDiagnostic>(diagnostic.Id, ReportDiagnostic.Warn)));
var compilationWithOptions = compilation.WithOptions(compilationOptions);
var compilationWithAnalyzer = compilationWithOptions
.WithAnalyzers(ImmutableArray.Create(diagnosticAnalyzer), null, tokenSource.Token);
return compilationWithAnalyzer.GetAnalyzerDiagnosticsAsync().Result;
}
}
示例3: EnableAnalyzer
private static Compilation EnableAnalyzer(DiagnosticAnalyzer analyzer, Compilation compilation)
{
return compilation
.WithOptions(
compilation
.Options
.WithSpecificDiagnosticOptions(
analyzer
.SupportedDiagnostics
.Select(x =>
KeyValuePair.Create(x.Id, ReportDiagnostic.Default))
.ToImmutableDictionaryOrEmpty()));
}
示例4: CompilationWithAnalyzers
private CompilationWithAnalyzers(Compilation compilation, ImmutableArray<DiagnosticAnalyzer> analyzers, CompilationWithAnalyzersOptions analysisOptions, CancellationToken cancellationToken)
{
VerifyArguments(compilation, analyzers, analysisOptions);
compilation = compilation
.WithOptions(compilation.Options.WithReportSuppressedDiagnostics(analysisOptions.ReportSuppressedDiagnostics))
.WithEventQueue(new AsyncQueue<CompilationEvent>());
_compilation = compilation;
_analyzers = analyzers;
_analysisOptions = analysisOptions;
_cancellationToken = cancellationToken;
_compilationData = new CompilationData(_compilation);
_analysisState = new AnalysisState(analyzers, _compilationData);
_analysisResult = new AnalysisResult(analysisOptions.LogAnalyzerExecutionTime, analyzers);
_driverPool = new ObjectPool<AnalyzerDriver>(() => _compilation.AnalyzerForLanguage(analyzers, AnalyzerManager.Instance));
_executingConcurrentTreeTasksOpt = analysisOptions.ConcurrentAnalysis ? new Dictionary<SyntaxTree, Tuple<Task, CancellationTokenSource>>() : null;
_concurrentTreeTaskTokensOpt = analysisOptions.ConcurrentAnalysis ? new Dictionary<Task, int>() : null;
_executingCompilationOrNonConcurrentTreeTask = null;
}
示例5: Compile
/// <summary>
/// Compiles S# <paramref name="project" /> and returns the bytes of the compiled assembly.
/// </summary>
/// <param name="project">The project that should be compiled.</param>
/// <param name="peBytes">Returns the compiled assembly.</param>
/// <param name="pdbBytes">The returns the compiled program database.</param>
public void Compile([NotNull] Project project, out byte[] peBytes, out byte[] pdbBytes)
{
Requires.NotNull(project, nameof(project));
Compilation = project.GetCompilationAsync().Result;
var diagnosticOptions = Compilation.Options.SpecificDiagnosticOptions.Add("CS0626", ReportDiagnostic.Suppress);
var options = (CSharpCompilationOptions)Compilation.Options;
options = options.WithAllowUnsafe(true).WithSpecificDiagnosticOptions(diagnosticOptions);
if (!Diagnose())
throw new CompilationException();
Compilation = Compilation.WithOptions(options);
Compilation = Normalizer.ApplyNormalizers(Compilation);
EmitInMemory(out peBytes, out pdbBytes);
}
示例6: Compile
/// <summary>
/// Compiles S# <paramref name="project" /> and returns the bytes of the compiled assembly.
/// </summary>
/// <param name="project">The project that should be compiled.</param>
/// <param name="peBytes">Returns the compiled assembly.</param>
/// <param name="pdbBytes">The returns the compiled program database.</param>
public void Compile([NotNull] Project project, out byte[] peBytes, out byte[] pdbBytes)
{
Requires.NotNull(project, () => project);
Compilation = project.GetCompilationAsync().Result;
var diagnosticOptions = Compilation.Options.SpecificDiagnosticOptions.Add("CS0626", ReportDiagnostic.Suppress);
var options = Compilation.Options.WithSpecificDiagnosticOptions(diagnosticOptions);
var syntaxGenerator = SyntaxGenerator.GetGenerator(project.Solution.Workspace, LanguageNames.CSharp);
if (!Diagnose())
throw new CompilationException();
Compilation = Compilation.WithOptions(options);
ApplyNormalizers(syntaxGenerator);
EmitInMemory(out peBytes, out pdbBytes);
}
示例7: GetAllDiagnostics
private static IEnumerable<Diagnostic> GetAllDiagnostics(Compilation compilation,
IEnumerable<DiagnosticAnalyzer> diagnosticAnalyzers)
{
using (var tokenSource = new CancellationTokenSource())
{
var compilationOptions = compilation.Language == LanguageNames.CSharp
? (CompilationOptions)new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
: new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var supportedDiagnostics = diagnosticAnalyzers
.SelectMany(analyzer => analyzer.SupportedDiagnostics)
.ToList();
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(
supportedDiagnostics
.Select(diagnostic =>
new KeyValuePair<string, ReportDiagnostic>(diagnostic.Id, ReportDiagnostic.Warn))
.Union(
new[]
{
new KeyValuePair<string, ReportDiagnostic>(AnalyzerFailedDiagnosticId, ReportDiagnostic.Error)
}));
var compilationWithOptions = compilation.WithOptions(compilationOptions);
var compilationWithAnalyzer = compilationWithOptions
.WithAnalyzers(
diagnosticAnalyzers.ToImmutableArray(),
cancellationToken: tokenSource.Token);
return compilationWithAnalyzer.GetAllDiagnosticsAsync().Result;
}
}
示例8: GetDiagnostics
internal static IEnumerable<Diagnostic> GetDiagnostics(Compilation compilation,
DiagnosticAnalyzer diagnosticAnalyzer)
{
using (var tokenSource = new CancellationTokenSource())
{
var compilationOptions = compilation.Language == LanguageNames.CSharp
? (CompilationOptions)new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
: new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(
diagnosticAnalyzer.SupportedDiagnostics
.Select(diagnostic =>
new KeyValuePair<string, ReportDiagnostic>(diagnostic.Id, ReportDiagnostic.Warn))
.Union(
new KeyValuePair<string, ReportDiagnostic>[]
{
new KeyValuePair<string, ReportDiagnostic>("AD0001", ReportDiagnostic.Error)
}));
var compilationWithOptions = compilation.WithOptions(compilationOptions);
var compilationWithAnalyzer = compilationWithOptions
.WithAnalyzers(
ImmutableArray.Create(diagnosticAnalyzer),
cancellationToken: tokenSource.Token);
var diagnostics = compilationWithAnalyzer.GetAllDiagnosticsAsync().Result;
diagnostics.Where(d => d.Id == "AD0001").Should().BeEmpty();
return diagnostics.Where(d => d.Id == diagnosticAnalyzer.SupportedDiagnostics.Single().Id);
}
}