C# 如何使用 Type.InvokeMember 调用显式实现的接口方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15296108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:21:13  来源:igfitidea点击:

How to use Type.InvokeMember to call an explicitly implemented interface method?

c#reflection

提问by Polyfun

I want to call an explicitly implemented interface method (BusinessObject2.InterfaceMethod) via reflection, but when I try this using the following code, I get an System.MissingMethodException for the Type.InvokeMember call. Non-interface methods work OK. Is there a way to do this? Thanks.

我想通过反射调用显式实现的接口方法 (BusinessObject2.InterfaceMethod),但是当我使用以下代码尝试此方法时,我收到 Type.InvokeMember 调用的 System.MissingMethodException。非接口方法工作正常。有没有办法做到这一点?谢谢。

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace Example
{
    public class BusinessObject1
    {
        public int ProcessInput(string input)
        {
            Type type = Assembly.GetExecutingAssembly().GetType("Example.BusinessObject2");
            object instance = Activator.CreateInstance(type);
            instance = (IMyInterface)(instance);
            if (instance == null)
            {
                throw new InvalidOperationException("Activator.CreateInstance returned null. ");
            }

            object[] methodData = null;

            if (!string.IsNullOrEmpty(input))
            {
                methodData = new object[1];
                methodData[0] = input;
            }

            int response =
                (int)(
                    type.InvokeMember(
                        "InterfaceMethod",
                        BindingFlags.InvokeMethod | BindingFlags.Instance,
                        null,
                        instance,
                        methodData));

            return response;
        }
    }

    public interface IMyInterface
    {
        int InterfaceMethod(string input);
    }

    public class BusinessObject2 : IMyInterface
    {
        int IMyInterface.InterfaceMethod(string input)
        {
            return 0;
        }
    }
}

Exception details: "Method 'Example.BusinessObject2.InterfaceMethod' not found."

异常详细信息:“未找到方法 'Example.BusinessObject2.InterfaceMethod'。”

采纳答案by Rich O'Kelly

This is caused by the fact that BusinessObject2explicitly implements IMyInterface. You need to use the IMyInterfacetype to gain access to and subsequently invoke the method:

这是由BusinessObject2显式实现IMyInterface. 您需要使用该IMyInterface类型来访问并随后调用该方法:

int response = (int)(typeof(IMyInterface).InvokeMember(
                    "InterfaceMethod",
                    BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
                    null,
                    instance,
                    methodData));