获取类型的GUID [英] Get type from GUID
问题描述
由于种种原因,我需要实现在C#中的类型缓存机制。幸运的是,CLR提供 Type.GUID
来唯一标识一个类型。不幸的是,我无法找到任何方式来查找基于此GUID的类型。有 Type.GetTypeFromCLSID()
但根据我的理解的文档(和实验)中,做一些非常,非常不同的。
For various reasons, I need to implement a type caching mechanism in C#. Fortunately, the CLR provides Type.GUID
to uniquely identify a type. Unfortunately, I can't find any way to look up a type based on this GUID. There's Type.GetTypeFromCLSID()
but based on my understanding of the documentation (and experiments) that does something very, very different.
有没有什么办法让基础上,通过对所有类型的负载循环和比较他们的GUID的GUID短类型?
Is there any way to get a type based on its GUID short of looping through all the loaded types and comparing to their GUIDs?
修改:我忘了提,我真的像一个型指纹固定宽度的,这就是为什么GUID是如此吸引我。当然,在一般情况下,该类型的全名是可行的。
EDIT: I forgot to mention that I would really like a "type fingerprint" of fixed width, that's why the GUID is so appealing to me. In a general case, of course, the fully qualified name of the type would work.
推荐答案
不要循环比较。填充词典<类型>
并使用包含
方法
Don't loop to compare. Populate a Dictionary<Type>
and use the Contains
method.
Dictionary<Type> types = new Dictionary<Types>();
... //populate
if (types.Contains(someObject.GetType()))
//do something
这必将给你一个固定大小的入口,因为所有的人都将是对象引用(类型的实例本质上是工厂对象)。
This will certainly give you a fixed size entry, since all of them will be object references (instances of Type essentially being factory objects).
这篇关于获取类型的GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!