-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractExpressionExtensions.cs
More file actions
40 lines (36 loc) · 1.16 KB
/
AbstractExpressionExtensions.cs
File metadata and controls
40 lines (36 loc) · 1.16 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
using MetaPrograms.Expressions;
using Microsoft.CodeAnalysis;
namespace MetaPrograms.CSharp
{
public static class AbstractExpressionExtensions
{
public static object GetConstantValueOrNull(this AbstractExpression expression)
{
if (TryGetConstantValue(expression, out var value))
{
return value;
}
return null;
}
public static bool TryGetConstantValue(this AbstractExpression expression, out object value)
{
if (expression is ConstantExpression constant)
{
if (constant.Value is Optional<object>) //TODO: can safely remove this case?
{
var optional = (Optional<object>) constant.Value;
var copyOfValue = optional.HasValue ? optional.Value : null;
value = copyOfValue;
return optional.HasValue;
}
else
{
value = constant.Value;
return (value != null);
}
}
value = null;
return false;
}
}
}