How can I get the correct text definition of a generic type using reflection?

前端 未结 6 1335
萌比男神i
萌比男神i 2020-12-01 14:17

I am working on code generation and ran into a snag with generics. Here is a \"simplified\" version of what is causing me issues.

Dictionary

        
6条回答
  •  不思量自难忘°
    2020-12-01 14:51

    A good and clean alternative, thanks to @LukeH's comment, is

    using System;
    using System.CodeDom;
    using System.Collections.Generic;
    using Microsoft.CSharp;
    //...
    private string GetFriendlyTypeName(Type type)
    {
        using (var p = new CSharpCodeProvider())
        {
            var r = new CodeTypeReference(type);
            return p.GetTypeOutput(r);
        }
    }
    

提交回复
热议问题