/// <summary> /// 增加 RequiredAttribute 扩展方法 /// </summary> /// <param name="context"></param> /// <param name="columnName"></param> /// <param name="errorMessage"></param> /// <param name="allowEmptyStrings"></param> public static void AddRequiredAttribute(this DynamicObjectContext context, string columnName, string?errorMessage = null, bool allowEmptyStrings = false) { var parameters = new KeyValuePair <string, object?>[] { new(nameof(RequiredAttribute.ErrorMessage), errorMessage), new(nameof(RequiredAttribute.AllowEmptyStrings), allowEmptyStrings) }; context.AddMultipleParameterAttribute <RequiredAttribute>(columnName, parameters); }
/// <summary> /// 增加多参数自定义标签泛型方法 /// </summary> /// <typeparam name="TAttribute"></typeparam> /// <param name="context"></param> /// <param name="columnName"></param> /// <param name="parameters"></param> public static void AddMultipleParameterAttribute <TAttribute>(this DynamicObjectContext context, string columnName, IEnumerable <KeyValuePair <string, object?> > parameters) where TAttribute : Attribute { var type = typeof(TAttribute); var propertyInfos = new List <PropertyInfo>(); var propertyValues = new List <object?>(); foreach (var kv in parameters) { var pInfo = type.GetProperty(kv.Key); if (pInfo != null) { propertyInfos.Add(pInfo); propertyValues.Add(kv.Value); } } context.AddAttribute(columnName, type, Type.EmptyTypes, Array.Empty <object>(), propertyInfos.ToArray(), propertyValues.ToArray()); }
/// <summary> /// 增加自定义标签泛型方法 /// </summary> /// <typeparam name="TAttribute"></typeparam> /// <param name="context"></param> /// <param name="columnName"></param> /// <param name="types"></param> /// <param name="constructorArgs"></param> /// <param name="propertyInfos"></param> /// <param name="propertyValues"></param> public static void AddAttribute <TAttribute>(this DynamicObjectContext context, string columnName, Type[] types, object?[] constructorArgs, PropertyInfo[]?propertyInfos = null, object?[]?propertyValues = null) where TAttribute : Attribute { var type = typeof(TAttribute); context.AddAttribute(columnName, type, types, constructorArgs, propertyInfos, propertyValues); }
/// <summary> /// 增加 DescriptionAttribute 扩展方法 /// </summary> /// <param name="context"></param> /// <param name="columnName"></param> /// <param name="description"></param> public static void AddDescriptionAttribute(this DynamicObjectContext context, string columnName, string description) => context.AddAttribute <DescriptionAttribute>(columnName, new Type[] { typeof(string) }, new object?[] { description });
/// <summary> /// 增加 DisplayNameAttribute 扩展方法 /// </summary> /// <param name="context"></param> /// <param name="columnName"></param> /// <param name="displayName"></param> public static void AddDisplayNameAttribute(this DynamicObjectContext context, string columnName, string displayName) => context.AddAttribute <DisplayNameAttribute>(columnName, new Type[] { typeof(string) }, new object?[] { displayName });
/// <summary> /// 增加 DisplayAttribute 扩展方法 /// </summary> /// <param name="context"></param> /// <param name="columnName"></param> /// <param name="parameters"></param> public static void AddDisplayAttribute(this DynamicObjectContext context, string columnName, IEnumerable <KeyValuePair <string, object?> > parameters) => context.AddMultipleParameterAttribute <DisplayAttribute>(columnName, parameters);