src/Mimp.SeeSharper.Reflection/TypeExtensions.Cast.cs
/// <summary>
/// Return a compiled delegate to cast a object of <paramref name="source"/> to <paramref name="destination"/>.
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
/// <param name="delegateType"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException">If delegate has one parameter for the instance.</exception>
/// <exception cref="InvalidOperationException">If source isn't castable to destination.</exception>
Return a compiled delegate to cast a object of  to .
Return a compiled delegate to cast a object of to .
public static Delegate GetCastDelegate(this Type source, Type destination, Type delegateType)
        {
            if (source is null)
                throw new ArgumentNullException(nameof(source));
            if (destination is null)
                throw new ArgumentNullException(nameof(destination));
            if (delegateType is null)
                throw new ArgumentNullException(nameof(delegateType));
            var parameterTypes = delegateType.GetDelegateParameterTypes().ToArray();
            if (parameterTypes.Length != 1)
                throw new ArgumentException($@"Delegate ""{delegateType}"" has an incorrect number of parameters", nameof(delegateType));
            var instance = Expression.Parameter(parameterTypes[0], "instance");
            return Expression.Lambda(delegateType,
                Expression.Convert(
                    Expression.ConvertChecked(
                        Expression.Convert(instance, source),
                        destination
                    ),
                    delegateType.GetDelegateReturnType()
                ),
                instance
            ).Compile();
        }
public static Delegate GetCastDelegate ( this Type source , Type destination , Type delegateType ) { if ( source is null ) throw new ArgumentNullException ( nameof ( source ) ) ; if ( destination is null ) throw new ArgumentNullException ( nameof ( destination ) ) ; if ( delegateType is null ) throw new ArgumentNullException ( nameof ( delegateType ) ) ; var parameterTypes = delegateType . GetDelegateParameterTypes ( ) . ToArray ( ) ; if ( parameterTypes . Length != 1 ) throw new ArgumentException ( $@" Delegate  "" { delegateType } ""  has an incorrect number of parameters " , nameof ( delegateType ) ) ; var instance = Expression . Parameter ( parameterTypes [ 0 ] , " " ) ; return Expression . Lambda ( delegateType , Expression . Convert ( Expression . ConvertChecked ( Expression . Convert ( instance , source ) , destination ) , delegateType . GetDelegateReturnType ( ) ) , instance ) . Compile ( ) ; }
Return a compiled delegate to cast a object of  to .
Return a compiled delegate to cast a object of to .
None
None
If delegate has one parameter for the instance.
If delegate has one parameter for the instance .
If source isn't castable to destination.
If source isn ' t castable to destination .
Type
this
destination
Type
None
delegateType
Type
None
source
None