/// <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()); }
public static IEnumMapper <TSource, TTarget> GetOrCreate() { var key = typeof(TSource).GetHashCode() * 397 ^ typeof(TTarget).GetHashCode(); return(_TypeCache.GetOrAdd(key, k => EnumMapperGenerator <TSource, TTarget> .CreateInstance())); }
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())); }