mardi 23 juillet 2019

Why are ParameterInfo and MemberInfo causing an AmbigousMatchException?

According to Microsoft Metadata, ParameterInfo is:

namespace System.Reflection
{
  //
  // Summary:
  //   Discovers the attributes of a parameter and provides access to parameter
  //   metadata.
  public class ParameterInfo : ICustomAttributeProvider, IObjectReference
  // ....
}

and MemberInfo is:

namespace System.Reflection
{
  //
  // Summary:
  //   Obtains information about the attributes of a member and provides access to member
  //   metadata.
  public abstract class MemberInfo : ICustomAttributeProvider
  // ...
}

So I'm not sure why the following code would throw an AmbiguousMatchException:

DotNetFiddle Example.

using System;
using System.Reflection;

public class Program
{
    public static void Main()
    {
        var pi = typeof(object).GetMethod("Equals").GetParameters()[0];
        Test(pi);
    }

    private static void Test(MemberInfo mi)
    {
    }

    private static void Test(ParameterInfo pi)
    {
    }
}

result:

Unhandled Exception: System.Reflection.AmbiguousMatchException: Ambiguous match found.

at System.RuntimeType.GetMethodImplCommon(String name, Int32 genericParameterCount, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)

at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)

at System.Type.GetMethod(String name, BindingFlags bindingAttr)

at Program.Main()





Aucun commentaire:

Enregistrer un commentaire