/// <summary> /// Creates an action that sets the value of the specified static field. The /// parameter of the resulting function takes the new value of the static field. /// </summary> /// <param name="field">The static field to create the setter action for.</param> /// <returns>An action that sets the static field value.</returns> public static Action <object> CreateStaticSetter(this FieldInfo field) { if (field is null) { throw new ArgumentNullException(nameof(field)); } if (field.IsInitOnly) { throw new ArgumentException("field cannot be readonly", nameof(field)); } if (!field.IsStatic) { throw new ArgumentException("Field must be static.", nameof(field)); } var setter = new StaticFieldSetter(field); QueueUserWorkItem(setter, s => s.SetOptimizedAction()); return(setter.SetValue); }
/// <summary> /// Creates an action that sets the value of the specified static field. The /// parameter of the resulting function takes the new value of the static field. /// </summary> /// <typeparam name="TFieldType"> /// The type of the 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 static field to create the setter action for.</param> /// <returns>An action that sets the static field value.</returns> public static Action <TFieldType> CreateStaticSetter <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)); } if (!field.IsStatic) { throw new ArgumentException("Field must be static.", nameof(field)); } var setter = new StaticFieldSetter <TFieldType>(field); QueueUserWorkItem(setter, s => s.SetOptimizedAction()); return(setter.SetValue); }