65.9K
CodeProject is changing. Read more.
Home

AssemblyQualifiedName Parser

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (3 votes)

Jul 22, 2013

Ms-PL

1 min read

viewsIcon

29485

downloadIcon

389

A parser for AssemblyQualifiedName for .NET

What Is It ?

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

It is a fact that 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 in 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 information:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

System.Collections.Generic.Dictionary<System.Int32, System.Collections.Generic.List<System.Double>>

neutral

b77a5c561934e089

mscorlib

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]]

System.Collections.Generic.Dictionary(Of System.Int32, System.Collections.Generic.List(Of System.Double))

4.0.0.0
  • AssemblyDescriptionString: This is the assembly description part of the AssemblyQualifiedName.
  • AssemblyNameDescriptor: An AssemblyName generated from the AssemblyQualifiedName. It is Lazy.
  • CSharpStyleName: A C#-style type name. It is Lazy.
  • Culture: Culture string of the Assembly.
  • 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.
  • ShortAssemblyName
  • TypeName: The first part of the AssemblyQualifiedName, without the Assembly information.
  • VBNetStyleName: A VB-style type name. It is Lazy.
  • Version: Version of the Assembly, as a string.

Note

Please note ParsedAssemblyQualifiedName can analyse a type description even if the type does not exist or if it lays in an inaccessible 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.

History

  • 2015-01-28 Correction for array types