using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Linq;
namespace ViewMaker.Core.Utils
{
///
/// ?????????????
///
internal static class TypeUtil
{
///
/// ??????????????????????Generics?[]??????
///
/// ????
/// ??????
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 p = new List();
foreach (Type paraType in type.GetGenericArguments())
{
p.Add(GetTypeString(paraType));
}
return string.Format("{0}<{1}>", name, string.Join(",", p.ToArray()));
}
else
{
return type.FullName;
}
}
///
/// ?????????????????????????????????Generics?[]??????
///
/// ????
/// ??????
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 p = new List();
foreach (Type paraType in type.GetGenericArguments())
{
p.Add(GetTypeString(paraType));
}
return string.Format("{0}<{1}>", name, string.Join(",", p.ToArray()));
}
else
{
return type.Name;
}
}
///
/// Nullable?????????
///
/// ????
/// Nullable????true??????false
public static bool IsNullable(Type type)
{
return (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>)));
}
///
/// ??????Enum?????????
///
/// Enum???Nullable?string?
/// ????Enum?
///
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;
}
}
///
/// Enum?Name????????
///
///
///
public static List GetEnumNames(Type type)
{
#if !SILVERLIGHT
return Enum.GetNames(type).ToList();
#else
List enumNames = new List();
foreach (FieldInfo fi in type.GetFields(BindingFlags.Static | BindingFlags.Public))
{
enumNames.Add(fi.Name);
}
return enumNames;
#endif
}
}
}