Created
November 8, 2012 18:50
-
-
Save barbeque/4040723 to your computer and use it in GitHub Desktop.
Turning C# generic Type back into "C# Type" declaration style.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Reflection.Emit; | |
| using System.Text; | |
| using System.Reflection; | |
| namespace GenericCodeGenerator | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Type f = typeof (IEnumerable<string>); | |
| Console.WriteLine("ToString {0}", f.ToString()); | |
| // Strip namespace | |
| string typeName = f.FullName.Replace(f.Namespace + ".", ""); | |
| // Get the codedom generator for C# ready | |
| var cdProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp"); | |
| // Create the reference object pointing to this type | |
| var reference = new System.CodeDom.CodeTypeReference(typeName); | |
| // Boom! | |
| string csTypeName = cdProvider.GetTypeOutput(reference); | |
| Console.WriteLine("CodeDom for C# {0}", csTypeName); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment