/// <summary> /// Set value to object /// </summary> /// <param name="obj"></param> /// <param name="propName"></param> /// <param name="value"></param> /// <param name="dateFormat"></param> /// <returns></returns> public static bool SetObjectValue(object obj, string propName, string value, string dateFormat = null) { try { if (CommonUtil.IsNullOrEmpty(value)) { return(true); } if (obj != null) { PropertyInfo prop = obj.GetType().GetProperty(propName); if (prop.PropertyType == typeof(string)) { Dictionary <string, MaxTextLengthAttribute> miscAttr = CommonUtil.CreateAttributeDictionary <MaxTextLengthAttribute>(obj); foreach (KeyValuePair <string, MaxTextLengthAttribute> attr in miscAttr) { if (attr.Key == propName) { if (value.Length > attr.Value.MaxLength) { value = value.Substring(0, attr.Value.MaxLength); } break; } } } return(SetObjectValue(prop, obj, value, dateFormat)); } return(false); } catch (Exception ex) { throw ex; } }
private void ValidateAtLeast1Field(object obj) { try { List <ValidateMessage> msgLst = new List <ValidateMessage>(); Dictionary <string, AtLeast1FieldNotNullOrEmptyAttribute> langAttr = CommonUtil.CreateAttributeDictionary <AtLeast1FieldNotNullOrEmptyAttribute>(obj); foreach (KeyValuePair <string, AtLeast1FieldNotNullOrEmptyAttribute> attr in langAttr) { ValidateMessage nm = null; foreach (ValidateMessage m in msgLst) { if (m.Controller == attr.Value.Controller && m.Screen == attr.Value.Screen && m.Module == attr.Value.Module && m.Code == attr.Value.MessageCode.ToString()) { nm = m; break; } } if (nm == null) { nm = new ValidateMessage() { Controller = attr.Value.Controller, Screen = attr.Value.Screen, Module = attr.Value.Module, Code = attr.Value.MessageCode.ToString(), HasNullValue = true }; msgLst.Add(nm); } if (nm.HasNullValue == false) { continue; } PropertyInfo prop = obj.GetType().GetProperty(attr.Key); if (prop != null) { object val = prop.GetValue(obj, null); if (CommonUtil.IsNullOrEmpty(val) == false) { nm.HasNullValue = false; nm.ClearParameter(); } else if (attr.Value.UseControl == true) { nm.SetParameter(0, CommonUtil.IsNullOrEmpty(attr.Value.ControlName) ? attr.Key : attr.Value.ControlName); } } } if (msgLst.Count > 0) { foreach (ValidateMessage msg in msgLst) { if (msg.HasNullValue == true) { MessageUtil.MessageList msgCode; if (Enum.TryParse <MessageUtil.MessageList>(msg.Code, out msgCode)) { string template = CommonUtil.TextList(msg.Params == null? null : msg.Params.ToArray()); this.AddErrorMessage(msg.Controller, msg.Screen, msg.Module, msgCode, obj.GetHashCode().ToString(), "", template); this.IsValid = false; } } } } } catch (Exception) { throw; } }