Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to know how I can extract all members of a class. I have a class and I have a function whose duty is saving a all members of some class variables(from the same class type) in a database. I know what is that class; but I don't want to update the function whenever I change the class.

For example this is my class:
public class user
{
     public string username= "";
     public string password= "";
     public bool KeepSignedIn= false;
     public int TryNumber= 3;
}


And this is my function:
void SaveInDatabase(params object[] classes_to_be_saved)
{
    // ????
    // ???? What should I write here to extract class members?
    // ???? (I have no problem with database. My problem in the class)
}


And my implemented codes:
user A= new user();
user B= new user();
user C= new user();

A.username="Alex";
A.password="ozon#74";

B.username="manfered";
B.password="zetaRx1388";
B.KeepSignedIn= true;
B.TryNumber=5;

C.username="perla";
C.password="anathema";

SaveInDatabase(A,B,C)


And the result will be:

usernamepasswordKeepSignedInTryNumber
Alex ozon#74 false 3
manfered zetaRx1388 true 5
perla anathema false 3



When I add another member to this class and call the function, the function adds another column to the result automatically(without updating the function regarding to that member).

CPallini wrote:
YOu may use reflection for the purpose, see [^].

Thanks very much.
This link was so useful:
http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo(VS.80).aspx
It solved my problem.
Posted
Updated 22-Mar-10 13:07pm
v4

YOu may use reflection for the purpose, see [^].
:)
 
Share this answer
 
You can use reflection to do this.
 
Share this answer
 
Given that you're trying to save an object's properties to a database, you may want to look at an ORM tool like Linq2SQL or NHibernate instead. I don't know your situation, so those tools may be overkill. If so, just use reflection as the other responses have suggested.

Object/Relational Mapper (ORM) tools do exactly the type of thing you are trying to do. They take your classes and map them to your database entities. ORMs handle more than just your simple property -> database column example. They generally can handle relationships, lazy or eager loading, and advanced queries.

Linq to SQL (also known as Linq2SQL) is built-in with the .Net 3.5 framework and allows you to use Linq queries against the database instead of straight SQL. You also get to use a drag-and-drop designer in Visual Studio to create the mappings between the database schema and your classes.

NHibernate is a free open source ORM tool you can find at https://www.nhibernate.org/[^]. It's mature and very powerful, but definitely has a steeper learning curve than Linq2SQL. There are many ways to do the mappings with NHibernate. You can chose between xml files, a fluent interface, or attributes applied to your classes. Queries can be make using the NHibernate criteria api, HQL (similar to SQL), or even Linq.

Naturally, there is far more to any ORM than what I can explain in a quick answer, so I recommend you Google ORM, Linq2SQL/Linq to SQL, and NHibernate for more information.
 
Share this answer
 
v2

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