lundi 16 mars 2020

How to call the overloaded method using reflection in c#. AmbiguousMatchException

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?





Aucun commentaire:

Enregistrer un commentaire