Generics oddity


And following this series of posts (here and here) on "strange phenomena" in .Net development, generics have their own group.

I was refactoring some code in order to avoid repetition and I came with what I thought it was a clever solution: using generics when the only difference is the type of the variable.

Great!! You discover America again!!! Want a medal for that?

Not really. Because it did not work.

Uh... let's see some code:

Two simple classes that inherit from each other:

One entry point that receives an object and acts depending of the type (no, I did not design it, I was extending Enterprise Library):

A generic method that can receive both types, perform the common operations and calls a non-generic method with the specific type (it displays the type of the object passed, just in case):

And the not generic methods that performs the specific operations on each type:

So I was expecting that the generic method could call the specific method depending on the type. Wasn't the whole fuzz on generics that they were strongly-typed and type-safe?

They must be, but they can't achieve my purpose as only the less specific ExecuteWriteSp(), the one that receives the Base type, gets called. The fact that the generic method outputs the right type name does not help.

In fact it adds confusion to my already bewildered head...

Any ideas why the code would not do what I want it to do?

PS: by the way, WL() is just the shortcut for Console.WriteLine() that wonderful Snippet Compiler uses

share class