private static void GetXMLForInsert(dynamic entity, XElement xElement, PropertyInfo[] infos, string tableName) { string idColumnName = string.Empty; string keyName = string.Empty; Type t = entity.GetType(); List <ColumnNameValue> nameValueList = new List <ColumnNameValue>(); infos.ToList <PropertyInfo>().ForEach ( x => { var customAttributes = x.GetCustomAttributes(false); if (customAttributes.Count() > 0) { if (customAttributes.Any(y => y.GetType().Name.Equals("PrimaryKey", StringComparison.OrdinalIgnoreCase))) { idColumnName = x.Name; } else if (customAttributes.Any(y => y.GetType().Name.Equals("ForeignKey", StringComparison.OrdinalIgnoreCase))) { var value = x.GetValue(entity, null); //if (value == null || value == (new Guid()) || value == 0) if (value == null || value == 0) { ColumnNameValue nv = new ColumnNameValue(x.Name, null, true); nameValueList.Add(nv); } else { ColumnNameValue nv = new ColumnNameValue(x.Name, value.ToString()); nameValueList.Add(nv); } } else if (customAttributes.Any(y => y.GetType().Name.Equals("KeyName", StringComparison.OrdinalIgnoreCase))) { keyName = x.GetValue(entity, null); } else if (!customAttributes.Any(y => y.GetType().Name.Equals("Exclude", StringComparison.OrdinalIgnoreCase))) { CreateNameValuePair(entity, nameValueList, x); } } else { CreateNameValuePair(entity, nameValueList, x); } } ); XUtility.AddXmlForInsert(xElement, tableName, idColumnName, keyName, nameValueList); }
private static void CreateNameValuePair(dynamic entity, List <ColumnNameValue> nameValueList, PropertyInfo x) { var value = x.GetValue(entity, null); if (value != null) { ColumnNameValue nv; if (value.GetType().Name == "DateTime") { int year = value.Year; int month = value.Month; int day = value.Day; nv = new ColumnNameValue(x.Name, string.Format("{0}-{1}-{2}", year, month, day)); } else { nv = new ColumnNameValue(x.Name, value.ToString()); } nameValueList.Add(nv); } }