-
Notifications
You must be signed in to change notification settings - Fork 0
Code Snippets
Sandesh Kota edited this page Mar 18, 2019
·
13 revisions
var start = System.Environment.TickCount;
// code
var stop = System.Environment.TickCount;
var elapsedTimeInSeconds = (stop - start) / 1000.0;
[TestMethod]
[ExpectedException(typeof(ArgumentException), "A userId of null was inappropriately allowed.")]
public void NullUserIdInConstructor()
{
LogonInfo logonInfo = new LogonInfo(null, "P@ss0word");
}
- Nuget package Microsoft.CodeAnalysis.CSharp
\\ A code.cs file has class "Greeter" and method "SayHello"
var code = File.readAllText("code.cs");
var tree = SyntaxFactory.ParseSyntaxTree(code);
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var reference = new MetadataFileReference(typeof(object).Assembly.Location)
var compilation = CSharpCompilation.Create("test").WithOptions(options).AddSyntaxTrees(tree).AddReferences(reference);
var diagnostics = compilation.GetDiagnostics();
foreach (var diagnostic in diagnostics )
{
System.Console.WriteLine(diagnostic.ToString());
}
using stream(var stream = new MemoryStream())
{
compilation.Emit(stream);
var assembly = Assembly.Load(stream.GetBuffer());
var type = assembly.GetType("Greeter");
var greeter = Activator.CreateInstance(type);
var method = type.GetMethod("SayHello");
method.Invoke(greeter, null);
}