This repository has been archived by the owner on Dec 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
/
HelloMetaProgramming.cs
74 lines (62 loc) · 1.73 KB
/
HelloMetaProgramming.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Framework.Runtime;
namespace HelloWorld.Compiler.Preprocess
{
public class HelloMetaProgramming : ICompileModule
{
public HelloMetaProgramming(IServiceProvider services)
{
}
public void BeforeCompile(IBeforeCompileContext context)
{
var options = CSharpParseOptions.Default
.WithLanguageVersion(context.CSharpCompilation.LanguageVersion);
context.CSharpCompilation = context.CSharpCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(@"
public class Foo
{
public string Message
{
get { return ""Metaprogrammg!""; }
}
}", options));
}
public void AfterCompile(IAfterCompileContext context)
{
}
}
}
namespace Microsoft.Framework.Runtime
{
[AssemblyNeutral]
public class AssemblyNeutralAttribute : Attribute
{
}
/// <summary>
/// Summary description for ICompileModule
/// </summary>
[AssemblyNeutral]
public interface ICompileModule
{
void BeforeCompile(IBeforeCompileContext context);
void AfterCompile(IAfterCompileContext context);
}
[AssemblyNeutral]
public interface IBeforeCompileContext
{
CSharpCompilation CSharpCompilation { get; set; }
IList<ResourceDescription> Resources { get; }
IList<Diagnostic> Diagnostics { get; }
}
[AssemblyNeutral]
public interface IAfterCompileContext
{
CSharpCompilation CSharpCompilation { get; set; }
IList<Diagnostic> Diagnostics { get; }
}
}