none
Type.GetType returns null, when trying to resolve a HashSet type RRS feed

  • Question

  • Why is it that for some strange reason, I cannot get Type.GetType to work with HashSets ?

    This code, shows Type.getType returning false, eventhough the fullName is 100% correct

                HashSet<String> set = new HashSet<String>();
                System.Console.WriteLine("Resolved : " +Type.GetType(set.GetType().FullName));

    Any ideas ?

    Wednesday, February 1, 2012 3:45 PM

Answers

  • Hi,

    the documentation simply states, that Type.GetType needs a assembly qualified name and not the full name.

    So you can change the code to use set.GetType().AssemblyQualifiedName instead.

    The difference is also quite easy to see:

    FullName:
    System.Collections.Generic.HashSet`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
    AssemblyQualifiedName:
    System.Collections.Generic.HashSet`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

    http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx
    http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx

    With kind regards,

    Konrad

    • Marked as answer by Paul Zhou Thursday, February 16, 2012 8:53 AM
    Wednesday, February 1, 2012 4:02 PM

All replies

  • Hi,

    the documentation simply states, that Type.GetType needs a assembly qualified name and not the full name.

    So you can change the code to use set.GetType().AssemblyQualifiedName instead.

    The difference is also quite easy to see:

    FullName:
    System.Collections.Generic.HashSet`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
    AssemblyQualifiedName:
    System.Collections.Generic.HashSet`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

    http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx
    http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx

    With kind regards,

    Konrad

    • Marked as answer by Paul Zhou Thursday, February 16, 2012 8:53 AM
    Wednesday, February 1, 2012 4:02 PM
  • Hi Konrad.

    That works, my problem is that I have to get the Type based on a String the user can supply.

    The strange thing is that getType works for

    System.Collections.Generic.Dictionary`2[System.String,System.String]
    System.Collections.Generic.List`1[System.String]

    So maybee it's just plain luck

    Wednesday, February 1, 2012 4:45 PM