using System; using System.Linq.Expressions; namespace RosettaUI { public static class CastBinder { public static IBinder Create(IBinder binder, Type fromType, Type toType) { var castBinderType = typeof(CastBinder<,>).MakeGenericType(fromType, toType); return (IBinder) Activator.CreateInstance(castBinderType, binder); } } public class CastBinder : ConvertBinder { public CastBinder(IBinder binder) : base(binder) { } protected override TTo GetFromParent(TFrom parent) => CastFunc(parent); protected override TFrom SetToParent(TFrom parent, TTo value) => CastToParentFunc(parent, value); #region static public static readonly Func CastFunc; public static readonly Func CastToParentFunc; static CastBinder() { var from = Expression.Parameter(typeof(TFrom)); var cast = Expression.Convert(from, typeof(TTo)); CastFunc = Expression.Lambda>(cast, from).Compile(); var to = Expression.Parameter(typeof(TTo)); var castToParent = Expression.Convert(to, typeof(TFrom)); CastToParentFunc = Expression.Lambda>(castToParent, from, to).Compile(); } #endregion } }