Check If Instance Is of a Type

Check if instance is of a type

The different answers here have two different meanings.

If you want to check whether an instance is of an exact type then

if (c.GetType() == typeof(TForm))

is the way to go.

If you want to know whether c is an instance of TForm or a subclass then use is/as:

if (c is TForm)

or

TForm form = c as TForm;
if (form != null)

It's worth being clear in your mind about which of these behaviour you actually want.

How to check if a subclass is an instance of a class at runtime?

You have to read the API carefully for this methods. Sometimes you can get confused very easily.

It is either:

if (B.class.isInstance(view))

API says: Determines if the specified Object (the parameter) is assignment-compatible with the object represented by this Class (The class object you are calling the method at)

or:

if (B.class.isAssignableFrom(view.getClass()))

API says: Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter

or (without reflection and the recommended one):

if (view instanceof B)

Check if some object is instance of some class in a list

These kinds of requirements call for the use of reflection. There is a class in Java meant to represent the type of an object: class Class. Instances of that class effectively represent the types of objects. So you could do:

List<Class<?>> types = new ArrayList<>();
types.add(Type1.class);
types.add(Type2.class);

for (Class<?> type : types) {
if (type.isAssignableFrom(someObject.getClass())) {
doSomething();
}
}

Note that in this situation it's important to know whether you want to check if your target object has exactly the same type as a type in a list, or can be assigned to the type in the list. The code sample covers the second option because it's closer to the original intent. If you need an exact match, you would do:

object.getClass() == type;

See also Class.isInstance vs Class.isAssignableFrom

How to check if an object is of instance `Type`?

You can't do so using a runtime Type object.

To achieve something similar, you'll have to use generic functions instead:

void foo<T>(dynamic instance) {
if (instance is T) {

}
}

How can I check if a object is an instance of a specific class?

You can check if an object is an instance of a class with instanceof, e.g.

if($role instanceof SimpleXMLElement) {
//do stuff
}

Determine if a variable is an instance of any class

I think your understanding of what an instance is wrong here, since everything is an object in python, so 5 is an object of class int and [2,3] is an object of class list and so on.

isinstance(x, y) is the way to go if you just want to check if x is an object of y, but if you want to check if x is an object of builtin class or your own custom defined class then you should be checking the existence of __dict__ using hasattr(x, '__dict__').

Check if an object is an instance of a class (but not an instance of its subclass)

If you have to do this, the only way would be the getClass().equals(Foo.class) option you've suggested.

However, the goal of OO design is to allow you to treat any Foo in the same fashion. Whether or not the instance is a subclass should be irrelevant in a normal program.

Python check if variable isinstance of any type in list

isinstance() takes a tuple of classes for the second argument. It'll return true if the first argument is an instance of any of the types in that sequence:

isinstance(var, (classinfo1, classinfo2, classinfo3))

In other words, isinstance() already offers this functionality, out of the box.

From the isinstance() documentation:

If classinfo is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted).

Emphasis mine; note the recursive nature; (classinfo1, (classinfo2, classinfo3)) is also a valid option.



Related Topics



Leave a reply



Submit