简体   繁体   English

将类型赋值给变量,使用带有泛型静态类的变量

[英]assign type to variable, use variable with generic static class

I'm working in a C# web service with a generic static class that takes a type. 我正在使用一个带有类型的通用静态类的C#Web服务。 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?) 找不到类型或命名空间名称'type1'(您是否缺少using指令或程序集引用?)

And ReSharper, when I hover over type1 in that second line of code, says "Type or namespace name expected". 当我将鼠标悬停在第二行代码中的type1时,ReSharper会说“预期类型或命名空间名称”。 Well, type1 is a type! 好吧, type1 一个类型! It's a variable of type 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. 我希望将我的类型分配给几个不同的Type变量,并且只是在测试不同的通用静态类时使用它们,而不是每次都输入MySnazzyType 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. 编辑:澄清MySnazzyTypeMySnazzyType 不是泛型类,也不是从泛型类继承。 The only generic class here is ConnectionPool_Accessor . 这里唯一的通用类是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. 感谢Pavel的评论“基本上,你的问题是C#是一种静态类型的语言”,我现在知道Ruby已经破坏了我。 ;) ;)

Generic types are evaluated at compile time , not in runtime . 通用类型在编译时进行评估,而不是在运行时进行评估。 Since it cannot be determined at runtime time what type1 will be, this construct is not allowed. 由于无法在运行时确定type1将是什么,因此不允许使用此构造。

This is actually what Resharper says: type1 is not a Type, it's a variable of the type Type , (just as it could be an object of the type String ). 其实,这是什么ReSharper的说: type1是不是一个类型,它的类型的变量Type ,(就像它可能是类型的对象String )。

First of all, ReSharper is actually correct. 首先,ReSharper实际上是正确的。 It isn't a type , it's a variable . 它不是一种类型 ,它是一个变量 Granted, it's a variable that is holding the reflection object that corresponds to a type, but that isn't enough. 当然,它是一个变量,它持有与一个类型相对应的反射对象,但这还不够。

Between the <...> brackets, you have to write the name of a type, not the name of any other identifier. 在<...>括号之间,您必须写入类型的名称,而不是任何其他标识符的名称。

You can construct generic objects through reflection, however, and access their properties, even static ones, so you should be able to rewrite the code to do that, however, have you looked at NUnit 2.5? 但是,您可以通过反射构建通用对象,并访问它们的属性,甚至是静态属性,因此您应该能够重写代码来执行此操作,但是,您是否查看过NUnit 2.5?

From the latest release notes, it appears that unit test classes can now be generic, and you can specify with an attribute on the test class which types to test it with. 从最新的发行说明中可以看出,单元测试类现在可以是通用的,您可以在测试类中使用属​​性指定要测试它的类型。

This would allow you to write something like this (note, I have not tested this, I only looked up 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);
    }
}

The TestFixture attribute should set you up, but just for the heck of it: if you wanted to do all this at runtime, you could do so using reflection. TestFixture属性应该设置你,但只是为了它:如果你想在运行时完成所有这些,你可以使用反射。

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

You could then proceed to do whatever you want using reflection on poolType . 然后,您可以使用poolType反射继续执行任何poolType Of course, that will be a major pain in the butt unless you use C# 4.0 dynamic typing. 当然,除非你使用C#4.0动态类型,否则这将是一个主要的痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM