c#中如何使用反射调用重载方法.歧义匹配异常 [英] How to call the overloaded method using reflection in c#. AmbiguousMatchException

查看:70
本文介绍了c#中如何使用反射调用重载方法.歧义匹配异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用反射调用重载方法.

public void SomeMethod(string param){param = param.Length >0 ?参数:空;输入 thisType = this.GetType();MethodInfo theMethod = thisType.GetMethod("methodName", BindingFlags.NonPublic | BindingFlags.Instance);theMethod.Invoke(this, param);}

当我使用两种方法中的一种时,一切都很好:

//如果 param = "" (null) 效果很好私有无效方法名称(){}//如果 param = "anystring" 效果很好私有无效方法名称(字符串参数){}

我只能使用其中一种方法.但是当我在类中使用这两种方法时(我需要同时使用这两种情况 - 当 param 将被传递而没有他时)我得到了异常:

<块引用>

AmbiguousMatchException:找到不明确的匹配

如何同时使用这两种重载方法?

解决方案

您应该使用 this 方法重载 GetMethod 以找到合适的方法 methodName.此重载考虑了方法参数及其类型的数量.使用这个重载方法 SomeMethod 应该像这样重写:

public void SomeMethod(string param){输入 thisType = GetType();if (!string.IsNullOrEmpty(param)){//使用一个参数查找并调用方法.MethodInfo theMethod = thisType.GetMethod("methodName", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] {typeof(string)}, null);theMethod.Invoke(this, new object[] {param});}别的{//查找并调用不带参数的方法.MethodInfo theMethod = thisType.GetMethod("methodName", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);theMethod.Invoke(this, null);}}

以下是使用此方法的示例:https://dotnetfiddle.net/efK1nt.>

I try to call overloaded method using reflection.

public void SomeMethod(string param)
{
    param = param.Length > 0 ? param : null;
    Type thisType = this.GetType();
    MethodInfo theMethod = thisType.GetMethod("methodName", BindingFlags.NonPublic | BindingFlags.Instance);
    theMethod.Invoke(this, param);
}

When I use one of both method all works well:

//works well if param = "" (null)
private void methodName(){
}

//works well if param = "anystring"
private void methodName(string parameter){
}

I can use only one of those methods. But when I use both methods in the class (I need to use both cases - when param will be passed and without him) I get the exception:

AmbiguousMatchException: Ambiguous match found

How to can I use both overloaded methods?

解决方案

You should use this overload of the method GetMethod to find appropriate method methodName. This overload takes to account number of method arguments and its types. Using this overload method SomeMethod should be rewritten like this:

public void SomeMethod(string param)
{
    Type thisType = GetType();

    if (!string.IsNullOrEmpty(param))
    {
        // Find and invoke method with one argument.
        MethodInfo theMethod = thisType.GetMethod("methodName", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] {typeof(string)}, null);
        theMethod.Invoke(this, new object[] {param});
    }
    else
    {
        // Find and invoke method without arguments.
        MethodInfo theMethod = thisType.GetMethod("methodName", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);
        theMethod.Invoke(this, null);
    }
}

Here is a sample of using this approach: https://dotnetfiddle.net/efK1nt.

这篇关于c#中如何使用反射调用重载方法.歧义匹配异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆