private static Action <THolder, TProperty> CreateSetter <THolder, TProperty>(PropertyInfo property, CreateInvalidSetterOption o) { var holderType = Expression.Parameter(typeof(THolder), "e"); var propType = Expression.Parameter(typeof(TProperty), "v"); MethodInfo s = property.GetSetMethod(); if (s == null) { if (o == CreateInvalidSetterOption.ThrowException) { throw new InvalidOperationException(string.Format("Property '{0}' has no setter.", property.Name)); } if (o == CreateInvalidSetterOption.NullAction) { return(null); } return(VoidAction); } return((Action <THolder, TProperty>)s.CreateDelegate(typeof(Action <THolder, TProperty>))); }
/// <summary> /// Creates a setter fo a property. /// </summary> /// <typeparam name="THolder">Property holder type.</typeparam> /// <typeparam name="TProperty">Property type.</typeparam> /// <param name="propertyLambda">A lambda function that selects the property.</param> /// <param name="o">Error handling options. Defaults to <see cref="CreateInvalidSetterOption.ThrowException"/>.</param> /// <returns>An action that takes an holder instance and the value to set.</returns> public static Action <THolder, TProperty> CreateSetter <THolder, TProperty>(Expression <Func <THolder, TProperty> > propertyLambda, CreateInvalidSetterOption o = CreateInvalidSetterOption.ThrowException) { return(CreateSetter <THolder, TProperty>(ReflectionHelper.DoGetPropertyInfo(propertyLambda), o)); }