locked
Resolving System.Reflection.AmbiguousMatchException: Ambiguous match found. RRS feed

  • Question

  • User-284642143 posted

    I have a class with the following code

    private NewItem GetItem(Guid GId, Form f)
    {....}

    Using Reflection, im trying to call the above method but get the error "System.Reflection.AmbiguousMatchException: Ambiguous match found."

    getMethod = typeof(MyClass).GetMethod("GetItem", BindingFlags.NonPublic | BindingFlags.Instance);

    I understand the error in that it doesnt know which method to pick due to the same name so i tried getting the relevant parameters to pass in but it doesnt work (Just returns null)

            var it = typeof(MyClass)
            .GetMethods()
            .Where(x => x.Name == "GetItem")
            .FirstOrDefault(x => x.IsGenericMethod);

    How could i select the correct method?

    Monday, June 10, 2019 12:33 PM

Answers

  • User753101303 posted

    Hi,

    And the other signature is ? More likely you should add a criteria based on parameters count and/or types so that only a single method matches.

    Edit: and so you would test what is returned by https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodbase.getparameters?view=netframework-4.8

    BTW you know the class name, the method name and which parameters are expected but you still need to call this method using Reflection ?

    Edit 2: could give something such as :

    var getMethod = typeof(MyClass).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Single(m => m.Name=="GetItem" && m.GetParameters().Length== 2) ;

    It seems also you have a GetMethod overload you can use and ask for particular parameter types

    Edit 3: seems this one should work :

    var getMethod = typeof(MyClass).GetMethod("GetItem", BindingFlags.NonPublic | BindingFlags.Instance, null,new Type[] { typeof(Guid),typeof(Form) },  null);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 10, 2019 12:45 PM

All replies

  • User753101303 posted

    Hi,

    And the other signature is ? More likely you should add a criteria based on parameters count and/or types so that only a single method matches.

    Edit: and so you would test what is returned by https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodbase.getparameters?view=netframework-4.8

    BTW you know the class name, the method name and which parameters are expected but you still need to call this method using Reflection ?

    Edit 2: could give something such as :

    var getMethod = typeof(MyClass).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Single(m => m.Name=="GetItem" && m.GetParameters().Length== 2) ;

    It seems also you have a GetMethod overload you can use and ask for particular parameter types

    Edit 3: seems this one should work :

    var getMethod = typeof(MyClass).GetMethod("GetItem", BindingFlags.NonPublic | BindingFlags.Instance, null,new Type[] { typeof(Guid),typeof(Form) },  null);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 10, 2019 12:45 PM
  • User-284642143 posted

    Thanks PatriceSc

    "BTW you know the class name, the method name and which parameters are expected but you still need to call this method using Reflection ?"

    I only know all this as i investigated into the dll and got stuck at this point. I've never used Reflection until now but the syntax had me confused as to where and what i need to add. The code you provided did the trick.

    Monday, June 10, 2019 2:01 PM
  • User753101303 posted

    For some reason I missed this is a private member. Make sure though to test that when upgrading as the 3rd party could change this without any prior notice...

    Monday, June 10, 2019 2:29 PM