C# Get Generic Type Name

前端 未结 9 1748
旧时难觅i
旧时难觅i 2020-11-30 06:09

I need some way to get the Name of a Type, when type.IsGenericType = true.

    Type t = typeof(List);
    MessageBox.         


        
9条回答
  •  醉酒成梦
    2020-11-30 07:11

    My take on yoyo's approach. Ensures more friendly names for primitives, handles arrays and is recursive to handle nested generics. Also unit tests.

        private static readonly Dictionary _typeToFriendlyName = new Dictionary
        {
            { typeof(string), "string" },
            { typeof(object), "object" },
            { typeof(bool), "bool" },
            { typeof(byte), "byte" },
            { typeof(char), "char" },
            { typeof(decimal), "decimal" },
            { typeof(double), "double" },
            { typeof(short), "short" },
            { typeof(int), "int" },
            { typeof(long), "long" },
            { typeof(sbyte), "sbyte" },
            { typeof(float), "float" },
            { typeof(ushort), "ushort" },
            { typeof(uint), "uint" },
            { typeof(ulong), "ulong" },
            { typeof(void), "void" }
        };
    
        public static string GetFriendlyName(this Type type)
        {
            string friendlyName;
            if (_typeToFriendlyName.TryGetValue(type, out friendlyName))
            {
                return friendlyName;
            }
    
            friendlyName = type.Name;
            if (type.IsGenericType)
            {
                int backtick = friendlyName.IndexOf('`');
                if (backtick > 0)
                {
                    friendlyName = friendlyName.Remove(backtick);
                }
                friendlyName += "<";
                Type[] typeParameters = type.GetGenericArguments();
                for (int i = 0; i < typeParameters.Length; i++)
                {
                    string typeParamName = typeParameters[i].GetFriendlyName();
                    friendlyName += (i == 0 ? typeParamName : ", " + typeParamName);
                }
                friendlyName += ">";
            }
    
            if (type.IsArray)
            {
                return type.GetElementType().GetFriendlyName() + "[]";
            }
    
            return friendlyName;
        }
    
    [TestFixture]
    public class TypeHelperTest
    {
        [Test]
        public void TestGetFriendlyName()
        {
            Assert.AreEqual("string", typeof(string).FriendlyName());
            Assert.AreEqual("int[]", typeof(int[]).FriendlyName());
            Assert.AreEqual("int[][]", typeof(int[][]).FriendlyName());
            Assert.AreEqual("KeyValuePair", typeof(KeyValuePair).FriendlyName());
            Assert.AreEqual("Tuple", typeof(Tuple).FriendlyName());
            Assert.AreEqual("Tuple, string>", typeof(Tuple, string>).FriendlyName());
            Assert.AreEqual("List>", typeof(List>).FriendlyName());
            Assert.AreEqual("Tuple", typeof(Tuple).FriendlyName());
        }
    }
    

提交回复
热议问题