/// <summary> /// Creates an action that sets the value of the specified field. The first parameter /// of the resulting function takes the object whose field value is to be set. If the /// specified field is static, the first parameter of the resulting function is ignored. /// The second parameter of the resulting function takes the new value of the field. /// </summary> /// <param name="field">The field to create the setter action for.</param> /// <returns>An action that sets the field value.</returns> public static Action <object, object> CreateSetter(this FieldInfo field) { if (field is null) { throw new ArgumentNullException(nameof(field)); } if (field.IsInitOnly) { throw new ArgumentException("field cannot be readonly", nameof(field)); } var setter = new FieldSetter(field); QueueUserWorkItem(setter, s => s.SetOptimizedAction()); return(setter.SetValue); }
/// <summary> /// Creates an action that sets the value of the specified field. The first parameter /// of the resulting function takes the object whose field value is to be set. If the /// specified field is static, the first parameter of the resulting function is ignored. /// The second parameter of the resulting function takes the new value of the field. /// </summary> /// <typeparam name="TFieldType"> /// The type of the second parameter of the resulting action. This type must be compatible with /// the <see cref="FieldInfo.FieldType"/> of the <paramref name="field"/> parameter. /// </typeparam> /// <param name="field">The field to create the setter action for.</param> /// <returns>An action that sets the field value.</returns> public static Action <object, TFieldType> CreateSetter <TFieldType>(this FieldInfo field) { if (field is null) { throw new ArgumentNullException(nameof(field)); } if (!field.FieldType.IsAssignableFrom(typeof(TFieldType))) { throw new ArgumentException("field.FieldType must be assignable from TFieldType", nameof(field)); } if (field.IsInitOnly) { throw new ArgumentException("field cannot be readonly", nameof(field)); } var setter = new FieldSetter <TFieldType>(field); QueueUserWorkItem(setter, s => s.SetOptimizedAction()); return(setter.SetValue); }