Skip to content

Instantly share code, notes, and snippets.

View joymon's full-sized avatar

Joy George Kunjikkuru joymon

View GitHub Profile
@joymon
joymon / Roslyn Compilation 01
Last active August 29, 2015 14:21
Roslyn code snippet for compiling C# code in console application
public void TestRoslynCompilation()
{
Compilation compilation = GetSimpleCompilation();
ImmutableArray<Diagnostic> diagnostics = Compile(compilation);
ShowDiagnostics(diagnostics);
}
@joymon
joymon / C# Function Call Without Parentheses
Last active August 29, 2015 14:21
Demo of function call without parentheses in C#
string name = "JOY";
if(name.Any(char.IsLower))
{
Console.WriteLine("Word '{0}' has lower letter(s)",name);
}
else
{
Console.WriteLine("Word '{0}' does not have lower letters",name);
}
@joymon
joymon / MyWebAPIController
Created May 21, 2015 19:21
A simple web api controller for testing purpose
using System.Web.Http;
public class MyWebAPIController : ApiController
{
[AcceptVerbs("GET")]
public string Help()
{
return "This is test WEB API. Supported methods are ../api/MyWebAPI/Help, ../api/MyWebAPI/Square/{number}";
}
[AcceptVerbs("GET")]
public int Square(int id)
@joymon
joymon / WebAPI_HttpSelfHostServer_Test
Last active August 29, 2015 14:21
Sample code to show how WebApi can be hosted in console using HttpSelfHostServer
using System.Web.Http;
using System.Web.Http.SelfHost;
public class WebAPI_HttpSelfHostServer_Test
{
/// <summary>
/// Exposed to outside to starts the server.
/// </summary>
/// <remarks>Simple function call into server class.start method</remarks>
public void StartServer()
{
@joymon
joymon / WebAPI_OWINSelfHostServer_Test
Last active August 29, 2015 14:21
Sample to show how WebApi can be hosted in console application using OWIN implemenation
public class WebAPI_OWINSelfHostServer_Test
{
/// <summary>
/// Exposed to starts server using WebApp class which uses OWIN specs.
/// </summary>
public void StartServer()
{
WebApp.Start<MyOWINServer>("http://localhost:8080");
Console.WriteLine("OWIN - Press Enter to quit...");
Console.ReadLine();
private Compilation GetSimpleCompilation()
{
string source = GetSourceCode();
SyntaxTree tree = GetSyntaxTree(source);
IEnumerable<MetadataReference> referecnes = GetReferences();
return CSharpCompilation.Create("TestRoslynOutput",
syntaxTrees: new[] { tree },
references: referecnes,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
private string GetSourceCode()
{
return @"
using System;
namespace RoslynCompileSample
{
public class writer
{
public void Write(string message)
private SyntaxTree GetSyntaxTree(string source)
{
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);
return syntaxTree;
}
private IEnumerable<MetadataReference> GetReferences()
{
return new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
};
}
private ImmutableArray<Diagnostic> Compile(Compilation compilation)
{
string outputDllFilePathName = GetOutputFilePath();
EmitResult result = compilation.Emit(outputDllFilePathName);
Console.WriteLine(" Compilation result = {0}", result.Success);
return result.Diagnostics;
}