Given a C# function as below, what is the valid syntax in IronPython to call it?
给定C#函数如下,IronPython中调用它的有效语法是什么?
public void MyFunction( Expression<Action> foo)
{
}
1 个解决方案
#1
0
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.
您只需要构造一个适合该模式的表达式。在表达式库中,泛型表达式是一个专门的lambda表达式。所以你只需要在IronPython中编写适当的lambda表达式。不幸的是,你不会像在C#中那样得到任何编译器帮助,你必须手动构建它。
Fortunately, the dlr will be very forgiving and will infer some types for you where possible.
幸运的是,dlr将非常宽容,并会在可能的情况下为您推断某些类型。
Given a library SomeLibrary.dll
, with a given class:
给定一个SomeLibrary.dll库,给定一个类:
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)
#1
0
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.
您只需要构造一个适合该模式的表达式。在表达式库中,泛型表达式是一个专门的lambda表达式。所以你只需要在IronPython中编写适当的lambda表达式。不幸的是,你不会像在C#中那样得到任何编译器帮助,你必须手动构建它。
Fortunately, the dlr will be very forgiving and will infer some types for you where possible.
幸运的是,dlr将非常宽容,并会在可能的情况下为您推断某些类型。
Given a library SomeLibrary.dll
, with a given class:
给定一个SomeLibrary.dll库,给定一个类:
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)