- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我写的使用反射的代码中
if (f.FieldType.IsAssignableFrom("".GetType()))
我有一个隐式转换为字符串的类。但是上面的 if 语句没有捕捉到它。如何通过隐式字符串转换进行反射/上述 if 语句捕获字符串和类?而不是特定的字符串和我知道的每个类?
最佳答案
我会使用一个扩展方法,它获取所有公共(public)静态方法并检查具有正确名称和返回类型的方法。
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;
}
}
关于c# - IsAssignableFrom 的隐式版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2075471/
我想更好地理解 Java 中基本类型和引用类型之间的 isAssignableFrom 行为。 例如: System.out.println(boolean.class.isAssignableFro
我有两个 java 类,如下所示, public class Class1{ private Object actionObject; public Object getActi
不确定我是否可以这样做,但我正在尝试查看一个类型是否继承自具有泛型约束的另一个类型。 这是我要查找的类: public class WorkoutCommentStreamMap : ClassMap
我有一些第三方类(class)。其精简版(为了简洁起见)功能如下: 提供两个类 ClassA 和 ClassB 供外部使用。这些类中的每一个类都与其他类完全无关(这意味着它们没有共同的祖先)。 有一个
我想更好地理解 Java 中基本类型和引用类型之间的 isAssignableFrom 行为。 例如: System.out.println(boolean.class.isAssignableFro
我如何检测类型 x 是否可从类型 y 分配,不仅通过继承层次结构而且通过协变和逆变? 最佳答案 IsAssignableFrom 会检查协变和逆变,您不需要任何其他东西: // Covariance
我有下一个代码: private T CreateInstance(object obj) // where T : ISomeInterface, class { ... if (!
在我写的使用反射的代码中 if (f.FieldType.IsAssignableFrom("".GetType())) 我有一个隐式转换为字符串的类。但是上面的 if 语句没有捕捉到它。如何通过隐式
在 Kotlin (1.0.6) 中,通过反射我需要迭代一个类的成员(我们称之为 Foo),并根据返回类型做一些事情。我可以编写以下内容,它有效: Foo::class.members{ m ->
我真的对这种行为感到困惑。在 JBoss 7 中,我使用 @Entity 注释检索所有类,然后我需要找到所有实现接口(interface)“BusinessObject”的类。 这是实现它的方法:
我正在查看 java.lang.Class#isAssignableFrom来自 Java Reflection in Action . 为什么 Object.class.isAssignableFr
我正在使用 COM-API,Autodesk Inventor。 此测试通过: [Test] public void CastTest() { Inventor.Document docume
我正在尝试创建一个通用包装器来包装所有类型的值并提供一些额外的功能。 包装器看起来像: class PropertyWrapper { private T _value; publi
给定一个 Class 对象,我如何检查它的“祖先”之一是否是某个类?有多次调用 getSuperClass 的替代方法吗? 最佳答案 给定一个类 c1,您想知道它的祖先之一是否是 c2? 不会 c2.
所以我想检查一个类是否可以分配给包含许多子类的父类(super class),像这样 public class A { public A(){ } } public class B exten
我发现原始类型问题 System.out.println("Integer.class.isAssignableFrom(int.class) = " + Integer.class.isAssign
int i=1; long longOne=i; //assignment works fine //...but bool canAssign=(typeof(long).IsAssignableF
实现oauth2系统,我在使用以下代码时遇到一些问题: import org.springframework.security.oauth2.provider.endpoint.FrameworkEn
我有一些 Java 代码需要使用当前支持泛型的编译器以及(尚)不了解泛型的旧版或外来编译器进行编译。除了极少数使用 Class.isAssignableFrom() 的情况外,我设法使几乎所有代码都可
我使用 isAssignableFrom() 类方法来确定其他应用程序中的类是否实现接口(interface)。因此,我在两个“应用程序”中具有相同的接口(interface),但不在同一个包中。当两
我是一名优秀的程序员,十分优秀!