繁体   English   中英

获取类型名称

[英]Get the type name

我如何获得泛型的全称?

例如:此代码

typeof(List<string>).Name

返回

清单`1

代替

List<string>

如何获得一个正确的名字?

typeof(List<string>).ToString()

返回System.Collections.Generic.List`1 [System.String],但我想获取初始名称:

List<string>

这是真的吗?

使用FullName属性

typeof(List<string>).FullName

这将为您提供名称空间+类+类型参数。

您要的是特定于C#的语法。 就.NET而言,这是正确的:

System.Collections.Generic.List`1[System.String]

因此,要获得所需的内容,就必须编写一个函数以所需的方式构建它。 也许像这样:

static string GetCSharpRepresentation( Type t, bool trimArgCount ) {
    if( t.IsGenericType ) {
        var genericArgs = t.GetGenericArguments().ToList();

        return GetCSharpRepresentation( t, trimArgCount, genericArgs );
    }

    return t.Name;
}

static string GetCSharpRepresentation( Type t, bool trimArgCount, List<Type> availableArguments ) {
    if( t.IsGenericType ) {
        string value = t.Name;
        if( trimArgCount && value.IndexOf("`") > -1 ) {
            value = value.Substring( 0, value.IndexOf( "`" ) );
        }

        if( t.DeclaringType != null ) {
            // This is a nested type, build the nesting type first
            value = GetCSharpRepresentation( t.DeclaringType, trimArgCount, availableArguments ) + "+" + value;
        }

        // Build the type arguments (if any)
        string argString = "";
        var thisTypeArgs = t.GetGenericArguments();
        for( int i = 0; i < thisTypeArgs.Length && availableArguments.Count > 0; i++ ) {
            if( i != 0 ) argString += ", ";

            argString += GetCSharpRepresentation( availableArguments[0], trimArgCount );
            availableArguments.RemoveAt( 0 );
        }

        // If there are type arguments, add them with < >
        if( argString.Length > 0 ) {
            value += "<" + argString + ">";
        }

        return value;
    }

    return t.Name;
}

对于这些类型(将true作为第二个参数):

typeof( List<string> ) )
typeof( List<Dictionary<int, string>> )

它返回:

List<String>
List<Dictionary<Int32, String>>

虽然在一般的,我敢打赌,你也许并不需要有你的代码的C#表示,也许如果你这样做,一些格式比C#语法更好的会比较合适。

您可以使用此:

public static string GetTypeName(Type t) {
  if (!t.IsGenericType) return t.Name;
  if (t.IsNested && t.DeclaringType.IsGenericType) throw new NotImplementedException();
  string txt = t.Name.Substring(0, t.Name.IndexOf('`')) + "<";
  int cnt = 0;
  foreach (Type arg in t.GetGenericArguments()) {
    if (cnt > 0) txt += ", ";
    txt += GetTypeName(arg);
    cnt++;
  }
  return txt + ">";
}

例如:

static void Main(string[] args) {
  var obj = new Dictionary<string, Dictionary<HashSet<int>, int>>();
  string s = GetTypeName(obj.GetType());
  Console.WriteLine(s);
  Console.ReadLine();
}

输出:

Dictionary<String, Dictionary<HashSet<Int32>, Int32>>
typeof(List<string>).ToString()

如果您有列表的实例,则可以调用.ToString()并获取以下内容

System.Collections.Generic.List`1[System.String]

这是其他答案直接针对类型而非实例提供的方法的补充。

编辑:在您进行编辑时,我不相信没有提供自己的解析方法是可能的,因为List<string>是C#的简写形式,如何实现该类型,就像您编写typeof(int).ToString() 。捕获的不是“ int”,而是CTS名称System.Int32

这是我的实现,它得益于上面的@Hans回答和对一个重复问题@Jack回答

public static string GetCSharpName( this Type type )
{
    string result;
    if ( primitiveTypes.TryGetValue( type, out result ) )
        return result;
    else
        result = type.Name.Replace( '+', '.' );

    if ( !type.IsGenericType )
        return result;
    else if ( type.IsNested && type.DeclaringType.IsGenericType )
        throw new NotImplementedException();

    result = result.Substring( 0, result.IndexOf( "`" ) );
    return result + "<" + string.Join( ", ", type.GetGenericArguments().Select( GetCSharpName ) ) + ">";
}

static Dictionary<Type, string> primitiveTypes = new Dictionary<Type, string>
{
    { typeof(bool), "bool" },
    { typeof(byte), "byte" },
    { typeof(char), "char" },
    { typeof(decimal), "decimal" },
    { typeof(double), "double" },
    { typeof(float), "float" },
    { typeof(int), "int" },
    { typeof(long), "long" },
    { typeof(sbyte), "sbyte" },
    { typeof(short), "short" },
    { typeof(string), "string" },
    { typeof(uint), "uint" },
    { typeof(ulong), "ulong" },
    { typeof(ushort), "ushort" },
};

通过使用扩展名获得漂亮的类型名称的另一种方法:

typeof(Dictionary<string, Dictionary<decimal, List<int>>>).CSharpName();
// output is: 
// Dictionary<String, Dictionary<Decimal, List<Int32>>>

扩展代码:

public static class TypeExtensions
{
   public static string CSharpName(this Type type)
   {
       string typeName = type.Name;

       if (type.IsGenericType)
       {
           var genArgs = type.GetGenericArguments();

           if (genArgs.Count() > 0)
           {
               typeName = typeName.Substring(0, typeName.Length - 2);

               string args = "";

               foreach (var argType in genArgs)
               {
                   string argName = argType.Name;

                   if (argType.IsGenericType)
                       argName = argType.CSharpName();

                   args += argName + ", ";
               }

               typeName = string.Format("{0}<{1}>", typeName, args.Substring(0, args.Length - 2));
           }
       }

       return typeName;
   }        
}

在某些情况下,我对其他答案(例如数组)有疑问,所以我最终又写了一个。 我不使用Type.Name或类似的文本来获取类型的纯名称,因为我不知道在不同的.Net版本或库的其他实现中格式是否保证相同(我认为不是)。

/// <summary>
/// For the given type, returns its representation in C# code.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="genericArgs">Used internally, ignore.</param>
/// <param name="arrayBrackets">Used internally, ignore.</param>
/// <returns>The representation of the type in C# code.</returns>

public static string GetTypeCSharpRepresentation(Type type, Stack<Type> genericArgs = null, StringBuilder arrayBrackets = null)
{
    StringBuilder code = new StringBuilder();
    Type declaringType = type.DeclaringType;

    bool arrayBracketsWasNull = arrayBrackets == null;

    if (genericArgs == null)
        genericArgs = new Stack<Type>(type.GetGenericArguments());


    int currentTypeGenericArgsCount = genericArgs.Count;
    if (declaringType != null)
        currentTypeGenericArgsCount -= declaringType.GetGenericArguments().Length;

    Type[] currentTypeGenericArgs = new Type[currentTypeGenericArgsCount];
    for (int i = currentTypeGenericArgsCount - 1; i >= 0; i--)
        currentTypeGenericArgs[i] = genericArgs.Pop();


    if (declaringType != null)
        code.Append(GetTypeCSharpRepresentation(declaringType, genericArgs)).Append('.');


    if (type.IsArray)
    {
        if (arrayBrackets == null)
            arrayBrackets = new StringBuilder();

        arrayBrackets.Append('[');
        arrayBrackets.Append(',', type.GetArrayRank() - 1);
        arrayBrackets.Append(']');

        Type elementType = type.GetElementType();
        code.Insert(0, GetTypeCSharpRepresentation(elementType, arrayBrackets : arrayBrackets));
    }
    else
    {
        code.Append(new string(type.Name.TakeWhile(c => char.IsLetterOrDigit(c) || c == '_').ToArray()));

        if (currentTypeGenericArgsCount > 0)
        {
            code.Append('<');
            for (int i = 0;  i < currentTypeGenericArgsCount;  i++)
            {
                code.Append(GetTypeCSharpRepresentation(currentTypeGenericArgs[i]));
                if (i < currentTypeGenericArgsCount - 1)
                    code.Append(',');
            }
            code.Append('>');
        }

        if (declaringType == null  &&  !string.IsNullOrEmpty(type.Namespace))
        {
            code.Insert(0, '.').Insert(0, type.Namespace);
        }
    }


    if (arrayBracketsWasNull  &&  arrayBrackets != null)
        code.Append(arrayBrackets.ToString());


    return code.ToString();
}

我已经用这种疯狂的类型对其进行了测试,到目前为止,它已经完美地运行了:

class C
{
    public class D<D1, D2>
    {
        public class E
        {
            public class K<R1, R2, R3>
            {
                public class P<P1>
                {
                    public struct Q
                    {
                    }
                }
            }
        }
    }
}

type = typeof(List<Dictionary<string[], C.D<byte, short[,]>.E.K<List<int>[,][], Action<List<long[][][,]>[], double[][,]>, float>.P<string>.Q>>[][,][,,,][][,,]);

// Returns "System.Collections.Generic.List<System.Collections.Generic.Dictionary<System.String[],Test.Program.C.D<System.Byte,System.Int16[,]>.E.K<System.Collections.Generic.List<System.Int32>[,][],System.Action<System.Collections.Generic.List<System.Int64[][][,]>[],System.Double[][,]>,System.Single>.P<System.String>.Q>>[][,][,,,][][,,]":
GetTypeCSharpRepresentation(type);

也许仍有一些我没想到的陷阱,但是有一个已知的陷阱:检索名称,我只会得到满足条件char.IsLetterOrDigit(c) || c == '_'字符。 char.IsLetterOrDigit(c) || c == '_'直到找不到为止,因此任何使用允许的不符合条件的字符的类型名称都将失败。

Adam Sills答案的改进,适用于非泛型嵌套类型和泛型类型定义:

public class TypeNameStringExtensions
{
    public static string GetCSharpRepresentation(Type t)
    {
        return GetCSharpRepresentation(t, new Queue<Type>(t.GetGenericArguments()));
    }
    static string GetCSharpRepresentation(Type t, Queue<Type> availableArguments)
    {
        string value = t.Name;
        if (t.IsGenericParameter)
        {
            return value;
        }
        if (t.DeclaringType != null)
        {
            // This is a nested type, build the parent type first
            value = GetCSharpRepresentation(t.DeclaringType, availableArguments) + "+" + value;
        }
        if (t.IsGenericType)
        {
            value = value.Split('`')[0];

            // Build the type arguments (if any)
            string argString = "";
            var thisTypeArgs = t.GetGenericArguments();
            for (int i = 0; i < thisTypeArgs.Length && availableArguments.Count > 0; i++)
            {
                if (i != 0) argString += ", ";

                argString += GetCSharpRepresentation(availableArguments.Dequeue());
            }

            // If there are type arguments, add them with < >
            if (argString.Length > 0)
            {
                value += "<" + argString + ">";
            }
        }

        return value;
    }

    [TestCase(typeof(List<string>), "List<String>")]
    [TestCase(typeof(List<Dictionary<int, string>>), "List<Dictionary<Int32, String>>")]
    [TestCase(typeof(Stupid<int>.Stupider<int>), "Stupid<Int32>+Stupider<Int32>")]
    [TestCase(typeof(Dictionary<int, string>.KeyCollection), "Dictionary<Int32, String>+KeyCollection")]
    [TestCase(typeof(Nullable<Point>), "Nullable<Point>")]
    [TestCase(typeof(Point?), "Nullable<Point>")]
    [TestCase(typeof(TypeNameStringExtensions), "TypeNameStringExtensions")]
    [TestCase(typeof(Another), "TypeNameStringExtensions+Another")]
    [TestCase(typeof(G<>), "TypeNameStringExtensions+G<T>")]
    [TestCase(typeof(G<string>), "TypeNameStringExtensions+G<String>")]
    [TestCase(typeof(G<Another>), "TypeNameStringExtensions+G<TypeNameStringExtensions+Another>")]
    [TestCase(typeof(H<,>), "TypeNameStringExtensions+H<T1, T2>")]
    [TestCase(typeof(H<string, Another>), "TypeNameStringExtensions+H<String, TypeNameStringExtensions+Another>")]
    [TestCase(typeof(Another.I<>), "TypeNameStringExtensions+Another+I<T3>")]
    [TestCase(typeof(Another.I<int>), "TypeNameStringExtensions+Another+I<Int32>")]
    [TestCase(typeof(G<>.Nested), "TypeNameStringExtensions+G<T>+Nested")]
    [TestCase(typeof(G<string>.Nested), "TypeNameStringExtensions+G<String>+Nested")]
    [TestCase(typeof(A<>.C<>), "TypeNameStringExtensions+A<B>+C<D>")]
    [TestCase(typeof(A<int>.C<string>), "TypeNameStringExtensions+A<Int32>+C<String>")]
    public void GetCSharpRepresentation_matches(Type type, string expected)
    {
        string actual = GetCSharpRepresentation(type);
        Assert.AreEqual(expected, actual);
    }

    public class G<T>
    {
        public class Nested { }
    }

    public class A<B>
    {
        public class C<D> { }
    }

    public class H<T1, T2> { }

    public class Another
    {
        public class I<T3> { }
    }
}

public class Stupid<T1>
{
    public class Stupider<T2>
    {
    }
}

我还选择放弃他的trimArgCount ,因为我看不到何时会有用,而选择Queue<Type>因为那是目的(将存在的项从前面拉出)。

如果要使用基本的通用类型:

List<string> lstString = new List<string>();
Type type = lstString.GetType().GetGenericTypeDefinition();

假设您想使用该类型来做某事,并且您实际上并不需要真正的字符串定义,那并不是那么有用。

遇到这个问题,以为我会分享我自己的解决方案。 它处理多个通用参数,可为空,锯齿状数组,多维数组,锯齿状/多维数组的组合以及上述任何项的任何嵌套组合。 我主要将其用于日志记录,以便于识别复杂类型。

public static string GetGoodName(this Type type)
{
    var sb = new StringBuilder();

    void VisitType(Type inType)
    {
        if (inType.IsArray)
        {
            var rankDeclarations = new Queue<string>();
            Type elType = inType;

            do
            {
                rankDeclarations.Enqueue($"[{new string(',', elType.GetArrayRank() - 1)}]");
                elType = elType.GetElementType();
            } while (elType.IsArray);

            VisitType(elType);

            while (rankDeclarations.Count > 0)
            {
                sb.Append(rankDeclarations.Dequeue());
            }
        }
        else
        {
            if (inType.IsGenericType)
            {
                var isNullable = inType.IsNullable();
                var genargs = inType.GetGenericArguments().AsEnumerable();
                var numer = genargs.GetEnumerator();

                numer.MoveNext();

                if (!isNullable) sb.Append($"{inType.Name.Substring(0, inType.Name.IndexOf('`'))}<");

                VisitType(numer.Current);

                while (numer.MoveNext())
                {
                    sb.Append(",");
                    VisitType(numer.Current);
                }

                if (isNullable)
                {
                    sb.Append("?");
                }
                else
                {
                    sb.Append(">");
                }
            }
            else
            {
                sb.Append(inType.Name);
            }
        }
    }

    VisitType(type);

    var s = sb.ToString();

    return s;
}

这个:

typeof(Dictionary<int?, Tuple<string[], List<string[][,,,]>>>).GetGoodName()

...返回此:

Dictionary<Int32?,Tuple<String[],List<String[][,,,]>>>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM