简体   繁体   English

.NET中如何比较类型? CLR如何确定它们相同或不同?

[英]How are types compared in .NET? How does the CLR identify they are the same or different?

How does the Type.Equals(Type) compare types to know if they are the same or not? Type.Equals(Type)如何比较类型以了解它们是否相同?

The original question was being confused because of what I had here in the body as what I thought would direct the question. 最初的问题很困惑,因为我在体内的存在以及我认为将指导该问题的内容。 Forget that part. 忘记那部分。 The question is in the title. 问题在标题中。

EDIT: Lasse V. Larksen posed my question best: 编辑:拉瑟五世·拉克森最恰当地提出了我的问题:

"What makes up the identity of a .NET type"..."If I declare the exact same type, down to the namespace, in two different projects/assemblies, .NET consider them different, why?" “组成.NET类型标识的是什么……”如果在两个不同的项目/程序集中,我声明完全相同的类型(直到名称空间),. NET认为它们是不同的,为什么?

It's implemented like this: (from the reference source) 它是这样实现的:(来自参考源)

public virtual bool Equals(Type o)
{ 
    if ((object)o == null) 
        return false;

    return (Object.ReferenceEquals(this.UnderlyingSystemType, o.UnderlyingSystemType));
}
public override bool Equals(object o)
{
    return (((o != null) && (o is Type)) && (this.UnderlyingSystemType == ((Type) o).UnderlyingSystemType));
}

This is from the latest reflected source. 这来自最新的反映来源。

ECMA-335 Common Language Infrastructure Partitions I and II dictate that each type will be known by a given type signature . ECMA-335公共语言基础结构分区I和II规定每种类型都可以通过给定的类型签名来识别 It states how IL represents this signature and how the Virtual Execution System should interpret the signature, but it leaves how a conformant runtime implementation must actually implement this type signature up to the implementation (as far as I can tell). 它说明了IL如何表示此签名,以及虚拟执行系统应如何解释该签名,但它保留了一致的运行时实现实际上必须如何实现类型签名的实现(据我所知)。

New types—value types and reference types—are introduced into the CTS via type declarations expressed in metadata. 通过元数据中表示的类型声明将新类型(值类型和引用类型)引入CTS。 In addition, metadata is a structured way to represent all information that the CLI uses to locate and load classes, lay out instances in memory, resolve method invocations, translate CIL to native code, enforce security, and set up runtime context boundaries. 此外,元数据是一种结构化的方式,表示CLI用来定位和加载类,在内存中布置实例,解析方法调用,将CIL转换为本机代码,实施安全性以及设置运行时上下文边界的所有信息。

However, to your question, regardless of how UnderlyingSystemType is actually implemented it will correlate to a unique reference using the containing: 但是,对于您的问题,无论实际上如何实现UnderlyingSystemType它都将使用以下内容与唯一引用相关联:

  • Assembly 部件
  • Module 模组
  • Namespace 命名空间
  • Parent Types (if any) 父类型(如果有)
  • The Type itself 类型本身

Partition II has the physical metadata for a TypeDef (section 22.37) which encodes this information. 分区II具有用于TypeDef的物理元数据(第22.37节),该元数据对该信息进行了编码。

Partition III contains the IL which represents loading a metadata token, known as a RuntimeHandle onto the stack. 分区III包含IL,该IL表示将元数据令牌(称为RuntimeHandle到堆栈上。 There are different handles for types, methods, and fields depending on the token given to the IL instructions. 类型,方法和字段有不同的句柄,具体取决于提供给IL指令的令牌。

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

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