C# 泛型类型上下文中的类型标识是什么?
What is type identity in the context of the C# generic types?
C# 泛型类型上下文中的类型标识是什么?
我正在阅读 CLR via C# 书中的章节。我遇到了 Generic Type Identity.
的概念
看来大家看完章节应该清楚什么是类型标识了。但是我不知道它是什么。
书中有例子:
internal sealed class DateTimeList : List<DateTime> {
// No need to put any code in here!
}
...you lose type identity and equivalence, as you can
see in the following code:
Boolean sameType = (typeof(List<DateTime>) == typeof(DateTimeList));
从那个例子中我可以清楚地看到类型等价是什么(它是类型对象的相等性)。虽然我无法获得类型标识所指的内容。
那么,有人可以解释一下类型标识概念吗?
没有什么特别之处 并且作者没有仅在 [=44= 的上下文中使用该词]通用类型。
当您第一次使用某种类型时,会在堆上创建该类型的实例,它包含与类型相关的信息。我想您已经意识到,堆上的每个对象都需要一些额外的成员。其中一个叫做type object pointer。该指针指向存储在堆上的相应 Type
对象。通过 Type Identity,作者的意思是对 Type
对象的引用是否指向堆中的同一个对象。
在那种情况下,类型的标识是不同的,因为借助 typeof
获取的对象是存储在堆中的不同对象。 DateTimeList
和List<DateTime>
的Type
对象是两个不同的对象。这就是 sameType
为假的原因。
internal sealed class DateTimeList : List<DateTime> {
// No need to put any code in here!
}
但是,在那种情况下,类型的标识是相同的。目前,DateTimeList
只是一个别名。在编译代码时,编译器会将所有 DateTimeList
替换为 List<System.DateTime
.
using DateTimeList = System.Collections.Generic.List<System.DateTime>;
例如,在那种情况下
Boolean sameType = (typeof(List<DateTime>) == typeof(DateTimeList));
将被编译器替换为:
// obvious that, the result is true
Boolean sameType = (typeof(List<DateTime>) == typeof(List<DateTime));
顺便说一句,作者也在该代码块的范围内使用了 Type Identity
:
// Define a type, construct an instance of it, & initialize its properties
var o1 = new { Name = "Jeff", Year = 1964 };
var people = new[] {
o1,
new { Name = "Kristin", Year = 1970 },
new { Name = "Aidan", Year = 2003 },
new { Name = "Grant", Year = 2008 }
};
他提到:
This top line of code (o1
) creates an anonymous type because I did
not specify a type name after the new keyword, so the compiler will
create a type name for me automatically and not tell me what it is
(which is why it is called an anonymous type).
The compiler is very intelligent about defining anonymous types. If
the compiler sees that you are defining multiple anonymous types (3 new objects inside array) in
your source code that have the identical structure, the compiler will
create just one definition for the anonymous type and create multiple
instances of that type.
Because of this type identity we can create an implicitly typed array
of anoymous types. This works because all of the objects are of the
same anonymous type.
C# 泛型类型上下文中的类型标识是什么?
我正在阅读 CLR via C# 书中的章节。我遇到了 Generic Type Identity.
的概念看来大家看完章节应该清楚什么是类型标识了。但是我不知道它是什么。
书中有例子:
internal sealed class DateTimeList : List<DateTime> {
// No need to put any code in here!
}
...you lose type identity and equivalence, as you can see in the following code:
Boolean sameType = (typeof(List<DateTime>) == typeof(DateTimeList));
从那个例子中我可以清楚地看到类型等价是什么(它是类型对象的相等性)。虽然我无法获得类型标识所指的内容。
那么,有人可以解释一下类型标识概念吗?
没有什么特别之处 并且作者没有仅在 [=44= 的上下文中使用该词]通用类型。
当您第一次使用某种类型时,会在堆上创建该类型的实例,它包含与类型相关的信息。我想您已经意识到,堆上的每个对象都需要一些额外的成员。其中一个叫做type object pointer。该指针指向存储在堆上的相应 Type
对象。通过 Type Identity,作者的意思是对 Type
对象的引用是否指向堆中的同一个对象。
在那种情况下,类型的标识是不同的,因为借助 typeof
获取的对象是存储在堆中的不同对象。 DateTimeList
和List<DateTime>
的Type
对象是两个不同的对象。这就是 sameType
为假的原因。
internal sealed class DateTimeList : List<DateTime> {
// No need to put any code in here!
}
但是,在那种情况下,类型的标识是相同的。目前,DateTimeList
只是一个别名。在编译代码时,编译器会将所有 DateTimeList
替换为 List<System.DateTime
.
using DateTimeList = System.Collections.Generic.List<System.DateTime>;
例如,在那种情况下
Boolean sameType = (typeof(List<DateTime>) == typeof(DateTimeList));
将被编译器替换为:
// obvious that, the result is true
Boolean sameType = (typeof(List<DateTime>) == typeof(List<DateTime));
顺便说一句,作者也在该代码块的范围内使用了 Type Identity
:
// Define a type, construct an instance of it, & initialize its properties
var o1 = new { Name = "Jeff", Year = 1964 };
var people = new[] {
o1,
new { Name = "Kristin", Year = 1970 },
new { Name = "Aidan", Year = 2003 },
new { Name = "Grant", Year = 2008 }
};
他提到:
This top line of code (
o1
) creates an anonymous type because I did not specify a type name after the new keyword, so the compiler will create a type name for me automatically and not tell me what it is (which is why it is called an anonymous type).The compiler is very intelligent about defining anonymous types. If the compiler sees that you are defining multiple anonymous types (3 new objects inside array) in your source code that have the identical structure, the compiler will create just one definition for the anonymous type and create multiple instances of that type.
Because of this type identity we can create an implicitly typed array of anoymous types. This works because all of the objects are of the same anonymous type.