• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Half baked ideas

Lots of contracts porting Java to C#.

I wonder why?

:laugh
 
OK

I have an interface Wotsit<T> { T doStuff(); }

I want a factory method that returns a Wotsit for different types of T. It needs to support multiple types, lookup a wotsit based on the return type, and support runtime registration of new types and associated wotsits, so the factory method needs to take the return type as a parameter. I can do this as long as T isn't a generic type. Unfortunately I need it to support generic types.

e.g. I can do this
Wotsit<Blah> myWotsit = WotsitFactory.getWotsit(Blah.class);
Blah result = myWotsit.doStuff();

but this doesn't work

Wotsit<List<Blah>> myWotsit = WotsitFactory.getWotsit(List<Blah>.class);
List<Blah> result = myWotsit.doStuff();
for (Blah blah : result) {
....

Yes, I know I can use Object, but I don't want to, that defeats the point of using generics in the first place. I might as well be using ******* void *

Can C# do this?

What I can do is create a non generic class that implements List<Blah> and then use something like

Wotsit<? extends List<Blah>> myWotsit = WotsitFactory.getWotsit(BlahList.class);

What a PITA.

Especially when you have List<BlahList> to deal with. One BlahListListListListList coming up.
 
Last edited: 22:19
tumblr_lri1z2Yydj1qz7hyso1_500-e1316707763726.jpg
 

Advertisers

Back
Top