是自动生成的GUID在.NET一致的类型?自动生成、类型、GUID、NET

2023-09-04 00:06:29 作者:LOVEの用心許諾灬

是自动生成的GUID C#的类型是否一致?例如,如果我得到一个GUID为我的接口,IFoo的(的typeof(的IFoo).GUID ),第一次运行程序,我会得到相同的GUID每次我运行程序?

Are the automatically generated GUIDs for C# Types consistent? For example, if I get a GUID for my interface, IFoo (typeof(IFoo).GUID), the first time a run the program, will I get that same GUID everytime I run the program?

我有我的本地机器上测试过它,它似乎永远是相同的,但我不知道我是否可以依靠它始终是相同的跨机器。

I have tested it locally on my machine, and it seems to always be the same, but I'm not sure if I can rely on it always being the same across machines.

推荐答案

请参阅:的 System.Type.GUID稳定。看来,该方法的当前实现依赖于一个由CLR本身实现的内部通话。除非微软明确了合同自动生成的GUID(在Type.GUID文档在这个问题上目前沉默),我不会依靠观察到的行为。可以肯定的约一致性,我建议明确装修类型与GuidAttribute.

See: System.Type.GUID stability. It appears that the current implementation of the method relies an internal call that is implemented by the CLR itself. Unless Microsoft clarifies the contracts for the auto-generated Guids (the Type.GUID documentation is currently silent on this issue), I wouldn't rely on the observed behaviour. To be sure about consistency, I recommend explicitly decorating the types in question with the GuidAttribute.