I'm working in a C# web service with a generic static class that takes a type. I was wondering why this does not compile:
Type type1 = typeof(MySnazzyType);
Assert.AreEqual(0, ConnectionPool_Accessor<type1>._pool.Count);
It gives this error:
The type or namespace name 'type1' could not be found (are you missing a using directive or an assembly reference?)
And ReSharper, when I hover over type1
in that second line of code, says "Type or namespace name expected". Well, type1
is a type! It's a variable of type Type
! It also doesn't work if I do:
Type type1 = typeof(MySnazzyType);
Assert.AreEqual(0, ConnectionPool_Accessor<typeof(type1)>._pool.Count);
I was hoping to assign my types to a couple of different Type
variables and just use those in testing the different generic static classes, instead of typing out MySnazzyType
each time. Any ideas, or am I stuck with doing:
Assert.AreEqual(0, ConnectionPool_Accessor<MySnazzyType>._pool.Count);
Edit: to clarify, MySnazzyType
is not a generic class, nor does it inherit from a generic class. The only generic class here is ConnectionPool_Accessor
.
Thanks to Pavel's comment "Essentially, your problem is that C# is a statically typed language", I now know that Ruby has spoiled me. ;)