Click here to Skip to main content
15,891,253 members
Articles / Web Development / HTML
Tip/Trick

AssemblyQualifiedName Parser

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
22 Jul 2013Ms-PL1 min read 28.5K   381   7   5
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:

C#
Dictionary<int, List<double>>

has this AssemblyQualifiedName:

C#
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

C#
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

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) independent
France France
Hi !

I made my first program on a Sinclair ZX-81.
Since that day, I have the virus of computers. Smile | :)

Here is my website:
https://chrisbertrand.net

And my blog:
https://chrisbertrandprogramer.wordpress.com/

Comments and Discussions

 
SuggestionA little improvement... Pin
Filippo Bottega17-Feb-15 3:58
Filippo Bottega17-Feb-15 3:58 
GeneralRe: A little improvement... Pin
Christophe Bertrand20-Feb-15 6:55
Christophe Bertrand20-Feb-15 6:55 
GeneralMy vote of 3 Pin
Member 1092263116-Jan-15 9:11
Member 1092263116-Jan-15 9:11 
AnswerRe: My vote of 3 Pin
Christophe Bertrand29-Jan-15 23:39
Christophe Bertrand29-Jan-15 23:39 
GeneralMy vote of 5 Pin
Member 767332830-Sep-13 17:10
Member 767332830-Sep-13 17:10 
Great job !! This worked perfectly for coordinating my Type Builder class which manually creates runtime instances of complex nested generic types that can span multiple MEF assemblies. Thanks !!

I added a new property that that was useful for my purposes:

C#
this.GenericPrefix = this.TypeName.Split('[')[0];


The GenericPrefix returns the correct format for creating open generic types. For example:

C#
Tuple<int, string, long>


returns a generic prefix of "System.Tuple`3" which can be used with the Type.MakeGenericType method to manually assign the generic parameter types, which in my case could be in other assemblies.

Using the AssemblyQualifiedName parser, I was able to recursively analyze the generic parameters and use Assembly.GetType() to piece together the nested generic types from each assembly. Awesome !!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.