Private _m As IntegerPublic Interface MyMarkerInterface End Interface Public Class myTestCls Implements MyMarkerInterface
Public ReadOnly Property Age() As Integer Get Return _m End Get End Property End Class
The above is the class that implements some interface........ So in some case I want to check whether the instance of above class is instance of Interface.
Now all the above methods are using different methods to compare the type of object to check is this type of particular kind .
Private Function isMarkerkedObj1(ByVal o1 As myTestCls) As Boolean If TypeOf o1 Is MyMarkerInterface Then Return True Else Return False End If End Function Private Function isMarkerkedObj2(ByVal o1 As myTestCls) As Boolean If o1.GetType.IsSubclassOf(GetType(MyMarkerInterface)) Then Return True Else Return False End If End Function Private Function isMarkerkedObj3(ByVal o1 As myTestCls) As Boolean If GetType(MyMarkerInterface).IsAssignableFrom(o1.GetType) Then Return True Else Return False End If End Function
e.g. Dim obj1 as new myTestCls
-------------
-------------
------------- isMarkerkedObj1(obj1) isMarkerkedObj2(obj1) isMarkerkedObj3(obj1)
all the above calls will return true.
Note: The requirement discussed might be confusing but is very useful in many scenarios a the interface used is a marker interface which consists of nothing in the body. So in certain cases we may require to mark some objects for certain behavior then we use this interface.
No comments:
Post a Comment