Exemple #1
0
 public static CachedType GetType(Type type)
 {
     if (type == null)
     {
         return(null);
     }
     if (!Cache.ContainsKey(type))
     {
         var cachedType = new CachedType(type);
         if (Cache.TryAdd(type, cachedType))
         {
             return(cachedType);
         }
     }
     return(Cache[type]);
 }
Exemple #2
0
 public static TypeKind GetKind(this CachedType type,
                                bool enumerableImplementationsAreComplex,
                                bool dictionaryImplementationsAreComplex)
 {
     if (type.IsSimpleType)
     {
         return(TypeKind.Simple);
     }
     if (type.IsDictionary)
     {
         return(!type.IsInBclCollectionNamespace &&
                dictionaryImplementationsAreComplex ? TypeKind.Complex : TypeKind.Dictionary);
     }
     if (type.IsEnumerable)
     {
         return(!type.IsInBclCollectionNamespace &&
                enumerableImplementationsAreComplex ? TypeKind.Complex : TypeKind.Enumerable);
     }
     return(TypeKind.Complex);
 }
Exemple #3
0
 public static bool Is <T>(this CachedType type)
 {
     return(type.Type == typeof(T));
 }
Exemple #4
0
        public static object ParseSimpleType(this string value, CachedType type)
        {
            if (type.Is <string>() || value == null)
            {
                return(value);
            }
            if (type.IsEnum)
            {
                return(value.ParseEnum(type, true));
            }
            switch (type.TypeCode)
            {
            case TypeCode.Char:
                if (value.Length == 1)
                {
                    return(value[0]);
                }
                throw new Exception("Char length {0} is invalid.".ToFormat(value.Length));

            case TypeCode.Boolean: return(Boolean.Parse(value.ToLower()));

            case TypeCode.SByte: return(SByte.Parse(value));

            case TypeCode.Byte: return(Byte.Parse(value));

            case TypeCode.Int16: return(Int16.Parse(value));

            case TypeCode.UInt16: return(UInt16.Parse(value));

            case TypeCode.Int32: return(Int32.Parse(value));

            case TypeCode.UInt32: return(UInt32.Parse(value));

            case TypeCode.Int64: return(Int64.Parse(value));

            case TypeCode.UInt64: return(UInt64.Parse(value));

            case TypeCode.Single: return(Single.Parse(value));

            case TypeCode.Double: return(Double.Parse(value));

            case TypeCode.Decimal: return(Decimal.Parse(value));

            case TypeCode.DateTime: return(value.TryParseMicrosoftJsonDateFormat() ?? DateTime.Parse(value));

            default:
                if (type.Is <Guid>(true))
                {
                    return(Guid.Parse(value));
                }
                if (type.Is <TimeSpan>(true))
                {
                    return(TimeSpan.Parse(value));
                }
                if (type.Is <Uri>())
                {
                    return(new Uri(value));
                }
                if (type.Is <IntPtr>(true))
                {
                    return(new IntPtr(Int64.Parse(value)));
                }
                if (type.Is <UIntPtr>(true))
                {
                    return(new UIntPtr(UInt64.Parse(value)));
                }
                throw new ArgumentException("Type '{0}' is not a simple type.".ToFormat(type));
            }
        }
Exemple #5
0
 public static object ParseEnum(this string value, CachedType type, bool ignoreCase)
 {
     return(Enum.Parse(type.UnderlyingType.Type, value, ignoreCase));
 }
Exemple #6
0
 public static bool CanBeCastTo <T>(this CachedType type)
 {
     return(type.Type.CanBeCastTo <T>());
 }
Exemple #7
0
 public static bool IsTypeOf(this CachedType type, object value)
 {
     return(value != null && type.Type == value.GetType());
 }
Exemple #8
0
 public static bool Is <T>(this CachedType type, bool includeNullable) where T : struct
 {
     return(type.Type.Is <T>(includeNullable));
 }
Exemple #9
0
 public static object CreateArray(this CachedType type, int length = 0)
 {
     return(type.Type.CreateArray());
 }