Reflection and me? Big friends. With all the love and hate a good friendship should have. A few days ago it was all about hate again. I had a bunch of service classes, some of them would implement a generic interface... Let's call it IHasAdorable - so a Service-Implementation could look like this:
public class MarketMerchant : IHasAdorable<CheeseBurger>, IProductSeller { // defined in IHasAdorable CheeseBurger BuyAdorable() { } // defined in IProductSeller IProduct Buy(String eanCode) { if (eanCode.equals("12345")) return this.bigStackOfSmellyFishburgers.Pop(); else } }
Now lets assume we want to browse through ALL market merchants and have a look if they have any adorable products. Let's skip the iteration process and pay attention to the probing of all market merchants in order to buy a adorable product from each of them. First attempt might be to use "is":
IProductSeller merchant; // iteration goes here { // this wont work. We are selling something very special, not just a stupid object! // Casting to IHasAdorable<> won't even compile. }
// `1 means there is 1 Generic parameter Type adorableInterfaceType = merchant.GetType().GetInterface("IHasAdorable`1"); if (adorableInterfaceType != null) { // yay, the merchant has adorable products, what whould those be!? Type adorableProductType = adorableInterfaceType.GetGenericArguments()[0]; // here is the magic we need to get the correct IHasAdorable Type // with "filled in" generic type. Type genericAdorableInterfaceType = MethodInfo mi = genericAdorableInterfaceType.GetMethod("BuyAdorable"); myBagOfAdorableProducts.Add(mi.invoke(merchant,null)); }
Comments
maybe your love and hate got confused this time?
In this case you should continue to love generics as they saved you, yet the generics turned out to be your enemy this time... ;-)