I have a window with a TextBox (expr) and a TextBlock (output).
When I add StringList class as global script item, evaluateexpression always returns null, even if the StringList isn’t referenced in the expression, eg. 2+2.
Also it seems like EvaluateExpression doesn’t understand Linq, eg:
_data.Items.Where(i => i.Name == “B”).First().Name
Is this correct or am I doing something wrong?
Are there any sort of documentation of what EvaluateExpression understands?
Code example:
public partial class MainWindow : Window
{
private DataClass _data = new DataClass();
private ScriptRun _scriptRun = new ScriptRun();
private StringList _strings = new StringList();
public MainWindow()
{
InitializeComponent();
_scriptRun.ScriptSource.WithDefaultReferences();
_scriptRun.AssemblyKind = ScriptAssemblyKind.DynamicLibrary;
_scriptRun.ScriptSource.References.Add("PresentationCore");
_scriptRun.ScriptSource.References.Add("WindowsBase");
_strings.Add("a");
_strings.Add("b");
_strings.Add("c");
var sgi = new ScriptGlobalItem("_data", typeof(DataClass), _data);
_scriptRun.GlobalItems.Add(sgi);
//EvaluateExpression always returns null (even 2+2) when this ScriptGlobalItem is added
sgi = new ScriptGlobalItem("_strings", typeof(StringList), _strings);
_scriptRun.GlobalItems.Add(sgi);
}
private void expr_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Enter:
var res = _scriptRun.EvaluateExpression(this.expr.Text);
this.output.Text = res?.ToString() ?? "null";
break;
default:
break;
}
}
}
class StringList : List<string>
{
public string GetString(int i) => this[i];
}
public class DataItem
{
public string Name { get; set; }
}
public class DataClass
{
public List<DataItem> Items { get; set; } = new List<DataItem>();
public string Name { get; set; } = "Jazz";
public string Description { get; set; } = "funk";
public int Value { get; set; } = 5;
public int GetValue(int i) => Value * i;
public DataClass()
{
Items.Add(new() { Name = "A" });
Items.Add(new() { Name = "B" });
Items.Add(new() { Name = "C" });
}
}