System.Yaml.Serialization.ReflectionUtils.CreateSetPropertyMethod C# (CSharp) Method

CreateSetPropertyMethod() public static method

generates a method to do 'object.property = (propertytype)value;'
public static CreateSetPropertyMethod ( PropertyInfo propertyInfo ) : object>.Action
propertyInfo System.Reflection.PropertyInfo
return object>.Action
        public static Action<object, object> CreateSetPropertyMethod(PropertyInfo propertyInfo)
        {
            MethodInfo mi = propertyInfo.GetSetMethod();

            if (!propertyInfo.CanWrite || mi == null)
                return null;

            ParameterExpression targParam = Expression.Parameter(typeof(object), "obj");
            ParameterExpression valueParam = Expression.Parameter(typeof(object), "val");

            //convert parameters into their proper types
            UnaryExpression target = Expression.Convert(targParam, propertyInfo.DeclaringType);
            UnaryExpression value = Expression.Convert(valueParam, propertyInfo.PropertyType);

            //and call the setter on it.
            MethodCallExpression mce = Expression.Call(target, mi, value);

            return Expression.Lambda<Action<object, object>>(mce, targParam, valueParam).Compile();
        }