public static T BinaryClone <T>(T source) { try { if (ReferenceEquals(source, null)) { return(default(T)); } var type = AnTypes.GetType(source, null); if ((type == null) || !type.IsSerializable) { throw new ArgumentException(ExceptionError, nameof(source)); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return((T)formatter.Deserialize(stream)); } } catch (Exception ex) { AnLog.Error(ex); throw; } }
public static string GenerateSelectQuery(string customerId, string universalNodeId, string dataSourceType, DateTime beginDateTime, DateTime endDateTime, bool processed, AnDatabaseTypes.DriverType driverType) { var result = ""; try { var whereClause = new StringBuilder(); whereClause.Append($"{nameof(RawDataRecord.Processed)} = {AnDatabaseTypes.FormatBooleanValue(processed, driverType)}"); if (!string.IsNullOrEmpty(customerId)) { whereClause.Append(" AND "); whereClause.Append($"{nameof(RawDataRecord.CustomerId)} = '{AnDatabaseTypes.FormatSingleQuote(AnDatabaseTypes.ValidStringLength(customerId, RawDataRecord.CustomerIdSize))}'"); } if (!string.IsNullOrEmpty(universalNodeId)) { whereClause.Append(" AND "); whereClause.Append($"{nameof(RawDataRecord.UniversalNodeId)} = '{AnDatabaseTypes.FormatSingleQuote(AnDatabaseTypes.ValidStringLength(universalNodeId, RawDataRecord.UniversalNodeIdSize))}'"); } if (!string.IsNullOrEmpty(dataSourceType)) { whereClause.Append(" AND "); whereClause.Append($"{nameof(RawDataRecord.DataSourceType)} = '{AnDatabaseTypes.FormatSingleQuote(AnDatabaseTypes.ValidStringLength(dataSourceType, RawDataRecord.DataSourceTypeSize))}'"); } if ((DateTime.Compare(beginDateTime, AnTypes.MinDateTimeValue) > 0) && (DateTime.Compare(endDateTime, beginDateTime) > 0)) { whereClause.Append(" AND "); whereClause.Append($"{nameof(RawDataRecord.EnteredDateTime)} > '{AnTypes.SqlDateTimeString(beginDateTime)}'"); whereClause.Append(" AND "); whereClause.Append($"{nameof(RawDataRecord.EnteredDateTime)} <= '{AnTypes.SqlDateTimeString(endDateTime)}'"); } whereClause.Append(" ORDER BY "); whereClause.Append(AnDatabaseTypes.FormatDatabaseColumnName(nameof(RawDataRecord.Id), RawDataRecord.DbTableName, AnDatabaseTypes.ExecutionType.Query, driverType)); result = AnDatabaseTypes.GenerateSelectQuery(driverType, RawDataRecord.DbTableName, AnDatabaseTypes.FormatColumnNames(RawDataRecord.DbTableName, new RawDataRecord().GetColumnNames(true), driverType), whereClause.ToString()); } catch (Exception ex) { AnLog.Error(ex); } return(result); }
public static T JsonClone <T>(T source, List <JsonConverter> jsonConverters = null) { try { if (ReferenceEquals(source, null)) { return(default(T)); } var type = AnTypes.GetType(source, null); if ((type == null) || !type.IsSerializable) { throw new ArgumentException(ExceptionError, nameof(source)); } var jsonString = new StringBuilder(); var jsonSerializer = new JsonSerializer(); if ((jsonConverters != null) && (jsonConverters.Count > 0)) { foreach (var jsonConverter in jsonConverters.Where(jsonConverter => jsonConverter != null)) { jsonSerializer.Converters.Add(jsonConverter); } } using (var stringWriter = new StringWriter(jsonString)) { jsonSerializer.Serialize(stringWriter, source); using (var stringReader = new StringReader(jsonString.ToString())) { return((T)jsonSerializer.Deserialize(stringReader, type)); } } } catch (Exception ex) { AnLog.Error(ex); throw; } }
private static Type GenericOfClassType(string fullClassName) { Type result = null; try { var subClassName = GenericOfClassName(fullClassName); result = AnTypes.GetType(!string.IsNullOrEmpty(subClassName) ? subClassName : fullClassName, null); } catch (Exception ex) { AnLog.Error(ex); } return(result); }
public static bool Exists <T>(T value, int id) { try { if (value != null) { var classType = AnTypes.GetType(value, null); if (classType != null) { foreach (var property in classType.GetProperties()) { var propertyType = property.PropertyType; if (property.PropertyType.IsEnum) { propertyType = AnReflectionCache.GetUnderlyingType(propertyType); } var key = property.Name.ToLower(); if ((Type.GetTypeCode(propertyType) == TypeCode.Int32) && (key == "id")) { var localId = (int)property.GetValue(value, null); if (localId == id) { return(true); } } } } } } catch (Exception ex) { AnLog.Error(ex); } return(false); }
public static T XmlClone <T>(T source, Type[] types) { try { if (ReferenceEquals(source, null)) { return(default(T)); } var type = AnTypes.GetType(source, null); if ((type == null) || !type.IsSerializable) { throw new ArgumentException(ExceptionError, nameof(source)); } var xmlSerializer = new XmlSerializer(type, types); Stream stream = new MemoryStream(); using (stream) { xmlSerializer.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); // Uncomment these for debugging. // var streamReader = new StreamReader(stream); // var xmlContents = streamReader.ReadToEnd(); // stream.Seek(0, SeekOrigin.Begin); return((T)xmlSerializer.Deserialize(stream)); } } catch (Exception ex) { AnLog.Error(ex); throw; } }
public static string ToXml <T>(T source, Type[] types) { var xmlString = ""; try { if (!ReferenceEquals(source, null)) { var type = AnTypes.GetType(source, null); if ((type == null) || !type.IsSerializable) { throw new ArgumentException(ExceptionError, nameof(source)); } var xmlSerializer = new XmlSerializer(type, types); Stream stream = new MemoryStream(); using (stream) { xmlSerializer.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(stream); xmlString = streamReader.ReadToEnd(); } } } catch (Exception ex) { AnLog.Error(ex); throw; } return(xmlString); }
private static void GetClasses(Type parentClassType, IDictionary <string, Type> classes) { try { if ((parentClassType != null) && (classes != null)) { var properties = AnReflectionCache.GetPropertyInfoList(parentClassType); if (properties?.Count > 0) { foreach (var property in properties.Where(prop => (prop?.Property != null) && (prop.Attribute != null))) { var type = property.Property.PropertyType; if (property.Property.PropertyType.IsEnum) { type = AnReflectionCache.GetUnderlyingType(type); } if (!(AnSerialization.SerialSizeDictionary.ContainsKey(type) || (type == typeof(string)) || (type == typeof(byte[])))) { if (!string.IsNullOrEmpty(type.BaseType?.Name) && (type.BaseType?.Name ?? "").ToLower().Contains("anbaseobjectlist")) { if (!string.IsNullOrEmpty(type.BaseType?.FullName)) { var subClassName = GenericOfClassName(type.BaseType?.FullName); if (!string.IsNullOrEmpty(subClassName)) { if (!classes.ContainsKey(subClassName)) { var subType = AnTypes.GetType(subClassName, null); if (subType != null) { classes.Add(subClassName, subType); GetClasses(subType, classes); } } } } } else { if (!string.IsNullOrEmpty(type.FullName) && !classes.ContainsKey(type.FullName)) { classes.Add(type.FullName, type); GetClasses(type, classes); } } } } } } } catch (Exception ex) { AnLog.Error(ex); } }
public static Dictionary <string, Type> GetFieldNamesAndDataTypes <T>(T t) { var result = new Dictionary <string, Type>(); try { if (t != null) { var classType = AnTypes.GetType(t, null); if (classType != null) { var properties = AnReflectionCache.GetPropertyInfoArray(classType); if (properties?.Length > 0) { foreach (var property in properties) { var propertyType = property.PropertyType; if (property.PropertyType.IsEnum) { propertyType = AnReflectionCache.GetUnderlyingType(propertyType); } Type type; if (AnSerialization.SerialSizeDictionary.ContainsKey(propertyType) || (propertyType == typeof(string)) || (propertyType == typeof(byte[]))) { type = propertyType; } else { if (propertyType.IsGenericType && (propertyType.GetGenericTypeDefinition() == typeof(List <>))) { type = GenericOfClassType(propertyType.FullName); } else if ((propertyType.BaseType != null) && propertyType.BaseType.IsGenericType && (propertyType.BaseType.GetGenericTypeDefinition() == typeof(List <>))) { type = GenericOfClassType(propertyType.BaseType.FullName); } else { type = GenericOfClassType(propertyType.FullName); } } if (type != null) { result.Add(property.Name, type); } } } } } } catch (Exception ex) { AnLog.Error(ex); } return(result); }
public static T FromDictionary <T>(IDictionary <string, object> dictionary) where T : new() { var result = new T(); try { if ((dictionary != null) && (dictionary.Count > 0)) { dictionary = AnTypes.LowerCaseKeys(dictionary); var classType = AnTypes.GetType(result, null); if (classType != null) { var properties = AnReflectionCache.GetPropertyInfoArray(classType); if (properties?.Length > 0) { foreach (var property in properties.Where(property => dictionary.ContainsKey(property.Name.ToLower()))) { var propertyType = property.PropertyType; if (property.PropertyType.IsEnum) { propertyType = AnReflectionCache.GetUnderlyingType(propertyType); } var key = property.Name.ToLower(); object value = null; if (AnSerialization.SerialSizeDictionary.ContainsKey(propertyType)) { value = AnSafeConvert.ChangeType(dictionary[key], propertyType); } else if (propertyType == typeof(string)) { value = AnSafeConvert.ToString(dictionary[key]); } else if (propertyType == typeof(byte[])) { value = dictionary[key] as byte[]; } else { if (dictionary[key] is Dictionary <string, object> subDictionary) { var fromDictionary = AnReflectionCache.GetFromDictionaryMethod(typeof(AnCloneUtility)); if (fromDictionary != null) { var genericListItemType = GenericOfClassType(propertyType.FullName); if (genericListItemType != null) { fromDictionary = fromDictionary.MakeGenericMethod(genericListItemType); value = fromDictionary.Invoke(null, new object[] { subDictionary }); } } } else { if ((dictionary[key] is List <Dictionary <string, object> > dictionaryList) && (dictionaryList.Count > 0)) { if (propertyType.IsGenericType && (propertyType.GetGenericTypeDefinition() == typeof(List <>))) { var genericListItemType = GenericOfClassType(propertyType.FullName); if (genericListItemType != null) { var fromDictionaryList = AnReflectionCache.GetFromDictionaryListMethod(typeof(AnCloneUtility)); if (fromDictionaryList != null) { fromDictionaryList = fromDictionaryList.MakeGenericMethod(genericListItemType); value = fromDictionaryList.Invoke(null, new object[] { dictionaryList }); } } } else if ((propertyType.BaseType != null) && propertyType.BaseType.IsGenericType && (propertyType.BaseType.GetGenericTypeDefinition() == typeof(List <>))) { var genericListItemType = GenericOfClassType(propertyType.BaseType.FullName); if (genericListItemType != null) { var fromDictionaryList = AnReflectionCache.GetFromDictionaryListMethod(typeof(AnCloneUtility)); if (fromDictionaryList != null) { fromDictionaryList = fromDictionaryList.MakeGenericMethod(genericListItemType); var listObject = fromDictionaryList.Invoke(null, new object[] { dictionaryList }); if (listObject != null) { value = AnTypes.CreateItemInstance(propertyType.FullName, propertyType, new[] { listObject }); } } } } else { var fromDictionary = AnReflectionCache.GetFromDictionaryMethod(typeof(AnCloneUtility)); if (fromDictionary != null) { var genericListItemType = GenericOfClassType(propertyType.FullName); if (genericListItemType != null) { fromDictionary = fromDictionary.MakeGenericMethod(genericListItemType); value = fromDictionary.Invoke(null, new object[] { dictionaryList[0] }); } } } } } } property.SetValue(result, value); } } } } } catch (Exception ex) { AnLog.Error(ex); } return(result); }
public static Dictionary <string, object> ToDictionary <T>(T t) { var result = new Dictionary <string, object>(); try { if (t != null) { var classType = AnTypes.GetType(t, null); if (classType != null) { var properties = AnReflectionCache.GetPropertyInfoArray(classType); if (properties?.Length > 0) { foreach (var property in properties) { var propertyType = property.PropertyType; if (property.PropertyType.IsEnum) { propertyType = AnReflectionCache.GetUnderlyingType(propertyType); } if (!string.IsNullOrEmpty(propertyType.FullName)) { object value = null; if (AnSerialization.SerialSizeDictionary.ContainsKey(propertyType) || (propertyType == typeof(string)) || (propertyType == typeof(byte[]))) { value = property.GetValue(t, null); } else { if (propertyType.IsGenericType && (propertyType.GetGenericTypeDefinition() == typeof(List <>))) { var toDictionaryList = AnReflectionCache.GetToDictionaryListMethod(typeof(AnCloneUtility)); if (toDictionaryList != null) { var genericListItemType = GenericOfClassType(propertyType.FullName); if (genericListItemType != null) { toDictionaryList = toDictionaryList.MakeGenericMethod(genericListItemType); value = toDictionaryList.Invoke(null, new[] { property.GetValue(t, null) }); } } } else if ((propertyType.BaseType != null) && propertyType.BaseType.IsGenericType && (propertyType.BaseType.GetGenericTypeDefinition() == typeof(List <>))) { var toDictionaryList = AnReflectionCache.GetToDictionaryListMethod(typeof(AnCloneUtility)); if (toDictionaryList != null) { var genericListItemType = GenericOfClassType(propertyType.BaseType.FullName); if (genericListItemType != null) { toDictionaryList = toDictionaryList.MakeGenericMethod(genericListItemType); value = toDictionaryList.Invoke(null, new[] { property.GetValue(t, null) }); } } } else { var toDictionary = AnReflectionCache.GetToDictionaryMethod(typeof(AnCloneUtility)); if (toDictionary != null) { var genericListItemType = GenericOfClassType(propertyType.FullName); if (genericListItemType != null) { toDictionary = toDictionary.MakeGenericMethod(genericListItemType); value = toDictionary.Invoke(null, new[] { property.GetValue(t, null) }); } } } } result.Add(property.Name.ToLower(), value); } } } } } } catch (Exception ex) { AnLog.Error(ex); } return(result); }
public static Dictionary <string, object> ToDictionary <T>(T modelObject) { var result = new Dictionary <string, object>(); try { if (modelObject != null) { var defaultObject = Activator.CreateInstance <T>(); var type = typeof(T); var dbTableName = ""; if (DbTableNameCache.ContainsKey(type)) { dbTableName = DbTableNameCache[type]; } else { var getDbTableNameMethod = type.GetMethod(GetDbTableNameMethodName); if (getDbTableNameMethod != null) { dbTableName = AnSafeConvert.ToString(getDbTableNameMethod.Invoke(defaultObject, null)); if (!string.IsNullOrEmpty(dbTableName)) { DbTableNameCache.Add(type, dbTableName); } } } if (!string.IsNullOrEmpty(dbTableName)) { var columnData = (defaultObject as AnModelBase)?.GenerateDatabaseCommandDataList(true); if (columnData?.Count > 0) { foreach (var column in columnData.Where(column => !string.IsNullOrEmpty(column?.Name))) { var value = type.GetProperty(column.Name)?.GetValue(modelObject, null); switch (column.DataType) { case AnTypes.DataType.Character: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToChar(value)); break; case AnTypes.DataType.Byte: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToByte(value)); break; case AnTypes.DataType.Short: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToInt16(value)); break; case AnTypes.DataType.UShort: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToUInt16(value)); break; case AnTypes.DataType.Int: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToInt32(value)); break; case AnTypes.DataType.UInt: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToUInt32(value)); break; case AnTypes.DataType.Long: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToInt64(value)); break; case AnTypes.DataType.ULong: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToUInt64(value)); break; case AnTypes.DataType.Single: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToSingle(value)); break; case AnTypes.DataType.Double: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToDouble(value)); break; case AnTypes.DataType.Decimal: case AnTypes.DataType.Currency: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToDecimal(value)); break; case AnTypes.DataType.Boolean: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToBoolean(value)); break; case AnTypes.DataType.DateTime: case AnTypes.DataType.FileTime: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToDateTime(value)); break; case AnTypes.DataType.Guid: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToGuid(value)); break; case AnTypes.DataType.Char: case AnTypes.DataType.VarChar: case AnTypes.DataType.Text: case AnTypes.DataType.NChar: case AnTypes.DataType.NVarChar: case AnTypes.DataType.NText: AnTypes.SetDictionaryValue(result, column.Name, AnSafeConvert.ToString(value)); break; case AnTypes.DataType.Image: case AnTypes.DataType.Binary: case AnTypes.DataType.VarBinary: AnTypes.SetDictionaryValue(result, column.Name, value as byte[]); break; case AnTypes.DataType.Table: { if (!ToDictionaryListMethodCache.ContainsKey(dbTableName)) { ToDictionaryListMethodCache.Add(dbTableName, new Dictionary <string, MethodInfo>()); } if (!ToDictionaryListMethodCache[dbTableName].ContainsKey(column.Name)) { ToDictionaryListMethodCache[dbTableName].Add(column.Name, null); } if (ToDictionaryListMethodCache[dbTableName][column.Name] == null) { if (!PropertyTypeCache.ContainsKey(dbTableName)) { PropertyTypeCache.Add(dbTableName, new Dictionary <string, Type>()); } if (!PropertyTypeCache[dbTableName].ContainsKey(column.Name)) { PropertyTypeCache[dbTableName].Add(column.Name, null); } if (PropertyTypeCache[dbTableName][column.Name] == null) { PropertyTypeCache[dbTableName][column.Name] = type.GetProperty(column.Name)?.PropertyType; } if (PropertyTypeCache[dbTableName][column.Name] != null) { ToDictionaryListMethodCache[dbTableName][column.Name] = PropertyTypeCache[dbTableName][column.Name].GetMethod(ToDictionaryListMethodName); } } if (ToDictionaryListMethodCache[dbTableName][column.Name] != null) { AnTypes.SetDictionaryValue(result, column.Name, ToDictionaryListMethodCache[dbTableName][column.Name].Invoke(null, new[] { value })); } } break; //case AnTypes.DataType.Null: //default: //break; } } } } } } catch (Exception ex) { AnLog.Error(ex); } return(result); }