Repository navigation
Expand file tree
/
Copy pathScriptVariable.cs
More file actions
72 lines (60 loc) · 2.24 KB
/
Copy pathScriptVariable.cs
File metadata and controls
72 lines (60 loc) · 2.24 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
// 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.Diagnostics;
using System.Reflection;
namespace Microsoft.CodeAnalysis.Scripting
{
/// <summary>
/// A variable declared by the script.
/// </summary>
[DebuggerDisplay("{GetDebuggerDisplay(), nq}")]
public sealed class ScriptVariable
{
private readonly object _instance;
private readonly FieldInfo _field;
internal ScriptVariable(object instance, FieldInfo field)
{
Debug.Assert(instance != null);
Debug.Assert(field != null);
_instance = instance;
_field = field;
}
/// <summary>
/// The name of the variable.
/// </summary>
public string Name => _field.Name;
/// <summary>
/// The type of the variable.
/// </summary>
public Type Type => _field.FieldType;
/// <summary>
/// True if the variable can't be written to (it's declared as readonly or a constant).
/// </summary>
public bool IsReadOnly => _field.IsInitOnly || _field.IsLiteral;
/// <summary>
/// The value of the variable after running the script.
/// </summary>
/// <exception cref="InvalidOperationException">Variable is read-only or a constant.</exception>
/// <exception cref="ArgumentException">The type of the specified <paramref name="value"/> isn't assignable to the type of the variable.</exception>
public object Value
{
get
{
return _field.GetValue(_instance);
}
set
{
if (_field.IsInitOnly)
{
throw new InvalidOperationException(ScriptingResources.CannotSetReadOnlyVariable);
}
if (_field.IsLiteral)
{
throw new InvalidOperationException(ScriptingResources.CannotSetConstantVariable);
}
_field.SetValue(_instance, value);
}
}
private string GetDebuggerDisplay() => $"{Name}: {Value ?? "<null>"}";
}
}