Mega Code Archive

 
Categories / C# / Reflection
 

Get Full Type String

using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Text; using System.Linq; namespace ViewMaker.Core.Utils {     /// <summary>     /// ?????????????     /// </summary>     internal static class TypeUtil     {         /// <summary>         /// ??????????????????????Generics?[]??????         /// </summary>         /// <param name="type">????</param>         /// <returns>??????</returns>         public static string GetFullTypeString(Type type)         {             if (type.IsGenericType)             {                 string name;                 int offset = type.FullName.IndexOf("`");                 if (offset >= 0)                 {                     name = type.FullName.Substring(0, offset);                 }                 else                 {                     name = type.FullName;                 }                 List<string> p = new List<string>();                 foreach (Type paraType in type.GetGenericArguments())                 {                     p.Add(GetTypeString(paraType));                 }                 return string.Format("{0}<{1}>", name, string.Join(",", p.ToArray()));             }             else             {                 return type.FullName;             }         }         /// <summary>         /// ?????????????????????????????????Generics?[]??????         /// </summary>         /// <param name="type">????</param>         /// <returns>??????</returns>         public static string GetTypeString(Type type)         {             if (type.IsGenericType)             {                 string name;                 int offset = type.Name.IndexOf("`");                 if (offset >= 0)                 {                     name = type.Name.Substring(0, offset);                 }                 else                 {                     name = type.Name;                 }                 List<string> p = new List<string>();                 foreach (Type paraType in type.GetGenericArguments())                 {                     p.Add(GetTypeString(paraType));                 }                 return string.Format("{0}<{1}>", name, string.Join(",", p.ToArray()));             }             else             {                 return type.Name;             }         }         /// <summary>         /// Nullable?????????         /// </summary>         /// <param name="type">????</param>         /// <returns>Nullable????true??????false</returns>         public static bool IsNullable(Type type)         {             return (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>)));         }         /// <summary>         /// ??????Enum?????????         /// </summary>         /// <param name="value">Enum???Nullable?string?</param>         /// <param name="targetType">????Enum?</param>         /// <returns></returns>         public static object GetValueWithEnumParse(object value, Type targetType)         {             if (IsNullable(targetType))             {                 targetType = targetType.GetGenericArguments()[0];             }             if (targetType.IsEnum && value is string)             { #if !SILVERLIGHT                 return Enum.Parse(targetType, (string)value); #else                 return Enum.Parse(targetType, (string)value, true); #endif             }             else             {                 return value;             }         }         /// <summary>         /// Enum?Name????????         /// </summary>         /// <param name="type"></param>         /// <returns></returns>         public static List<string> GetEnumNames(Type type)         {  #if !SILVERLIGHT             return Enum.GetNames(type).ToList(); #else             List<string> enumNames = new List<string>();             foreach (FieldInfo fi in type.GetFields(BindingFlags.Static | BindingFlags.Public))             {                 enumNames.Add(fi.Name);             }             return enumNames;  #endif         }     } }