My blog about computing

Published or updated on the : 2013-07-22

A parser for AssemblyQualifiedName for .NET (C#)

Source code is available here.
Update code should be available, part of UniversalSerializer.

What is it ?

ParsedAssemblyQualifiedName is a simple parser for type.AssemblyQualifiedName for .NET.

It is a fact the syntax of AssemblyQualifiedName is rather complex.
While I am working on a new version of UniversalSerializer, I need such a parser and I am unable to find one on the Internet.
So here is a class that may help.

Summary

As I said, the syntax of AssemblyQualifiedName is rather complex, especially with generics.

An example:

Dictionary<int, List<double>>

has this AssemblyQualifiedName:

System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List`1[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Not very comprehensible!

ParsedAssemblyQualifiedName will extract these informations:

  • AssemblyDescriptionString
    This is the Assembly description part of the AssemblyQualifiedName.

    mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
  • AssemblyNameDescriptor
    An AssemblyName generated from the AssemblyQualifiedName.
    It is Lazy.

  • CSharpStyleName
    A C#-style type name.
    It is Lazy.

    System.Collections.Generic.Dictionary<System.Int32, System.Collections.Generic.List<System.Double>>
  • Culture
    Culture string of the Assembly.

    neutral
  • FoundType
    Creates the Type corresponding to the AssemblyQualifiedName, if available.
    It is Lazy.

  • GenericParameters
    Gets a list of ParsedAssemblyQualifiedName, one for each generic parameter.
    Obviously, It is recursive.

  • PublicKeyToken
    The Token of the Assembly.

    b77a5c561934e089
  • ShortAssemblyName

    mscorlib
  • TypeName
    The first part of the AssemblyQualifiedName, without the Assembly information.

    System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List`1[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
  • VBNetStyleName
    A VB-style type name.
    It is Lazy.

    System.Collections.Generic.Dictionary(Of System.Int32, System.Collections.Generic.List(Of System.Double))
  • Version
    Version of the Assembly, as a string.

    4.0.0.0

Note

Please note ParsedAssemblyQualifiedName can analyse a type description even if the type does not exist or if it lays in a not-accessible library.
In that case, only ‘FoundType’ will not produce anything (it will return ‘null’).

Example of usage

var parsed = new ParsedAssemblyQualifiedName(typeof(Dictionary<int, List<double>>).AssemblyQualifiedName);
var t = parsed.FoundType.Value;
var an = parsed.AssemblyNameDescriptor.Value;
var cs = parsed.CSharpStyleName.Value;
var vb = parsed.VBNetStyleName.Value;

Conclusion

I hope this small class will be useful to many.

Future enhancements will be part of UniversalSerializer.