Interface Discovery


Before you attempt to use an object through an interface it's best to discover if the object in fact supports the interface. Obviously if you have the definition of the class, it's easy to look at it and see if the class implements the interface. However, sometimes you may be dealing with the object data type and you may want to see if the object the variable points to is compatible with the interface you want to use. There are two ways of finding out if an object supports the interface.

To discover if an object supports the interface:

  1. Type if (obj is IAnything) where obj is the variable that points to the object you wish to test, and IAnything is the name of the interface for which you wish to test ( Figure 8.22 ).

    Figure 8.22 The variable obj is of type object. That means that it can point to a Cat object or a Dog object. The code tests to see what obj contains by checking if it supports the IDog interface.
     interface ICat {    string IgnoreOwner(); } interface IDog {    string ListenToOwner(); } class Poodle : IDog { } class Siamese : ICat { } class Owner {    void FeedAnimal(object obj)    {      if (  obj is IDog  )      {         IDog dog = (IDog)obj;         string cmd = dog.ListenToOwner();      }    } } 

    or

    Type IAnything var = obj as IAnything , where IAnything is the interface you wish to test for, var is any variable to hold a reference to the object, and obj is the object you wish to test.

  2. Type if (var != null) . If after performing step one, var is equal to null then the object does not support the interface. If it returns a non null value then the object supports the interface and var now points to an interface reference ( Figure 8.23 ).

    Figure 8.23 With the as command if the object does not have support for the interface the result is null.
     interface ICat {    string IgnoreOwner(); } interface IDog {    string ListenToOwner(); } class Poodle : IDog { } class Siamese : ICat { } class Owner {    void FeedAnimal(object obj)    {      IDog dog =  obj as IDog;  if (dog != null)         string cmd = dog.ListenToOwner();    } } 

graphics/tick.gif Tips

  • With the first mechanism, if you discover that the object in fact supports the interface, to use the object through the interface you have to declare a variable of the type of interface and cast the object to the interface.

  • With the second mechanism of discovery, both the casting of the object and the discovery are done in one step. If the object supports the interface, the var variable will point to the object, if it doesn't support the interface, the var variable will have the value of null.

  • There is a third mechanism for discovering if an object supports an interface. You could attempt to cast the object to the interface. If the object doesn't support the interface, the casting will generate an exception (an error). Exceptions will be discussed in Chapter 11, "Error Handling." Counting on an exception as a way of discovering if the object supports the interface is discouraged because exceptions have the potential of affecting program performance.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net