Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a database with over 60 tables where I created a corresponding class that encapsulates a property for each column in the table. I want the classes to move data between UI & the database. I want to create a generic set of code to create an instance, create a list, and add class objects with the data. The following is 2 completely manual code sets that do this for 2 sized down "user" and "project" tables:
C#
string myName = "NNNNN";
string myPassword = "PPPPP";
DBUser dbUser = new DBUser();
List<dbuser> list1 = new List<dbuser>();
list1.Add(new DBUser { Name = myName, Password = myPassword });

string myDescLong = "LLLLL";
string myDescShort = "SSSSSS";
DBProject dbProject = new DBProject();
List<dbproject> list2 = new List<dbproject>();
list2.Add(new DBProject { Desc_long = myDescLong, Descr_short = myDescShort }); 


class DBProject
{
   private string desc_long;
   public string Desc_long
   {get { return this.desc_long; } set { this.desc_long = value; }}

   private string descr_short;
   public string Descr_short
   {get { return this.descr_short; } set { this.descr_short = value; } }
}

class DBUser
{
   private string password;
   public string Password
   {get { return this.password; } set { this.password = value; }
}
   private string name;
   public string Name
   {get { return this.name; } set { this.name = value; } }
}

Now to my question. How do I generalize this so I don;t have 58 additional sets of the instance, list, add to list code? Maybe it isn't possible?

Something like send the "DbUser" classname and it can dynamically create the code below?
C#
type DBGeneric = DBUser;
type Data = "All property data";

DBGeneric dbGeneric = new DBGeneric();
List<dbgeneric> listGeneric = new List<dbgeneric>();
listGeneric.Add(new DBGeneric { Data });
Posted
Updated 4-Sep-14 21:43pm
v3

1 solution

You can use this code as an example of how to create a dynamic generic data type:
C#
public object CreateGeneric(Type generic, Type innerType, params object[] args)
{
    Type specificType = generic.MakeGenericType(new Type[] { innerType });
    return Activator.CreateInstance(specificType, args);
}

Then call the method with the wanted data type.
In this case I created a list with int.
C#
List<int> list = (List<int>)CreateGeneric(typeof(List<>), typeof(int));
list.Add(2);
list.Add(3);


If you are not too stuck on your own classes, you can look into the class DataTable.
Together with the class DataAdapter it is very easy to extract data form a database and show in a data grid.
This is the more generic approach and will probably save you some work.
DataAdapter Class[^]
 
Share this answer
 
v2
Comments
CosmosNebula 5-Sep-14 10:30am    
Inserted the code in my application and there seems to be an issue with the "List<>". Any ideas? I will look into DataTable and DataAdapter. Thanks!
George Jonsson 6-Sep-14 3:00am    
I updated the code snippet so it actually works. :P
CosmosNebula 6-Sep-14 12:38pm    
Thanks. That was great!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900