什么是Type.GUID,它与Type.Equals()有什么关系? [英] What is Type.GUID and how does it relate to Type.Equals()?
问题描述
在尝试将 System.RuntimeType
的实例与泛型类型 TOut
进行比较时,我遇到了一些有趣的行为。 :
类型runtimeT = methodInfo.ReturnType; //使用反射获得RuntimeType
类型genericT = typeof(TOut);
//此条件失败,因为runtimeT不
//似乎包含程序集限定名
if(runtimeT.Equals(genericT)){...}
以下是我的证据:
免责声明:我不确切知道CLR / type-system中的GUID是什么,当然除了首字母缩写代表全局唯一标识符。也许这个名字误导了我。
假设:我在这里假设类型
GUID唯一标识完全限定类型,包括 <$ ( null $>)中缺少
的c $ c> AssemblyQualifiedName c $ c> value。)
我的假设是错误的吗? 是:类型GUID真正代表什么,以及它如何使用?
Equals()
是通过比较GUID来实现的吗?
原因之一是 Type.GUID
Equals
的比较是它是用户可控的项目。例如,我可以通过执行以下操作来指定我的接口
的 GUID
[Guid(2bfd006d-94b9-43af-843f-5b32f7567086)]
interface IFoo {...}
为了生成COM互操作接口,这是必需的。如果 Equals
依赖于类型所特有的 GUID
,那么给定的开发人员可能会给他们的系统造成严重破坏键入相同的 GUID
,例如 string
, int
等等...
I came across some interesting behavior while trying to compare an instance of System.RuntimeType
with a generic type TOut
:
Type runtimeT = methodInfo.ReturnType; // get RuntimeType using reflection
Type genericT = typeof(TOut);
// This condition fails because runtimeT doesn't
// seem to include an assembly qualified name
if(runtimeT.Equals(genericT)) { ... }
Here is my evidence:
Disclaimer: I don't know precisely what a GUID is in the context of the CLR / type-system, except of course that the acronym stands for global unique identifier. Perhaps the name is misleading me.
Assumption: I'm assuming here that aType
GUID uniquely identifies the fully qualified type, including theAssemblyQualifiedName
that's missing from factoryInfo.ReturnType
in the screenshot (the null
value.)
Is my assumption wrong?
Yes: What does type GUID truly represent, and how is it used?
No: Why wouldn't
Equals()
be implemented by comparing GUID?
One reason that Type.GUID
isn't used as the comparison for Equals
is that it's user controllable item. For example i can dictate the GUID
of my interface
by doing the following
[Guid("2bfd006d-94b9-43af-843f-5b32f7567086")]
interface IFoo { ... }
This is necessary in order to produce interfaces for COM interop. If Equals
relied on the GUID
being unique to a type then a given developer could wreak havoc on the system by giving their type the same GUID
as say string
, int
, etc ...
这篇关于什么是Type.GUID,它与Type.Equals()有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!