1

Let's say I want to log/show this type's name:

Dictionary<string, Dictionary<Guid, DateTime>>

What would be preferred:

1) Dictionary`2`string`Dictionary`2`Guid`DateTime

2) Dictionary`2(string, Dictionary`2(Guid, DateTime))

3) Dictionary< string , Dictionary < Guid, DateTime>>

How does JVM solve this?

Also see: https://stackoverflow.com/a/2448918/486561

Community
  • 1
  • 1
Den
  • 1,827
  • 3
  • 25
  • 46

1 Answers1

1

It is a little long, but the typeof(Dictionary<string, Dictionary<Guid, DateTime>>).FullName is:

System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.Dictionary`2[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

The typeof() operator returns a RuntimeType, that is a .NET class. So the FullName is a property of a type of .NET, so it's official :-)

If you try to do a Type.GetType(thatstring), you obtain the correct type :-)

A more compact version, without assembly shortnames, can be obtained with: typeof(Dictionary<string, Dictionary<Guid, DateTime>>).ToString(). This too can be used with Type.GetType(...)

System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.Dictionary`2[System.Guid,System.DateTime]]

Note that there is a limitation in Type.GetType with this format:

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

If you are interested, here there is the full format: AssemblyQualifiedName

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Sorry, I never mentioned but I want a "friendly" name :). What do you think if I just strip the full qualifications: Dictionary\`2[String,Dictionary\`2[Guid,DateTime]] – Den Mar 23 '15 at 10:25
  • @Den I think the namespaces are still important :-) – xanatos Mar 23 '15 at 10:27
  • That's true, but when you do typeof(...).Name you don't get namespaces. I want the best of both worlds :). It's more for logging, rather than dynamic instantiation. – Den Mar 23 '15 at 10:28
  • @Den Now, if you want to describe univocally a type, the namespace and assembly name are necessary. If you want to give a "general idea", then you can surely strip them. Perhaps using a Regex. – xanatos Mar 23 '15 at 10:30
  • Thanks, by the way. A very quick, straightforward and thorough answer. – Den Mar 23 '15 at 10:30
  • @Den The regex could be something like `new Regex(@"[^[,]*\.")` (it should strip all the namespaces) (you use it starting with the `Type.ToString()` version) – xanatos Mar 23 '15 at 10:34