Implicit version of IsAssignableFrom?

前端 未结 2 1085
春和景丽
春和景丽 2021-01-05 17:02

In my code using reflections i wrote

if (f.FieldType.IsAssignableFrom(\"\".GetType()))

I have a class that has an implicit conversion to st

2条回答
  •  臣服心动
    2021-01-05 17:07

    I would use an extension method which gets all public static methods and checks for a method with the correct name and return type.

    public static class TypeExtentions
    {
        public static bool ImplicitlyConvertsTo(this Type type, Type destinationType)
        {
    
            if (type == destinationType)
                return true;
    
    
            return (from method in type.GetMethods(BindingFlags.Static |
                                                   BindingFlags.Public)
                    where method.Name == "op_Implicit" &&
                          method.ReturnType == destinationType
                    select method
                    ).Count() > 0;
        }
    }
    

提交回复
热议问题