public void ToObject(object obj, List <SqlParameter> sqlParameters = null) { if (sqlParameters.Where(s => s.Direction == ParameterDirection.Output || s.Direction == ParameterDirection.InputOutput).Any()) { var props = ( from p in obj.GetType().GetProperties() let nameAttr = p.GetCustomAttributes(typeof(QueryParamAttribute), true) let ignoreAttr = p.GetCustomAttributes(typeof(QueryParamIgnoreAttribute), true) select new { Property = p, Names = nameAttr, Ignores = ignoreAttr }).ToList(); List <KeyValuePair <string, string> > flds = new List <KeyValuePair <string, string> >(); props.ForEach(p => { if (p.Ignores != null && p.Ignores.Length > 0) { return; } QueryParamAttribute name = p.Names.FirstOrDefault() as QueryParamAttribute; if (name == null) { return; } QueryParamInfo pinfo = new QueryParamInfo(); if (name != null && !string.IsNullOrWhiteSpace(name._name)) { pinfo.Name = name._name.Replace("@", ""); } else { pinfo.Name = p.Property.Name.Replace("@", ""); } flds.Add(new KeyValuePair <string, string>(p.Property.Name.Replace("@", ""), pinfo.Name)); }); foreach (SqlParameter item in sqlParameters.Where(s => s.Direction == ParameterDirection.Output || s.Direction == ParameterDirection.InputOutput)) { PropertyInfo prop = obj.GetType().GetProperty(flds.Where(f => f.Value == item.ParameterName).FirstOrDefault().Key, BindingFlags.Public | BindingFlags.Instance); if (prop != null && prop.CanWrite) { if (item.Value == DBNull.Value) { prop.SetValue(obj, null); } else { prop.SetValue(obj, item.Value); } } } } }
public List <SqlParameter> ToSqlParamsList(object obj, SqlParameter[] additionalParams = null) { var props = ( from p in obj.GetType().GetProperties() let nameAttr = p.GetCustomAttributes(typeof(QueryParamAttribute), true) let ignoreAttr = p.GetCustomAttributes(typeof(QueryParamIgnoreAttribute), true) select new { Property = p, Names = nameAttr, Ignores = ignoreAttr }).ToList(); List <SqlParameter> result = new List <SqlParameter>(); props.ForEach(p => { if (p.Ignores != null && p.Ignores.Length > 0) { return; } QueryParamAttribute name = p.Names.FirstOrDefault() as QueryParamAttribute; if (name == null) { return; } QueryParamInfo pinfo = new QueryParamInfo(); if (name != null && !string.IsNullOrWhiteSpace(name._name)) { pinfo.Name = name._name.Replace("@", ""); } else { pinfo.Name = p.Property.Name.Replace("@", ""); } pinfo.Value = p.Property.GetValue(obj) ?? DBNull.Value; if (name._isStructured) { object data = p.Property.GetValue(obj) ?? DBNull.Value; List <dynamic> dlist = new List <dynamic>(); string json = JsonConvert.SerializeObject(data); pinfo.Value = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable))); SqlParameter sqlParam = new SqlParameter(pinfo.Name, SqlDbType.Structured) { TypeName = name._dataType, Value = pinfo.Value, Direction = (ParameterDirection)name._direction }; if ((name._direction == Direction.InputOutput || name._direction == Direction.Output) || pinfo.Value != DBNull.Value) { result.Add(sqlParam); } } else { SqlParameter sqlParam = new SqlParameter(pinfo.Name, (name._dbType != null ? (SqlDbType)name._dbType : TypeConvertor.ToSqlDbType(p.Property.PropertyType))) { Value = pinfo.Value, Direction = (ParameterDirection)name._direction }; if (name._size != 0) { sqlParam.Size = name._size; } if (name._precision > 0) { sqlParam.Precision = name._precision; } if (name._scale > 0) { sqlParam.Scale = name._scale; } if ((name._direction == Direction.InputOutput || name._direction == Direction.Output) || pinfo.Value != DBNull.Value) { result.Add(sqlParam); } } }); if (additionalParams != null && additionalParams.Length > 0) { result.AddRange(additionalParams); } return(result); }