DataAccess.CreateObjectFromDB C# (CSharp) Method

CreateObjectFromDB() public method

public CreateObjectFromDB ( Type objType ) : void
objType Type
return void
    public void CreateObjectFromDB(Type objType)
    {
        //instantiate an object of the type specified
        object instance = Activator.CreateInstance(objType, true);

        //get all properties of the class specified
        PropertyInfo[] properties = objType.GetProperties();

        for (int i = 0; i < properties.Length; i++)
        {
            //add the DBColumnAttributes for each property
            DBColumnAttribute[] attribute = (DBColumnAttribute[])properties[i].GetCustomAttributes(typeof(DBColumnAttribute), true);

            //make sure the property has a column attribute
            if (attribute.Length > 0)
            {
                //fill in the value for the property
                object value = reader[attribute[0].Name];
                properties[i].SetValue(instance, value, null);
            }
        }
    }