Repository navigation
Expand file tree
/
Copy pathScriptVariables.cs
More file actions
109 lines (96 loc) · 3.26 KB
/
Copy pathScriptVariables.cs
File metadata and controls
109 lines (96 loc) · 3.26 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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.CodeAnalysis;
namespace Microsoft.CodeAnalysis.Scripting
{
/// <summary>
/// A collection that holds the final state of all global variables used by the script.
/// </summary>
public class ScriptVariables : IEnumerable<ScriptVariable>, IEnumerable
{
private readonly Dictionary<string, ScriptVariable> _map;
internal ScriptVariables(ScriptExecutionState executionState)
{
_map = CreateVariableMap(executionState);
}
public int Count
{
get { return _map.Count; }
}
/// <summary>
/// Returns the global variable with the specified name.
/// </summary>
public ScriptVariable this[string name]
{
get
{
ScriptVariable global;
if (_map.TryGetValue(name, out global))
{
return global;
}
else
{
return null;
}
}
}
/// <summary>
/// Determines if a global variable with the specified name exists.
/// </summary>
public bool ContainsVariable(string name)
{
return _map.ContainsKey(name);
}
/// <summary>
/// A list the global variable names.
/// </summary>
public IEnumerable<String> Names
{
get { return _map.Keys; }
}
/// <summary>
/// Gets an enumerator over all the variables.
/// </summary>
public IEnumerator<ScriptVariable> GetEnumerator()
{
return _map.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
private static Dictionary<string, ScriptVariable> CreateVariableMap(ScriptExecutionState executionState)
{
var map = new Dictionary<string, ScriptVariable>();
for (int i = 0; i < executionState.Count; i++)
{
var state = executionState[i];
if (state != null)
{
AddVaraibles(map, state);
}
}
return map;
}
private static void AddVaraibles(Dictionary<string, ScriptVariable> map, object instance)
{
var members = instance.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (var member in members.Where(m => m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property))
{
if (member.Name.Length > 0 && Char.IsLetterOrDigit(member.Name[0])
&& !map.ContainsKey(member.Name))
{
map.Add(member.Name, new ScriptVariable(instance, member));
}
}
}
}
}