/// <summary> /// </summary> /// <param name="itemInstanceTypeToCreate">The type of DatabaseObjects.DatabaseObject to create.</param> /// <param name="databaseObjects">Parameter that is passed to the constructor of the DatabaseObject to create. If there is a default constructor then this argument is not used.</param> public static IDatabaseObject CreateItemInstance(Type itemInstanceTypeToCreate, DatabaseObjects databaseObjects) { object objObjectInstance = null; foreach (ConstructorInfo objConstructor in itemInstanceTypeToCreate.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { ParameterInfo[] objConstructorParameters = objConstructor.GetParameters(); if (objConstructorParameters.Length == 0) { objObjectInstance = objConstructor.Invoke(null); break; } else if (objConstructorParameters.Length == 1 && (objConstructorParameters[0].ParameterType.IsSubclassOf(typeof(DatabaseObjects)) || objConstructorParameters[0].ParameterType.Equals(typeof(DatabaseObjects)))) { objObjectInstance = objConstructor.Invoke(new[] {databaseObjects}); break; } } if (objObjectInstance == null) throw new Exceptions.DatabaseObjectsException("An empty constructor or constructor with argument DatabaseObjects.DatabaseObjects (or subclass) could not be found for type '" + itemInstanceTypeToCreate.FullName + "'. This type has been specified by the ItemInstanceAttribute for the type '" + databaseObjects.GetType().FullName + "' or as the T argument."); else if (!(objObjectInstance is IDatabaseObject)) throw new Exceptions.DatabaseObjectsException("'" + itemInstanceTypeToCreate.FullName + "' does not implement IDatabaseObject or inherit from DatabaseObject. Type was specified for use by the ItemInstanceAttribute on the type '" + databaseObjects.GetType().FullName + "'"); else return (IDatabaseObject)objObjectInstance; }
public DatabaseObjectsUsingAttributesHelper(DatabaseObjects objDatabaseObjects) { if (objDatabaseObjects == null) throw new ArgumentNullException(); pobjDatabaseObjects = objDatabaseObjects; object[] objAttributes = objDatabaseObjects.GetType().GetCustomAttributes(true); if (objAttributes != null) { foreach (Attribute objAttribute in objAttributes) { if (objAttribute is DistinctFieldAttribute) pobjDistinctField = (DistinctFieldAttribute) objAttribute; else if (objAttribute is KeyFieldAttribute) pobjKeyField = (KeyFieldAttribute) objAttribute; else if (objAttribute is OrderByFieldAttribute) pobjOrderByAttributes.Add((OrderByFieldAttribute) objAttribute); else if (objAttribute is SubsetAttribute) pobjSubset = (SubsetAttribute) objAttribute; else if (objAttribute is TableAttribute) pobjTable = (TableAttribute) objAttribute; else if (objAttribute is TableJoinAttribute) pobjTableJoins.Add((TableJoinAttribute) objAttribute); else if (objAttribute is ItemInstanceAttribute) pobjItemInstance = (ItemInstanceAttribute) objAttribute; } } }