Get Type Code Name - CSharp System.Reflection

CSharp examples for System.Reflection:Type

Description

Get Type Code Name

Demo Code


using System.Text;
using System.Reflection;
using System;/*from w w  w.  j ava2s  .  c o  m*/

public class Main{
        private static void GetTypeCodeName(this Type type, StringBuilder builder)
        {
            if (type.IsGenericType)
            {
                var name = type.GetGenericTypeDefinition().Name;
                var apostropheIndex = name.IndexOf('`');
                builder.Append(name.Remove(apostropheIndex, name.Length - apostropheIndex));
                builder.Append("<");
                var genericTypes = type.GetGenericArguments();
                foreach (var genericType in genericTypes)
                {
                    genericType.GetTypeCodeName(builder);
                    builder.Append(", ");
                }
                builder.Remove(builder.Length - 2, 2);
                builder.Append(">");
            }
            else
            {
                builder.Append(type.Name);
            }
        }
        public static string GetTypeCodeName(this Type type)
        {
            #region Check
            if (ReferenceEquals(type, null))
            {
                throw new ArgumentNullException("type");
            }
            #endregion
            var builder = new StringBuilder();
            type.GetTypeCodeName(builder);
            return builder.ToString();
        }
}

Related Tutorials