2

Is there any way to call method by name and specify arguments without using dynamic? (C#)

UPDATE:

` public class Program { public static void Main(string[] args) { ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = engine.CreateScope();

        engine.ExecuteFile("script.py", scope);

        dynamic calculator = scope.GetVariable("Calculator");
        dynamic calculatorInstance = engine.Operations.CreateInstance(calculator);
        dynamic result = engine.Operations.InvokeMember(calculatorInstance, "sample");

        Console.ReadKey();
    }
}

`

class Calculator: def sample(self): return 'test'

That works great

2 Answers 2

2

You can use the ObjectOperations class to perform dynamic operations, and you can get an instance of it from a ScriptEngine instance:

ScriptEngine engine = ...;
object result = engine.Operations.InvokeMember(o, "foo", 1, 2, "3");
Sign up to request clarification or add additional context in comments.

Comments

0

Open Source Impromptu-Interface (can be found in nuget) has a bunch of methods for doing reflection-like invocation on any dynamic object, python, ruby, c#, etc, as it uses the dlr c# binder code.

 Impromptu.InvokeMember(o, "foo", 1, 2, "3")

But in this case, JeffHardy's answer is best, since you are already working with a script engine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.