/// -------------------------------------------------------------------------------- /// <summary> /// Recurses through the lowest order base classes up to the highest order classes loading fields /// and hooked objects on each object. /// </summary> /// -------------------------------------------------------------------------------- public static SQL.SQLFieldValues SaveFieldsForBaseTypes(object objObject, System.Type objType) { SQL.SQLFieldValues objFieldValues = new SQL.SQLFieldValues(); //Need to check that the type is not at the object level which can occur when traversing through hooked objects if (!objType.Assembly.Equals(System.Reflection.Assembly.GetExecutingAssembly()) && !objType.Equals(typeof(object))) { objFieldValues.Add(SaveFieldsForBaseTypes(objObject, objType.BaseType)); objFieldValues.Add(SaveFieldsForObject(objObject, objType)); objFieldValues.Add(SaveFieldsForHookedObjects(objObject, objType)); } return objFieldValues; }
private static SQL.SQLFieldValues SaveFieldsForHookedObjects(object objObject, System.Type objType) { object[] objAttributes; object objFieldObject; SQL.SQLFieldValues objFieldValues = new SQL.SQLFieldValues(); //Search for fields that have the FieldMappingObjectHookAttribute foreach (System.Reflection.FieldInfo objField in objType.GetFields(pcePropertyFieldScope)) { objAttributes = objField.GetCustomAttributes(typeof(FieldMappingObjectHookAttribute), true); if (objAttributes != null) { foreach (FieldMappingObjectHookAttribute objFieldMappingObjectHook in objAttributes) { if (objField.FieldType.IsValueType) throw new Exceptions.DatabaseObjectsException("Field " + objField.FullName() + " marked with FieldMappingObjectHook attribute on value type - must be a class type"); else { objFieldObject = objField.GetValue(objObject); if (objFieldObject == null) throw new Exceptions.DatabaseObjectsException("Field " + objField.FullName() + " marked with " + typeof(FieldMappingObjectHookAttribute).Name + " is Nothing"); objFieldValues.Add(SaveFieldsForBaseTypes(objFieldObject, objFieldObject.GetType())); } } } } //Search for properties that have the FieldMappingObjectHookAttribute foreach (System.Reflection.PropertyInfo objProperty in objType.GetProperties(pcePropertyFieldScope)) { objAttributes = objProperty.GetCustomAttributes(typeof(FieldMappingObjectHookAttribute), true); if (objAttributes != null) { foreach (FieldMappingObjectHookAttribute objFieldMappingObjectHook in objAttributes) { if (objProperty.CanRead) { if (objProperty.PropertyType.IsValueType) throw new Exceptions.DatabaseObjectsException("Property " + objProperty.FullName() + " marked with FieldMappingObjectHook attribute on value type - must be a class type"); else { objFieldObject = objProperty.GetValue(objObject, null); if (objFieldObject == null) throw new Exceptions.DatabaseObjectsException("Property " + objProperty.FullName() + " marked with " + typeof(FieldMappingObjectHookAttribute).Name + " is Nothing"); objFieldValues.Add(SaveFieldsForBaseTypes(objFieldObject, objFieldObject.GetType())); } } } } } return objFieldValues; }
/// -------------------------------------------------------------------------------- /// <summary> /// Saves an object with fields or properties marked with DatabaseObjects.FieldMapping attributes /// to the fields values, which can be used to save to the database. /// </summary> /// -------------------------------------------------------------------------------- private static SQL.SQLFieldValues SaveFieldsForObject(object objObject, System.Type objType) { object[] objAttributes; SQL.SQLFieldValues objFieldValues = new SQL.SQLFieldValues(); //Search for fields that have the FieldMappingAttribute foreach (System.Reflection.FieldInfo objField in objType.GetFields(pcePropertyFieldScope)) { objAttributes = objField.GetCustomAttributes(typeof(FieldMappingAttribute), true); if (objAttributes != null) { foreach (FieldMappingAttribute objFieldMapping in objAttributes) { try { //If an enum field then convert the enum to an integer if (objField.FieldType.IsEnum) objFieldValues.Add(objFieldMapping.FieldName, System.Convert.ToInt32(objField.GetValue(objObject))); else if (objField.GetValue(objObject) is ObjectReference) //Get the distinct value if this is an ObjectReference field objFieldValues.Add(objFieldMapping.FieldName, ((ObjectReference) (objField.GetValue(objObject))).DistinctValue); else objFieldValues.Add(objFieldMapping.FieldName, objField.GetValue(objObject)); } catch (Exception ex) { throw new Exceptions.DatabaseObjectsException("Field '" + objField.FullName() + "' could not be read.", ex); } } } } //Search for properties that have the FieldMappingAttribute foreach (System.Reflection.PropertyInfo objProperty in objType.GetProperties(pcePropertyFieldScope)) { objAttributes = objProperty.GetCustomAttributes(typeof(FieldMappingAttribute), true); if (objAttributes != null) { foreach (FieldMappingAttribute objFieldMapping in objAttributes) { if (objProperty.CanRead) { try { //If an enum field then convert the enum to an integer if (objProperty.PropertyType.IsEnum) objFieldValues.Add(objFieldMapping.FieldName, System.Convert.ToInt32(objProperty.GetValue(objObject, null))); else objFieldValues.Add(objFieldMapping.FieldName, objProperty.GetValue(objObject, null)); } catch (Exception ex) { throw new Exceptions.DatabaseObjectsException("Property '" + objProperty.FullName() + "' could not be read.", ex); } } } } } return objFieldValues; }
private static SQL.SQLFieldValues FieldValuesFromDataReader(IDatabaseObjects objCollection, IDataReader objReader) { string strFieldName; string strTablePrefix; SQL.SQLFieldValues objFieldValues; //check that the distinct field name exists if (!FieldExists(objReader, objCollection.DistinctFieldName())) throw new Exceptions.DatabaseObjectsException(System.Convert.ToString(((object) objCollection).GetType().Name + ".DistinctFieldName '" + objCollection.DistinctFieldName() + "' is invalid")); objFieldValues = new SQL.SQLFieldValues(); strTablePrefix = objCollection.TableName() + "."; //Copy the recordset values into the SQL.SQLFieldValues object for (int intIndex = 0; intIndex < objReader.FieldCount; intIndex++) { //If the recordset has been loaded with a join then it may be prefixed with //the table name - this is the case with Microsoft Access //If so remove the table name if the table prefix is the same as objCollection.TableName //All of the other joined fields with tablename prefixes on the fields will remain. This is ok considering //most of the time an inner join has been performed where the field names are equal in the 2 joined tables strFieldName = objReader.GetName(intIndex); if (strFieldName.IndexOf(strTablePrefix) == 0) objFieldValues.Add(strFieldName.Substring(strTablePrefix.Length), objReader[intIndex]); else objFieldValues.Add(strFieldName, objReader[intIndex]); } return objFieldValues; }