[英]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时静态编译的那些“等同”。
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.