我如何从 MethodInfo 访问实例

How I can access instance from MethodInfo

我有一个接收函数的方法,通常是一个方法。通过该函数,我可以使用 MethodInfo 访问方法的 class 和其他内容。 我希望能够在传递 function/method.

时访问使用的实例(如果有)

示例:

MyFunctionReceivingAMethod(new SomeClass().MethodA)

我想从 MethodA 的反射数据访问 MyFunctionReceivingAMethodSomeClass 实例。这可能吗?

更新:

我知道我可以传递对方法的引用,但我想知道是否可以使用我已有的函数指针来获取它。

MethodInfo class 中的反射数据通常与方法相关联,而不与任何特定实例相关联。因此,您无法从 class.

中获取所需的信息

相反,Delegate.Target property 将 return 与接收到的委托关联的实例(我假设这就是您正在寻找的)。根据它在 MSDN 上的描述, 属性 returns:

The object on which the current delegate invokes the instance method, if the delegate represents an instance method; null if the delegate represents a static method.