隐版本IsAssignableFrom的? [英] Implicit version of IsAssignableFrom?
本文介绍了隐版本IsAssignableFrom的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我使用的思考,我写code
In my code using reflections i wrote
if (f.FieldType.IsAssignableFrom("".GetType()))
我有了一个隐式转换为字符串类。然而,如果上述声明犯规抓住它。我怎样才能使反射/上述if语句捕获字符串和类隐式串转换?而不是具体的字符串和每个班级我知道的?
I have a class that has an implicit conversion to strings. However the if statement above doesnt catch it. How can i make reflection/the above if statement catch strings and classes with implicit string conversion? instead of specifically strings and each class i know about?
推荐答案
我会用它可以获取所有的公共静态方法,并检查使用正确的名称的方法扩展方法和返回类型。
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;
}
}
这篇关于隐版本IsAssignableFrom的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文