Given a C# function as below, what is the valid syntax in IronPython to call it?
public void MyFunction( Expression<Action> foo)
{
}
Given a C# function as below, what is the valid syntax in IronPython to call it?
public void MyFunction( Expression<Action> foo)
{
}
You just need to construct an expression that fits the pattern. In the expression library, a generic expression is a specialized lambda expression. So you would just write the appropriate lambda expression in IronPython. Unfortunately you won't get any compiler help with this as you would in C#, you'll have to build it by hand.
Fortunately, the dlr will be very forgiving and will infer some types for you where possible.
Given a library SomeLibrary.dll
, with a given class:
using System;
using System.Linq.Expressions;
namespace SomeNamespace
{
public class SomeClass
{
public void SomeMethod(Expression<Action> expr) => Console.WriteLine(expr);
}
}
import clr
clr.AddReference('System.Core')
clr.AddReference('SomeLibrary')
from System import Console, Array, Type, Action
from System.Linq.Expressions import *
from SomeNamespace import SomeClass
# build the expression
expr = Expression.Lambda(
Expression.Call(
clr.GetClrType(Console).GetMethod('WriteLine', Array[Type]([str])),
Expression.Constant('Hello World')
)
)
# technically, the type of expr is Expression as far as C# is concerned
# but it happens to be an instance of Expression<Action> which the dlr will make work
SomeClass().SomeMethod(expr)