// Test<int,int>,支持递归深入下一层

        /// <summary>
        /// 获取类型的泛型定义。
        /// <para>获取示例 Test{int,int,List{int}}></para>
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetGenriceName(this Type type)
        {
            if (!type.IsGenericType)
            {
                return(type.Name);
            }

            // 要识别出为定义泛型和已定义泛型
            // List<>、List<int>

            StringBuilder code = new StringBuilder($"{WipeOutName(type.Name)}<");
            List <string> vs   = new List <string>();

            // 未定义泛型
            if (type.IsGenericTypeDefinition)
            {
                var gType = type.GetGenericTypeDefinition();
                code.Append(string.Join(", ", gType.GetGenericArguments().Select(x => GetInOut(x) + x.Name)));
            }
            else
            {
                foreach (var item in type.GetGenericArguments())
                {
                    if (item.IsGenericType)
                    {
                        vs.Add(GetInOut(item) + GetGenriceName(item));
                    }
                    else
                    {
                        vs.Add(GetInOut(item) + ConstantTable.GetBaseTypeName(item));
                    }
                }
                code.Append(string.Join(", ", vs));
            }


            return(code.Append('>').ToString());

            // 解析协变逆变
            string GetInOut(Type type1)
            {
                if (!type1.IsGenericParameter)
                {
                    return(string.Empty);
                }
                switch (type1.GenericParameterAttributes)
                {
                case GenericParameterAttributes.Contravariant: return("in");

                case GenericParameterAttributes.Covariant: return("out");

                default: return(string.Empty);
                }
            }
        }
        /// <summary>
        /// 获取泛型类型中的参数列表,如果泛型参数未定义,则列表数为 0
        /// <para>注意,只支持一层泛型</para>
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string[] GetGenriceParams(this Type type)
        {
            if (!type.IsGenericType)
            {
                return(new string[0]);
            }

#if NETSTANDARD2_0
            return(type.GetGenericArguments().Select(x =>
                                                     x.IsGenericParameter ? x.Name : ConstantTable.GetBaseTypeName(x))
                   .ToArray());
#else
            return(type.GetGenericArguments().Select(x =>
                                                     x.IsGenericTypeParameter ? x.Name : ConstantTable.GetBaseTypeName(x))
                   .ToArray());
#endif
        }
Example #3
0
        public void Test()
        {
            var output = ConstantTable.GetBaseTypeName <Int32>();

            Assert.Equal("int", output);

            output = ConstantTable.GetBaseTypeName(666);
            Assert.Equal("int", output);

            output = ConstantTable.GetBaseTypeName(new object());
            Assert.Equal("object", output);

            output = ConstantTable.GetBaseTypeName(typeof(object));
            Assert.Equal("object", output);

            output = ConstantTable.GetBaseTypeName(typeof(int));
            Assert.Equal("int", output);
        }