protected virtual bool LoadFromDatabase(ResultRow row) { if (CacheProperties() == false) { return(false); } var dict = mDatabaseProperties[GetType()]; foreach (KeyValuePair <PropertyInfo, StoreableDatabaseObjectAttribute> kvp in dict) { StoreableDatabaseObjectAttribute att = kvp.Value; PropertyInfo prop = kvp.Key; string dbName = att.DatabaseName; if (row.Table.Columns.Contains(dbName) == false) { ServerConsole.ErrorLine("Column '{0}' not found in table!", dbName); continue; } object value = row[dbName].Value; Type valueType = value.GetType(); // Catch value "null" if (value is DBNull) { value = null; } // Enum's need the exact Enum value.. if (prop.PropertyType.IsEnum == true) { Type enumType = prop.PropertyType; value = Enum.ToObject(enumType, (value == null ? 0 : value)); } prop.SetValue(this, value, null); } return(true); }
protected virtual List <string> GetUpdateParams() { List <string> updateParams = new List <string>(); var dict = mDatabaseProperties[GetType()]; foreach (KeyValuePair <PropertyInfo, StoreableDatabaseObjectAttribute> kvp in dict) { StoreableDatabaseObjectAttribute att = kvp.Value; // Dont update prop? if (att.NoUpdate == true) { continue; } PropertyInfo prop = kvp.Key; string dbName = att.DatabaseName; object value = null; value = prop.GetValue(this, null); // If type is null, no conversion is needed if (att.Type != null) { try { object valueNew = Convert.ChangeType(value, att.Type); // Conversion done without exception, copy back value = valueNew; valueNew = null; // Cleanup } catch { ServerConsole.ErrorLine("Failed to convert value " + value + " (" + value.GetType() + ") to " + att.Type); // Skip if failed to convert! continue; } } updateParams.Add("`" + dbName + "` = '" + value + "'"); } return(updateParams); }