Where is IsInstanceOfType or its equivalent in the new .NET Type / TypeInfo API?

I read “Reflection Reflection API Blog Post” and I converted most of the code I used when I had the “old” API included in my PCL, but I could not find IsInstanceOfTypeeither the equivalent with Typeor TypeInfo. It is used very often, so I doubt that it completely fell under the new APIs, so I’m sure that the functionality was simply renamed or added together with some other function, and I just don’t see it.

+4
source share
2 answers

Use this extension method:

public static bool IsInstanceOfType(this Type type, object obj){
        return obj != null && type.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo());
}
+6
source

Instead

Type foo;
BarType bar;
if(foo.IsInstanceOfType(bar)) { ... }

I ended up using

Type foo;
BarType bar;
if(bar.GetType() == foo || bar.GetType().GetTypeInfo().IsSubclassOf(foo)) { ... }

which seems to work very similarly in most situations.

0
source

Source: https://habr.com/ru/post/1544405/


All Articles