将从接口类型获得的MethodInfo对象转换为C#中实现类型上的相应MethodInfo对象? [英] Translating a MethodInfo object obtained from an interface type, to the corresponding MethodInfo object on an implementing type in C#?
问题描述
我的问题是:如果我有一个MethodInfo对象,该对象是从接口类型获得的方法,并且我还有一个类型对象,该对象是实现该接口的类的,但是它使用一个显式实现,如何为该类中的实现方法正确获取相应的MethodInfo对象?
The question I have is this: If I have the MethodInfo object, for a method, obtained from an interface type, and I also have the Type object for a class that implements this interface, but it implements the said method with an explicit implementation, how do I correctly obtain the corresponding MethodInfo object for the implementing method in that class?
之所以需要这样做,是因为实现方法可以应用一些属性,我需要通过反射来找到它们,但是需要找到这些属性的类只有实现类的对象引用,以及该接口的Type对象(+对应的MethodInfo对象).
The reason I need to do this is that implementing methods can have some attributes applied to them, and I need to find these through reflection, but the class that needs to find those attributes only have an object reference for the implementing class, and the Type object (+ corresponding MethodInfo objects) for the interface.
所以,假设我有以下程序:
So, let's assume I have the following program:
using System;
using System.Reflection;
namespace ConsoleApplication8
{
public interface ITest
{
void Test();
}
public class Test : ITest
{
void ITest.Test()
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
Type interfaceType = typeof(ITest);
Type classType = typeof(Test);
MethodInfo testMethodViaInterface =
interfaceType.GetMethods()[0];
MethodInfo implementingMethod =
classType.GetMethod(/* ??? */"Test");
Console.Out.WriteLine("interface: " +
testMethodViaInterface.Name);
if (implementingMethod != null)
Console.Out.WriteLine("class: " +
implementingMethod.Name);
else
Console.Out.WriteLine("class: unable to locate");
Console.Out.Write("Press enter to exit...");
Console.In.ReadLine();
}
}
}
运行此命令可以给我:
interface: Test
class: unable to locate
Press enter to exit...
在代码中有一个带有???的.GetMethod调用.评论.这是我需要帮助的部分.我需要在这里指定(并且我已经测试了很多,这使我陷入了另一种困境),或者需要用这段代码替换.
Up in the code there is a .GetMethod call with a ??? comment. This part is what I need help with. Either what I need to specify here (and I've tested a lot, which brings me to the other way) or what I need to replace this code with.
由于我从接口使用了该方法的显式实现,因此该方法的实际名称不只是"Test".如果我使用以下代码转储类类型的GetMethods()数组的全部内容:
Since I used explicit implementation of the method from the interface, the actual name of the method isn't just "Test". If I dump the entire contents of the GetMethods() array of the class type, with this code:
foreach (var mi in classType.GetMethods(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
Console.Out.WriteLine(mi.Name);
}
然后我得到了:
ConsoleApplication8.ITest.Test <-- this is the one I want
ToString
Equals
GetHashCode
GetType
Finalize
MemberwiseClone
很明显,该名称具有接口的全名及其前面的名称空间.但是,由于重载,我要做的就是在类中找到所有此类实现方法(即,假设有多个Test方法因参数而异),然后比较参数.
clearly, the name has the full name of the interface and its namespace in front of it. However, due to overloading, what it looks like I have to do is find all such implementing methods in the class (ie. assuming there's multiple Test methods varying by parameters), and then compare parameters.
有没有更简单的方法?基本上,我想从接口中获得方法的MethodInfo对象后,通过获取其MethodInfo对象来查找实现该方法的类的确切方法.
Is there an easier way? Basically I'd like to, once I have the MethodInfo object for a method from an interface, to find the exact method that a class that implements this method, by getting its MethodInfo object.
请注意,我在这里处于循环状态,因此,如果我必须遍历类中的方法以从接口中找到确切的方法,那没关系,只要我有一个很好的方法来确定何时我可以找到了合适的.
Note that I'm in a loopy situation here, so if I have to loop through the methods in the class to find the exact method from the interface, that's ok, as long as I have a good way to identify when I have found the right one.
我试图这样改变上面的循环:
I tried to change the loop above like this:
foreach (var mi in classType.GetMethods(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
if (mi.GetBaseDefinition() == testMethodViaInterface)
Console.Out.WriteLine(mi.Name);
}
这并没有打印出任何内容,因此很显然,这样的方法上的 GetBaseDefinition
并不指向接口中的MethodInfo对象.
This didn't print out anything, so clearly GetBaseDefinition
on such a method doesn't point to the MethodInfo object from the interface.
有指针吗?
推荐答案
查看 Type.GetInterfaceMap
.抱歉-很着急,所以没有时间给出完整的答案,但这应该是一个开始.
Look at Type.GetInterfaceMap
. Sorry - am in a rush so don't have time for a full answer but it should be a start.
这篇关于将从接口类型获得的MethodInfo对象转换为C#中实现类型上的相应MethodInfo对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!