Beispiel #1
0
        /// <summary>
        /// 转换方法。
        /// </summary>
        /// <param name="obj">值</param>
        /// <returns>返回新值</returns>
        public static TDestination Convert(object obj)
        {
            if (obj == null)
            {
                return(XConvert <object, TDestination> .Convert(null));
            }

            if (obj is TDestination)
            {
                return((TDestination)obj);
            }

            var typeKey = (long)Pointer.GetTypeHandle(obj);

            IDConvert <TDestination> value;

            if (!Cache.TryGetValue(typeKey, out value))
            {
                lock (CacheLock)
                {
                    if (!Cache.TryGetValue(typeKey, out value))
                    {
                        var type = typeof(XConvert <,>).MakeGenericType(obj.GetType(), typeof(TDestination));

                        value = (IDConvert <TDestination>)Activator.CreateInstance(type);

                        Cache.Add(typeKey, value);
                    }
                }
            }

            return(value.Convert(obj));
        }
Beispiel #2
0
        /// <summary>
        /// 转换方法。
        /// </summary>
        /// <param name="source">值</param>
        /// <param name="type">目标类型</param>
        /// <returns>返回新值</returns>
        public static object Convert(TSource source, Type type)
        {
            var typeKey = type.TypeHandle.Value.ToInt64();

            ISConvert <TSource> value;

            if (!Cache.TryGetValue(typeKey, out value))
            {
                lock (CacheLock)
                {
                    if (!Cache.TryGetValue(typeKey, out value))
                    {
                        var convertType = typeof(XConvert <,>).MakeGenericType(typeof(TSource), type);

                        value = (ISConvert <TSource>)Activator.CreateInstance(convertType);

                        Cache.Add(typeKey, value);
                    }
                }
            }

            return(value.Convert(source));
        }