This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void ShowDiagnostics(ImmutableArray<Diagnostic> diagnostics) | |
{ | |
foreach (Diagnostic diag in diagnostics) | |
{ | |
Console.WriteLine(diag); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async void TestRoslynCompilationWithAnalyzers() | |
{ | |
Compilation compilation = GetSimpleCompilation(); | |
CompilationWithAnalyzers compilationWithAnalyzers = GetAnalyzerAwareCompilation(compilation); | |
ImmutableArray<Diagnostic> diagnosticResults = await compilationWithAnalyzers.GetAllDiagnosticsAsync(); | |
if (diagnosticResults.Length == 0) | |
{ | |
ImmutableArray<Diagnostic> diagnostics = Compile(compilationWithAnalyzers.Compilation); | |
ShowDiagnostics(diagnostics); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private CompilationWithAnalyzers GetAnalyzerAwareCompilation(Compilation compilation) | |
{ | |
ImmutableArray<DiagnosticAnalyzer> analyzers = ImmutableArray.Create<DiagnosticAnalyzer>(new TypeNameShouldStartWithCapitalLetterDiagnosticAnalyzer()); | |
CompilationWithAnalyzers compilationWithAnalyzers = new CompilationWithAnalyzers(compilation, analyzers, | |
GetAnalyzerOptions(), | |
new CancellationToken()); | |
return compilationWithAnalyzers; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CodeAnalysis; | |
using Microsoft.CodeAnalysis.Diagnostics; | |
using System.Collections.Immutable; | |
using System.Linq; | |
namespace JoymonsCode | |
{ | |
public class TypeNameShouldStartWithCapitalLetterDiagnosticAnalyzer : DiagnosticAnalyzer | |
{ | |
internal const string DiagnosticId = "TypeNameShouldStartWithCapitalLetter"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static int FindFactorialWithSimulatedDelay(int no) | |
{ | |
int result = 1; | |
for (int i = 1; i <= no; i++) | |
{ | |
Thread.Sleep(500); | |
result = result * i; | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void WriteFactorial(int no) | |
{ | |
int result = FindFactorialWithSimulatedDelay(no); | |
Console.WriteLine("Factorial of {0} is {1}", no, result); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Main() | |
{ | |
for (int counter = 1; counter < 5; counter++) | |
{ | |
if (counter % 3 == 0) | |
{ | |
WriteFactorial(counter); | |
} | |
else | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void WriteFactorialAsyncUsingDelegate(int facno) | |
{ | |
Func<int, int> findFact = FindFactorialWithSimulatedDelay; | |
findFact.BeginInvoke(facno, | |
(iAsyncresult) => | |
{ | |
AsyncResult asyncResult = iAsyncresult as AsyncResult; | |
Func<int, int> del = asyncResult.AsyncDelegate as Func<int, int>; | |
int factorial = del.EndInvoke(asyncResult); | |
Console.WriteLine("Factorial of {0} is {1}", facno, factorial); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Main() | |
{ | |
for (int counter = 1; counter < 5; counter++) | |
{ | |
if (counter % 3 == 0) | |
{ | |
WriteFactorialAsyncUsingDelegate(counter); | |
} | |
else | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void WriteFactorialAsyncUsingTask(int no) | |
{ | |
Task<int> task=Task.Run<int>(() => | |
{ | |
int result = FindFactorialWithSimulatedDelay(no); | |
return result; | |
}); | |
task.ContinueWith(new Action<Task<int>>((input) => | |
{ | |
Console.WriteLine("Factorial of {0} is {1}", no, input.Result); |