Capgemini / Cauldron

C# Toolkit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

As<> Implicit Explicit convertion breaks if multiple methods with the same parameter exists

reflection-emit opened this issue · comments

Use this instead

    public static MethodInfo GetImplicitOrExplicit(this Type type, Type resultType)
    {
        var result = resultType.GetMethods(BindingFlags.Static | BindingFlags.Public)
              .FirstOrDefault(x => (x.Name == "op_Implicit" || x.Name == "op_Explicit") && x.ReturnType == resultType && x.GetParameters()[0].ParameterType == type);

        if (result == null)
            return type.GetMethods(BindingFlags.Static | BindingFlags.Public)
            .FirstOrDefault(x => (x.Name == "op_Implicit" || x.Name == "op_Explicit") && x.ReturnType == resultType && x.GetParameters()[0].ParameterType == type);

        return result;
    }