Skip to content

dynamic/ExpandoObject string member can't be passed to a function that accepts a string #295

@AndrewKoransky

Description

@AndrewKoransky

If you add a variable to the interpreter that is a dynamic/ExpandoObject with a string member, then attempt to use that string member in a function, you get an exception:
DynamicExpresso.Exceptions.ParseException : Argument list incompatible with delegate expression (at index 0).

Here is a test that demonstrates the behavior.

var evaluator = new Interpreter();

// create path helper functions in expressions...
Func<string, string, string> pathCombine = (x, y) => System.IO.Path.Combine(x, y);
evaluator.SetFunction("PathCombine", pathCombine);

// add a GlobalSettings dynamic object...
dynamic globalSettings = new ExpandoObject();
globalSettings.MyTestPath = "C:\\delme\\";
evaluator.SetVariable("GlobalSettings", globalSettings);

// Here is a workaround that succeeds - you have to cast GlobalSettings.MyTestPath to string:
string works = (string)evaluator.Eval("PathCombine((string)GlobalSettings.MyTestPath,\"test.txt\")");
Assert.That(works, Is.EqualTo("C:\\delme\\test.txt"));

// I think this should work, but doesn't:
string doesntWork = (string)evaluator.Eval("PathCombine(GlobalSettings.MyTestPath,\"test.txt\")");
Assert.That(doesntWork, Is.EqualTo("C:\\delme\\test.txt"));

Because PathCombine takes a string, and GlobalSettings.MyTestPath is a string, I think this should work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions