如果A< T1,T2>是实际类型的模板,那么为什么typeof运算(A<,>)允许吗? [英] If A<T1,T2> is a template for actual type, then why is typeof(A<,>) allowed?

查看:114
本文介绍了如果A< T1,T2>是实际类型的模板,那么为什么typeof运算(A<,>)允许吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Program
{
    static void Main(string[] args)
    {
        Type t = typeof(A<,>);
        Console.WriteLine(typeof(A<,>)); // prints A'2[T1,T2]
    }    
}

class A<T1,T2>
{

}



据我所知,通用键入 A< T1,T2> 不是一个实际的类型,而是一个蓝图/模板的实际类型,所以为什么的typeof 接受它作为参数(因为据我所知,的typeof 接受作为参数的实际类型)?

As far as I know, generic type A<T1, T2> is not an actual type, but rather a blueprint/template for an actual type, so why does typeof accept it as a parameter (since as far as I know, typeof accepts as parameter actual types)?

感谢您

推荐答案

反射的的未构造的泛型类型,比如 C<> 被混为一谈用的实例类型 C< T>

In reflection the unconstructed generic type, say C<> is conflated with the instance type C<T>.

这是也许比纯粹的理论少;我觉得这些非常不同的实体。我认为,一个作为的符号的C一个类型参数,另一为的编译时类型℃下的; T> 。在代码中, C<> C< T> 不是彼此的同义词;可以使后一种类型的字段,如果T为范围,但你永远不能让前者类型的字段。

This is perhaps less than theoretically pure; I think of these as very different entities. I think of one as the symbol "C with one type parameter" and the other as a compile time type C<T>. In code, C<> and C<T> are not synonyms for each other; you can make a field of the latter type if T is in scope, but you cannot ever make a field of the former type.

这反射库给你的类型当你问的未构造类型的实例类型的并不全是坏事,但。请问您的的与未构造的类型?没有什么你真的可以用它做。但与该实例的类型,你可以说,在这里替代int作为T

That the reflection library gives you the type of the instance type when you ask for the unconstructed type is not all bad though. What would you do with the unconstructed type? There's nothing you really can do with it. But with the instance type, you can say "substitute int for T" in here.

得到的真正好处 C< T> 当你问的typeof(C<>)是,这总是给你未构造类型。比较:

The real benefit of getting C<T> when you ask for typeof(C<>) is that this always gives you the unconstructed type. Compare:

class C<T>
{
    public static Type CT() { return typeof(C<T>); }
    public static Type JustC() { return typeof(C<>); }
}

当你打电话CT,你有什么打算叫它的的?有没有键入 C< T> 来调用CT上。您可以拨打 C< INT> .CT 在这种情况下,你会得到 C< INT> ,而不是 C< T> 。只有这样,才能获得类型 C<> 和T构造是要求的typeof(C<>)

When you call CT, what are you going to call it on? There's no type C<T> to call CT on. You can call C<int>.CT in which case you'll get back C<int>, not C<T>. The only way to get the type C<> constructed with T is to ask for typeof(C<>).

这是否有道理?

我还没有覆盖反射(我它是什么一个模糊的概念)

I haven't yet covered reflection ( I have a vague idea of what it is )

反思是代码简单地库,使您可以获取有关你的代码信息代码本身。 typeof运算给你一个Type对象,然后您可以反射时,以了解类型的信息。

Reflection is simply the library of code that allows you to get information about your code in the code itself. "typeof" gives you a Type object which you can then "reflect" upon to find out information about the type.

我不相信我明白你的意思,即使T是不在范围内

I'm not sure I understand what you mean by "even when T is not in scope"

我说,这是令人困惑的方式。我改写了。

The way I said it was confusing. I've rephrased it.

这篇关于如果A&LT; T1,T2&GT;是实际类型的模板,那么为什么typeof运算(A&LT;,&GT;)允许吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆