src/Mimp.SeeSharper.Reflection/PropertyInfoExtensions.cs
/// <summary>
/// Return a compiled delegate to assign the property.
/// </summary>
/// <param name="property"></param>
/// <param name="delegateType"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException">If the delegate hasn't one parameter for the instance and one parameter for the value.</exception>
/// <exception cref="InvalidOperationException">If the delegate value type isn't castable to the property type.</exception>
Return a compiled delegate to assign the property.
Return a compiled delegate to assign the property .
public static Delegate GetInstanceAssignDelegate(this PropertyInfo property, Type delegateType)
        {
            if (property is null)
                throw new ArgumentNullException(nameof(property));
            if (delegateType is null)
                throw new ArgumentNullException(nameof(delegateType));
            var parameterTypes = delegateType.GetDelegateParameterTypes().ToArray();
            if (parameterTypes.Length != 2)
                throw new ArgumentException($@"Delegate ""{delegateType}"" has an incorrect number of parameters", nameof(delegateType));
            var instance = Expression.Parameter(parameterTypes[0], "instance");
            var value = Expression.Parameter(parameterTypes[1], "value");
            return Expression.Lambda(delegateType,
                Expression.Assign(
                    Expression.Property(Expression.Convert(instance, property.ReflectedType!), property),
                    Expression.Convert(value, property.PropertyType)
                ),
                instance, value
            ).Compile();
        }
public static Delegate GetInstanceAssignDelegate ( this PropertyInfo property , Type delegateType ) { if ( property is null ) throw new ArgumentNullException ( nameof ( property ) ) ; if ( delegateType is null ) throw new ArgumentNullException ( nameof ( delegateType ) ) ; var parameterTypes = delegateType . GetDelegateParameterTypes ( ) . ToArray ( ) ; if ( parameterTypes . Length != 2 ) throw new ArgumentException ( $@" Delegate  "" { delegateType } ""  has an incorrect number of parameters " , nameof ( delegateType ) ) ; var instance = Expression . Parameter ( parameterTypes [ 0 ] , " " ) ; var value = Expression . Parameter ( parameterTypes [ 1 ] , " " ) ; return Expression . Lambda ( delegateType , Expression . Assign ( Expression . Property ( Expression . Convert ( instance , property . ReflectedType ! ) , property ) , Expression . Convert ( value , property . PropertyType ) ) , instance , value ) . Compile ( ) ; }
Return a compiled delegate to assign the property.
Return a compiled delegate to assign the property .
None
None
If the delegate hasn't one parameter for the instance and one parameter for the value.
If the delegate hasn ' t one parameter for the instance and one parameter for the value .
If the delegate value type isn't castable to the property type.
If the delegate value type isn ' t castable to the property type .
PropertyInfo
this
delegateType
Type
None
property
None