【发布时间】:2016-08-31 16:04:01
【问题描述】:
据我所知,当 C# 中第一次调用类型时,CLR 找到此类型并为此类型创建对象类型,其中包含类型对象指针、同步块索引器、静态字段、方法表(第 4 章中的更多信息'CLR via C#' 书)。好的,一些泛型类型具有静态泛型字段。我们为这些字段设置值
GenericTypesClass<string, string>.firstField = "firstField";
GenericTypesClass<string, string>.secondField = "secondField";
一次又一次
GenericTypesClass<int, int>.firstField = 1;
GenericTypesClass<int, int>.secondField = 2;
之后在堆上创建了两种不同的对象类型还是没有?
这里有更多例子:
class Simple
{
}
class GenericTypesClass<Type1,Type2>
{
public static Type1 firstField;
public static Type2 secondField;
}
class Program
{
static void Main(string[] args)
{
//first call GenericTypesClass, create object-type
Type type = typeof (GenericTypesClass<,>);
//create new object-type GenericTypesClass<string, string> on heap
//object-type contains type-object pointer,sync-block indexer,static fields,methods table(from Jeffrey Richter : Clr Via C#(chapter 4))
GenericTypesClass<string, string>.firstField = "firstField";
GenericTypesClass<string, string>.secondField = "secondField";
//Ok, this will create another object-type?
GenericTypesClass<int, int>.firstField = 1;
GenericTypesClass<int, int>.secondField = 2;
//and another object-type?
GenericTypesClass<Simple,Simple>.firstField = new Simple();
GenericTypesClass<Simple, Simple>.secondField = new Simple();
}
}
【问题讨论】: