コード例 #1
0
        /// <summary>
        /// Gets the mapper from TSource to TTarget.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TTarget">The type of the target.</typeparam>
        /// <returns>
        /// An instance of <seealso cref="IEnumMapper{TSource, TTarget}"/> representing the enum mapper.
        /// </returns>
        /// <exception cref="System.InvalidCastException"> TSource and TTarget must be Enum.</exception>
        public static IEnumMapper <TSource, TTarget> GetMapper <TSource, TTarget>()
            where TSource : struct
            where TTarget : struct
        {
            if (!typeof(TSource).IsEnum)
            {
                throw new InvalidCastException("TSource must be an Enum");
            }
            if (!typeof(TTarget).IsEnum)
            {
                throw new InvalidCastException("TTarget must be an Enum");
            }

            return(EnumMapperGenerator <TSource, TTarget> .GetOrCreate());
        }
コード例 #2
0
        public static IEnumMapper <TSource, TTarget> GetOrCreate()
        {
            var key = typeof(TSource).GetHashCode() * 397 ^ typeof(TTarget).GetHashCode();

            return(_TypeCache.GetOrAdd(key, k => EnumMapperGenerator <TSource, TTarget> .CreateInstance()));
        }
コード例 #3
0
        public static Func <TSource, TTarget> GetOrCreateMethod()
        {
            var key = typeof(TSource).GetHashCode() * 397 ^ typeof(TTarget).GetHashCode();

            return(_MethodCache.GetOrAdd(key, k => EnumMapperGenerator <TSource, TTarget> .CreateLambda().Compile()));
        }