25

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
blachniet
  • 4,323
  • 5
  • 33
  • 34
  • I think that that GUID is not being generated at all, but was generated once and therefore it's always the same. – Rhapsody Apr 13 '11 at 13:18

2 Answers2

16

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.

Ani
  • 111,048
  • 26
  • 262
  • 307
  • Thanks. I was trying to avoid explicitly defining the GUID, but it seems as though that will be the safest route. – blachniet Apr 13 '11 at 13:33
  • 2
    If you are curious, [here is the code in the CLR](https://github.com/dotnet/coreclr/blob/7f7d8276e65059a37001aaa5ce9af7e82249f4a0/src/vm/methodtable.cpp#L7377) that Type.GUID is forwarding on to. – Scott Chamberlain Jul 29 '16 at 20:06
0

It's not documented so i wouldn't completly rely on them, said that the implementation may or may not change in the future.

ba__friend
  • 5,783
  • 2
  • 27
  • 20