Friday, June 1, 2012

If Type Implements Interface == true - Part I

How to check if a type implements certain interface. .net framework provides various options to do just that. In this post we will be discussing these options and their limitations, if any.

This method is provided in System.Type to verify whether a reference of a given type can be assigned an instance of a specified type. As we know that polymorphism support in object oriented programming languages allow an instance of a certain type to be assigned to a reference of another type if that another type is either a parent in the inheritence hierarchy or an interface which the type implements. This method is to check just that. Let's suppose we have an interface IStudent with a contract as follows:

Now we implement this interface with a type, named Student, as follows:

Let's use Type.IsAssignableFrom to find out to check the type assignment for these types.

Let's run the above code and see if it behaves as expected.

As stated earlier, Type.IsAssignable can also be used to see the possible polymorphic behavior for inhertitence relationship between types. Let's add another class SpecialStudent which inherits from Student.

We can do the similar check for type assignment check between Student and SpecialStudent.

Running the above code yields the following output:

In the following example, we are using Type.IsAssignableFrom to scan all the assemblies in the current App Domain to find out the types which implements a particular interface. The interface type is passed as an argument to the method.

Let's use the above method to determine the types implementing IStudent interface. In the following code we are displaying all the implementing types on the console.

When we run the above code, it yields the following output.

It prints Student and SpecialStudent types. Additionally it prints IStudent as IsAssignableFrom would return true in that case too. We can just check if the other type is itself not interface.

Which just prints Student and SpecialStudent on the screen getting rid of IStudent.

This method provides list of all the interfaces that the specified type implements. In order to check whether the type implements our required interface, we can get this list from Type and check if our required interface is in there.

We can use the above method as follows:

Since we have the list of all the interfaces as System.Type objects, we can determine other interesting things about the interfaces. In the following example, we are determining whether the interface is generic, and we are interested in the type arguments.

As we discussed above, Type.GetInterfaces returns all the interfaces implemented by a type. We can later filter it with any filter criteria which is desired. System.Type.FindInterfaces is to make this filtering easier by just returning those interfaces implemented by a certain type which fulfills a certain criteria.

This ensures that all the interfaces that the type implements must pass through this filter criteria. Actually the filter method is called for each interface that the type implements. In the following example, we are expecting a certain type passed as argument. We need to check if the type implements an interface passed as the other argument.

Since we are just interested in checking whether a type implements a certain interface or not, that is why, the expression is very simple. Basically the .net framework giving us each interface implemented by type, one by one, to decide whether to include that in the resulting list or not. If we need to include this then the filter expression should return true, false otherwise. We can also return true all the time and get the same result as Type.GetInterfaces().

This method is to get the type of an interface implemented by a certain type. We just need to pass the interface name as string argument. The following example expects the type and the expected interface type as the parameters. It the uses Type.GetInterface() to determine if the type implements the specified interface. It uses both Type.Name and Type.FullName to run this test.

Let's use this method to test whether Student type implements IStudent and see the output.

This would result in the following output (which is as expected):

Let's use this to test if Calculator<int> implements ICalculator<int>

Let's run this and see the output. Please notice that using full name with generic type would not get the specified interface.

There is another overload of the same method [Type.GetInterface()] which allows a case-insensitive match for the string passed as the interface name.

is Operator
This is the easiest option to check the type of a certain object. The expression would yield true if the object is of the exact type or a child type as the specified type. This would also yield true if the object is of type which implements the interface. In the following example, we are creating Student and SpecialStudent instances. Since SpecialStudent inherits from Student which implements IStudent interface. The relationship can be seen in the following hierarchy:

Based the above hierarchy please see the expectations in the code below specified as the comments following the statements.

Which can be verified by the following output:

No comments: