次のスニペットは、機能するはずですが失敗します。IsInstanceOfTypeも試しました。
Assert.IsTrue(typeof(Predicate<>).IsAssignableFrom(typeof(Predicate<int>)), "No predicate match!");
私の主張は間違っていますか?
いいえ、それは機能しません-オープンタイプの値を持つことは決してできないので、それは機能しませんでした。
おそらくあなたが望むように聞こえますType.GetGenericTypeDefinition
:
if (type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Predicate<>))
{
...
}
私はそれがその方法が機能することになっている方法ではないと思います。
A
タイプとタイプインの関係についてB
:
A.IsAssignableFrom(B) == true
、A
はの基本クラスであるB
、またはA
のインターフェースですB
のようなもの:
public class Animal { }
public class Dog : Animal { }
となることによって:
typeof(Animal).IsAssignableFrom(typeof(Dog)) == true;
// you can do
Dog spot = new Dog();
Animal a = spot; // assigned an Animal from a Dog
// but not
Animal b = new Animal();
Dog spike = b; // compiler complains