Example #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));
        }
Example #2
0
        private static void SwitchSequence <T>(this ILGenerator ilGen,
                                               Action <ILGenerator> emitLoadValue,
                                               CaseInfo <T>[] cases,
                                               Label defaultLabel,
                                               T offset = default)
        {
            emitLoadValue(ilGen);

            if (!TypeHelper.IsEmptyValue(offset))
            {
                if (Underlying.SizeOf <T>() <= sizeof(int))
                {
                    ilGen.LoadConstant(XConvert <int> .Convert(offset));
                }
                else
                {
                    ilGen.LoadConstant(XConvert <long> .Convert(offset));
                }

                ilGen.Subtract();
            }

            ilGen.Switch(cases.Map(item => item.Label));

            ilGen.Branch(defaultLabel);
        }
Example #3
0
 /// <summary>
 /// 创建 XConvert 类型转换迭代器。
 /// </summary>
 /// <typeparam name="TIn">输入类型</typeparam>
 /// <typeparam name="TOut">输出类型</typeparam>
 /// <param name="input">输入迭代器</param>
 /// <returns>返回一个 yield 关键字实现的迭代器</returns>
 public static IEnumerable <TOut> CreateAsIterator <TIn, TOut>(IEnumerable <TIn> input)
 {
     foreach (var item in input)
     {
         yield return(XConvert <TOut> .Convert(item));
     }
 }
Example #4
0
        public static TDestination?ConvertTo <TSource, TDestination>(TSource source) where TDestination : struct
        {
            if (source == null)
            {
                return(null);
            }

            return(XConvert <TSource, TDestination> .Convert(source));
        }
Example #5
0
        public static TDestination Convert <TSource, TDestination>(TSource?source) where TSource : struct
        {
            if (source == null)
            {
                object nullValue = null;

                return((TDestination)nullValue);
            }

            return(XConvert <TSource, TDestination> .Convert(source.Value));
        }
Example #6
0
        private static bool IsSequence <T>(CaseInfo <T>[] cases, out T offset)
        {
            if (cases.Length > 0)
            {
                offset = cases[0].Value;

                for (uint i = 0; i < cases.Length; i++)
                {
                    if (XConvert <long> .Convert(offset) + i != XConvert <long> .Convert(cases[i].Value))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            offset = default;

            return(false);
        }
Example #7
0
 public override TDestination Convert <TDestination>(object value) => XConvert <TDestination> .Convert((TSource)value);
Example #8
0
 public static TDestination Convert <TSource, TDestination>(TSource source)
 {
     return(XConvert <TSource, TDestination> .Convert(source));
 }
Example #9
0
 public override object Convert <TSource>(TSource value) => XConvert <TDestination> .Convert(value);