Get Type Full Code Name - CSharp System.Reflection

CSharp examples for System.Reflection:Type

Description

Get Type Full Code Name

Demo Code


using System.Text;
using System.Reflection;
using System;/*from w w  w  .j  a  v  a  2 s  . c  o m*/

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

Related Tutorials