[Fork] AutoMapper Type Converter

CSharp · Console

/Program.cs

using System;
using AutoMapper;
					
public class Program
{
	public abstract class Enum<T> where T : struct, IComparable
	{
		public T Id { get; set;}

        public string Value { get; set; }
		
		protected Enum(T id, string @value)
        {
            Id = id;
            Value = @value;
        }
	}
	
    public sealed class Status : Enum<byte>
    {
        public static readonly Status Pending = new Status(0, "PENDING");
        public static readonly Status Finished = new Status(1, "FINISHED");
		
		private Status(byte id, string name) : base(id, name)
        {
        }
		
		public static Status Create (string @value)
		{
			// some logic here
			//return Wrapper.Wrap(Finished);
			return Finished;
		}
		
		public static Wrapper<Status> CreateWrapper(string @value)
		{
			// some logic here
			return Wrapper.Wrap(Finished);
		}
	}
	
	public static Enum<byte> ConvertToSomething<T>() where T : struct, IComparable
    {
        return Status.Finished;
    }

	public interface IWrapper<out T> 
    { 
    }

	public class Wrapper
	{
		public static Wrapper<T> Wrap<T>(T @value)
        {
            return new Wrapper<T>(@value);
        }
	}
	
	public class Wrapper<T> : IWrapper<T>
	{
		T Value;
		
		internal Wrapper(T @value)
		{
			Value = @value;
		}
	}
	
	public class StatusConverter<TType> : ITypeConverter<string, Enum<byte>>
    {
        public Enum<byte> Convert(string source, Enum<byte> destination, ResolutionContext context)
        {
            return Status.Create(source);
        }
    }
	
	public class StatusConverter2<TValue> : ITypeConverter<string, IWrapper<Status>> // works fine
	{
		public IWrapper<Status> Convert(string source, IWrapper<Status> destination, ResolutionContext context)
		{
			return Status.CreateWrapper(source);
		}
	}
	
	public class StatusConverter3<TValue> : ITypeConverter<string, IWrapper<Enum<byte>>> // doesn't work with Enum<T>
	{
		public IWrapper<Enum<byte>> Convert(string source, IWrapper<Enum<byte>> destination, ResolutionContext context)
		{
			return Status.CreateWrapper(source);
		}
	}
	
	public static void Main()
	{
		Enum<byte> test = Status.Finished;
		Console.WriteLine("Hello World");
	}
}
;