Capgemini / Cauldron

C# Toolkit
MIT License
77 stars 18 forks source link

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

Closed reflection-emit closed 7 years ago

reflection-emit commented 7 years ago

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;
    }
reflection-emit commented 7 years ago

fixed