简体   繁体   English

在C#中获取变量下函数的MethodInfo

[英]Get MethodInfo of a function under a variable in C#

I have a specific object in C#, call it MyCustomObject .我在 C# 中有一个特定的对象,称之为MyCustomObject MyCustomObject is of type MyNamespace.CustomObject , and every object of that type contains a method MyCustomMethod . MyCustomObject属于MyNamespace.CustomObject类型,该类型的每个对象都包含一个方法MyCustomMethod I am trying to get the MethodInfo ( System.Reflection.MethodInfo ) of MyCustomObject.MyCustomMethod so I can use it to create an expression tree later.我正在尝试获取MyCustomObject.MyCustomMethodMethodInfo ( System.Reflection.MethodInfo ),以便以后可以使用它来创建表达式树。 However, if I just use typeof(MyCustomObject).GetMethod("MyMethodInfo") , it returns a general method for all objects of type MyNamespace.CustomObject .但是,如果我只使用typeof(MyCustomObject).GetMethod("MyMethodInfo") ,它会为MyNamespace.CustomObject类型的所有对象返回一个通用方法。 How can I get the MethodInfo of just MyCustomObject.MyCustomMethod ?如何获得MyCustomObject.MyCustomMethodMethodInfo

When creating your expression tree (per this comment ), you presumably want to use the Call factory method.创建表达式树时(根据此注释),您大概想使用Call工厂方法。

In your case, you're trying to create an expression tree representing an instance method call , not a static method call ;在您的情况下,您正在尝试创建一个表示实例方法调用的表达式树,而不是静态方法调用 the difference between them is that an instance method call uses an instance, while a static method call does not.它们之间的区别在于实例方法调用使用实例,而静态方法调用不使用。

To create such an expression tree, you'll need some kind of expression tree that represents the instance;要创建这样的表达式树,您需要某种表示实例的表达式树; it might be a property or field, or the result of another method call.它可能是一个属性或字段,或者另一个方法调用的结果。 But if you want to apply it to an existing instance, you could pass your instance into the Constant factory method.但是如果你想将它应用到一个现有的实例,你可以将你的实例传递给Constant工厂方法。

You could pass this node into one of the Call overloads which represent an instance method call, such as this one , for an instance method call with no arguments.对于没有参数的实例方法调用,您可以将此节点传递到代表实例方法调用的Call重载之一,例如this one

Something like this:像这样的东西:

// using System.Linq.Expressions.Expression

CustomObject MyCustomObject = /* initialized somehow */
var methodInfo = typeof(CustomObject).GetMethod("MyCustomMethod");
var expr = Lambda(
    Call(
        Constant(MyCustomObject),
        methodInfo
    ),
    new ParameterExpression[] { }  // if the LambdaExpression has parameters, add them here
);

Addendum附录

When using the compiler to generate a similar expression tree:当使用编译器生成类似的表达式树时:

CustomObject MyCustomObject = /* initialized somehow */
Expression<Action> expr = () => MyCustomObject.MyCustomMethod();

MyCustomObject isn't represented with a ConstantExpression , but rather with a MemberAccessExpression . MyCustomObject不是用ConstantExpression表示,而是用MemberAccessExpression The C# compiler rewrites closed-over variables (in this case, MyCustomObject within the lambda expression) as a property access on a compiler-generated object. C# 编译器将封闭变量(在本例中为 lambda 表达式中的MyCustomObject )重写为对编译器生成的对象的属性访问。 Instead of the call to Constant , the corresponding factory methods to represent MyCustomObject would look something like this:代替对Constant的调用,表示MyCustomObject的相应工厂方法将如下所示:

// using System.Linq.Expressions.Expression

PropertyOrField(
    Constant(<<closure_object>>),
    "MyCustomObject"
)

We can't write something like this in code, because our code doesn't have access to the <<closure_object>> instance.我们不能在代码中写这样的东西,因为我们的代码无权访问<<closure_object>>实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM