简体   繁体   English

.NET运行时如何确定两种类型是否相同?

[英]How does the .NET runtime determine that two types are the same?

I have assembly A that depends (statically) on type T (reference type, a class) in assembly B. 我有程序集A,它(静态地)依赖于程序集B中的类型T(引用类型,类)。

I do not own assembly A but I do own assembly B. T unfortunately is a real type (not an interface) but luckily A uses reflection to discover its members. 我不拥有程序集A但我拥有程序集B.不幸的是,它是一个真正的类型(不是接口)但幸运的是A使用反射来发现它的成员。

I want to be able to create dynamically B (and T). 我希望能够动态创建B(和T)。 The only important item is that I need to be sure that the "B and T" dynamically created (not their members) are "equivalent" tho those that were statically compiled when A was compiled. 唯一重要的项目是我需要确保动态创建的“B和T”(不是它们的成员)与编译A时静态编译的那些“等同”。

  1. I can use reflection Emit to create B and T. No problem. 我可以使用反射Emit来创建B和T.没问题。
  2. I can write B.dll to disk 我可以把B.dll写到磁盘上
  3. If A was compiled against this B.dll then I can dynamically change T, no problem 如果A是针对这个B.dll编译的,那么我可以动态地改变T,没问题
  4. However if A was compiled against a equivalent (hand written B and T) then the T' in A would have the same assembly name and type name but it would not be considered equal to the one dynamically created. 但是,如果A是针对等价物(手写的B和T)编译的,则A中的T'将具有相同的程序集名称和类型名称,但不会被视为等于动态创建的那个。

I assume that the reason is the GUID property on the type. 我假设原因是该类型的GUID属性。 When I write B.dll to disk the T type has the same GUID than the one I can create dynamically (even if I change its members). 当我将B.dll写入磁盘时,T类型具有与我可以动态创建的GUID相同的GUID(即使我更改其成员)。 However the "otherwise equivalent" hand-written type does not share the same GUID. 但是,“其他等效”手写类型不共享相同的GUID。

In summary the GUID is the same if the dll comes from the dynamically created but is different from the one statically created. 总之,如果dll来自动态创建但与静态创建的不同,则GUID是相同的。 The GUID is the same every time (even if I recompile). 每次GUID都是一样的(即使我重新编译)。 The GUID is the same if I change the content of the dynamic Type. 如果我更改动态类型的内容,GUID是相同的。

How does the runtime determine if two types are the same? 运行时如何确定两种类型是否相同? How is this GUID created? 这个GUID是如何创建的? It uses this GUID and/or something else? 它使用这个GUID和/或其他东西? Is it possible to assign this GUID in my dimamic assembly to match the one in assembly A? 是否可以在我的dimamic程序集中分配此GUID以匹配程序集A中的GUID?

Can this GUID be examined statically (I was not able to see it with Reflector or ildasm). 可以静态检查此GUID(我无法使用Reflector或ildasm查看它)。

NOTE: Assume assemblies are not signed. 注意:假设程序集未签名。

The .NET runtime uses the assembly name, version, strong name (if present), and in some cases the culture to locate referenced assemblies. .NET运行时使用程序集名称,版本,强名称(如果存在),在某些情况下使用文化来查找引用的程序集。 If an assembly A references another assembly B it is possible to replace B with another assembly if the mentioned properties are exactly the same. 如果组件A引用另一个组件B,则如果提到的属性完全相同,则可以用另一个组件替换B. If B has a strong name you can only create another assembly with the same strong name if you own the key file that was used to sign the assembly. 如果B具有强名称,则只有拥有用于对程序集进行签名的密钥文件,才能创建具有相同强名称的另一个程序集。

AFAIK, the Type.GUID property is only used when your types are registered for COM use. AFAIK,Type.GUID属性仅在您的类型注册用于COM时使用。 It works with the GuidAttribute class. 它适用于GuidAttribute类。 Hence, Type.GUID has nothing to do with the "identity" of a type. 因此,Type.GUID与类型的“身份”无关。

You can read more about how the runtime locates assemblies here: http://msdn.microsoft.com/en-us/library/yx7xezcf.aspx 您可以在此处详细了解运行时如何查找程序集: http//msdn.microsoft.com/en-us/library/yx7xezcf.aspx

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

相关问题 如何在.NET下的C / C ++中确定运行时的方法参数类型? - How to determine method parameter types during runtime in C/C++ under .NET? .NET中如何比较类型? CLR如何确定它们相同或不同? - How are types compared in .NET? How does the CLR identify they are the same or different? .Net两种不同组件中的相同类型 - .Net same types in two different assemblies .NET如何解析类型? - How does .NET resolve types? 如何在运行时确定.NET列表视图的尺寸? - How do I determine the dimensions of a .NET listview at runtime? 如何确定 WiX 安装所需的 .NET Core 3.1 运行时文件 - How to determine .NET Core 3.1 runtime files needed for WiX install 如何从2个不同的程序集加载相同的插件,并在两者之间序列化和反序列化类型? C#.net - How to load same plugins from 2 different assemblies, and serializing and deserializing types between the two? C# .net 是否可以在同一进程中加载​​两个版本的.NET运行时? - Is it possible to load two versions of the .NET runtime in the same process? .NET如何确定角色的Unicode类别? - How does .NET determine the Unicode category of a character? .net如何确定“ foreach”循环的顺序? - How does .net determine the order for the “foreach” loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM