-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRoslyn-ScriptingWithDynamiclyTypeResult.linq
More file actions
133 lines (118 loc) · 4.66 KB
/
Roslyn-ScriptingWithDynamiclyTypeResult.linq
File metadata and controls
133 lines (118 loc) · 4.66 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<Query Kind="Program">
<NuGetReference>Roslyn</NuGetReference>
<NuGetReference>Roslyn.Compilers</NuGetReference>
<NuGetReference>Roslyn.Compilers.Common</NuGetReference>
<NuGetReference>Roslyn.Compilers.CSharp</NuGetReference>
<NuGetReference>Roslyn.Services.Common</NuGetReference>
<NuGetReference>Roslyn.Services.CSharp</NuGetReference>
<Namespace>Microsoft.CSharp.RuntimeHelpers</Namespace>
<Namespace>Microsoft.Runtime.Hosting</Namespace>
<Namespace>Microsoft.Runtime.Hosting.Interop</Namespace>
<Namespace>Roslyn.Compilers</Namespace>
<Namespace>Roslyn.Compilers.Common</Namespace>
<Namespace>Roslyn.Compilers.Compilation</Namespace>
<Namespace>Roslyn.Compilers.CSharp</Namespace>
<Namespace>Roslyn.Scripting</Namespace>
<Namespace>Roslyn.Scripting.CSharp</Namespace>
<Namespace>Roslyn.Services</Namespace>
<Namespace>Roslyn.Services.Classification</Namespace>
<Namespace>Roslyn.Services.CodeCleanup</Namespace>
<Namespace>Roslyn.Services.CodeGeneration</Namespace>
<Namespace>Roslyn.Services.Completion</Namespace>
<Namespace>Roslyn.Services.CSharp.Classification</Namespace>
<Namespace>Roslyn.Services.FindReferences</Namespace>
<Namespace>Roslyn.Services.Formatting</Namespace>
<Namespace>Roslyn.Services.Host</Namespace>
<Namespace>Roslyn.Services.Interactive</Namespace>
<Namespace>Roslyn.Services.MetadataAsSource</Namespace>
<Namespace>Roslyn.Services.Organizing</Namespace>
<Namespace>Roslyn.Services.Shared.Extensions</Namespace>
<Namespace>Roslyn.Utilities</Namespace>
<Namespace>System</Namespace>
<Namespace>System.Collections.Generic</Namespace>
<Namespace>System.Dynamic</Namespace>
<Namespace>System.IO</Namespace>
<Namespace>System.Linq</Namespace>
<Namespace>System.Reflection</Namespace>
<Namespace>System.Text</Namespace>
<IncludePredicateBuilder>true</IncludePredicateBuilder>
</Query>
void Main()
{
// generate input data
IEnumerable<Model.ProcessingModel> models =
Enumerable.Range(0, 1000000)
.Select(n => new Model.ProcessingModel { InputA = n, InputB = n * 0.5M, Factor = 0.050M });
var sw = Stopwatch.StartNew();
// create scriot engine
var engine = new ScriptEngine();
// add references
new[]
{
typeof (Math).Assembly,
this.GetType().Assembly
}.ToList().ForEach(assembly => engine.AddReference(assembly));
// import assemblies
new[]
{
"System", "System.Math",
typeof(Model.ProcessingModel).Namespace
} .ToList().ForEach(@namespace => engine.ImportNamespace(@namespace));
// limitations of script:
// * no dynamic allowed
// * no async/await allowed
var script =
@"
Result = InputA + InputB * Factor;
Delta = Math.Abs((Result ?? 0M) - InputA);
Description = ""Some description"";
new Model.ReportModel { Σ = Result, Δ = Delta, λ = Description }
";
// create submission
var submissionModel = new Model.ProcessingModel();
var session = engine.CreateSession(submissionModel);
var submission = session.CompileSubmission<dynamic>(script);
// process input data
IEnumerable<dynamic> results =
models
.Select(model =>
{
submissionModel.InputA = model.InputA;
submissionModel.InputB = model.InputB;
submissionModel.Factor = model.Factor;
return submission.Execute();
})
.ToList();
sw.Stop();
string.Format("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds).Dump();
// merge results
results
.Zip(models, (result, model) => new { result, model })
.Select(@group =>
new
{
@return = @group.result,
ResultModel = @group.model
})
.Take(10)
.Dump();
}
}
namespace Model
{
public class ProcessingModel
{
public decimal InputA { get; set; }
public decimal InputB { get; set; }
public decimal Factor { get; set; }
public decimal? Result { get; set; }
public decimal? Delta { get; set; }
public string Description { get; set; }
public decimal? Addition { get; set; }
}
public class ReportModel
{
public decimal? Σ { get; set; }
public decimal? Δ { get; set; }
public string λ { get; set; }
}