forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoslynReplEngine.cs
More file actions
76 lines (65 loc) · 3.05 KB
/
RoslynReplEngine.cs
File metadata and controls
76 lines (65 loc) · 3.05 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
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using Common.Logging;
using Roslyn.Scripting;
using ScriptCs.Contracts;
namespace ScriptCs.Engine.Roslyn
{
using System.Globalization;
public class RoslynReplEngine : RoslynScriptEngine, IReplEngine
{
public RoslynReplEngine(IScriptHostFactory scriptHostFactory, ILog logger)
: base(scriptHostFactory, logger)
{
}
public ICollection<string> GetLocalVariables(ScriptPackSession scriptPackSession)
{
var variables = new Collection<string>();
if (scriptPackSession != null && scriptPackSession.State.ContainsKey(SessionKey))
{
var sessionState = (SessionState<Session>)scriptPackSession.State[SessionKey];
var submissionObjectField = sessionState.Session.GetType()
.GetField("submissions", BindingFlags.Instance | BindingFlags.NonPublic);
if (submissionObjectField != null)
{
var submissionObjectFieldValue = submissionObjectField.GetValue(sessionState.Session);
if (submissionObjectFieldValue != null)
{
var submissionObjects = submissionObjectFieldValue as object[];
if (submissionObjects != null && submissionObjects.Any(x => x != null))
{
var processedFields = new Collection<string>();
// reversing to get the latest submission first
foreach (var submissionObject in submissionObjects.Where(x => x != null).Reverse())
{
foreach (var field in submissionObject.GetType().GetFields()
.Where(x => x.Name.ToLowerInvariant() != "<host-object>")
.Where(field => !processedFields.Contains(field.Name)))
{
var variable = string.Format(
CultureInfo.InvariantCulture,
"{0} {1} = {2}",
field.FieldType,
field.Name,
field.GetValue(submissionObject));
variables.Add(variable);
processedFields.Add(field.Name);
}
}
}
}
}
}
return variables;
}
protected override ScriptResult Execute(string code, Session session)
{
Guard.AgainstNullArgument("session", session);
return string.IsNullOrWhiteSpace(FileName) && !IsCompleteSubmission(code)
? ScriptResult.Incomplete
: base.Execute(code, session);
}
}
}