Assign a variable type, use a variable with a common static class

I work in a C # web service with a common static class that accepts a type. I was wondering why this does not compile:

Type type1 = typeof(MySnazzyType); Assert.AreEqual(0, ConnectionPool_Accessor<type1>._pool.Count); 

He gives this error:

Cannot find the type or namespace name 'type1' (are you missing the using directive or assembly references?)

And ReSharper, when I hang over type1 in the second line of code, says "Type or namespace name." Well, type1 is type! This is a variable of type Type ! It also does not work if I do this:

 Type type1 = typeof(MySnazzyType); Assert.AreEqual(0, ConnectionPool_Accessor<typeof(type1)>._pool.Count); 

I was hoping to assign my types to several Type variables and just use the ones that test different common static classes, instead of printing MySnazzyType every time. Any ideas, or I'm stuck in business:

 Assert.AreEqual(0, ConnectionPool_Accessor<MySnazzyType>._pool.Count); 

Edit: to clarify, MySnazzyType not a generic class and is not inherited from a generic class. The only common class is ConnectionPool_Accessor .

Thanks to Paul’s remark, “Essentially, your problem is that C # is a statically typed language”, now I know what Ruby ruined for me .;)

+4
source share
3 answers

First of all, ReSharper is actually correct. This is not a type, this is a variable. Of course, this is a variable that holds the reflection object that matches the type, but this is not enough.

Between the brackets <...> you should write the type name, not the name of another identifier.

However, you can create shared objects through reflection and access their properties, even static ones, so you should be able to rewrite the code for this, however, have you looked at NUnit 2.5?

From the latest release notes, it looks like unit test classes can now be shared, and you can specify a test class attribute that will test it with.

This will allow you to write something like this (note, I did not test this, I was only looking for the names of the new attributes in the documentation):

 [TestFixture(typeof(MySnazzyType))] [TestFixture(typeof(MyOtherSnazzyType))] public class Tests<T> { [Test] public void PoolCount_IsZero() { Assert.AreEqual(0, ConnectionPool_Accessor<T>._pool.Count); } } 
+3
source

Generic types are computed at compile time, not at runtime. Since at runtime it cannot be determined what type1 will be, this construct is not allowed.

This is actually what Resharper says: type1 not a type, it is a variable of type Type (just as it can be an object of type String ).

+3
source

The TestFixture attribute should set you up, but only for its devils: if you want to do all this at runtime, you can do it with reflection.

 Type poolType = typeof(ConnectionPool_Accessor<>); Type snazzyType = typeof(MySnazzyType); // Or however you want to get the appropriate Type poolType.MakeGenericType(snazzyType); 

Then you can continue to do whatever you want using reflection on poolType . Of course, this will be a big pain in the butt, if you do not use the dynamic typing of C # 4.0.

+1
source

All Articles