Creating and using a generic interface
Generic interfaces work in much the same way as the previous examples in generics. Let's assume that we want to find the properties of certain classes in our code, but we can't be sure how many classes we will need to inspect. A generic interface could come in very handy here.
Getting ready
We need to inspect several classes for their properties. To do this, we will create a generic interface that will return a list of all the properties found for a class as a list of strings.
How to do it…
Let's take a look at the following implementation of the generic interface as follows:
- Go ahead and create a generic interface called
IListClassProperties<T>
. The interface will define a method that needs to be used calledGetPropertyList()
that simply uses a LINQ query to return aList<string>
object:interface IListClassProperties<T> { List<string> GetPropertyList(); }
- Next, create a generic class called
InspectClass...