In one of my coding requirement i need to map database fields to fields which user have been created self. I think a bit more on this and come with finally solution get gain of Reflection for fulfilling the requirement.
Here is how i get the list of Properties which i can map easily to User table to database, i used the GetProperties() method to achieve this things, then i do the Sorting on them to show in Sorted order, below is code snippet for this,
39 User objUser = new User();
40 Type t = objUser.GetType();
41 PropertyInfo[] pi = t.GetProperties();
42 Array.Sort(pi,
43 delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
44 { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });
45
46 UserProperties = new List<string>();
47 foreach (MemberInfo mi in pi)
48 {
49 UserProperties.Add(mi.Name);
50 }
here User is my class which contains the properties of "User" table in database.
Reflections provides vast collections of Methods which we can utilize to access within code.
Happy Coding :)
No comments:
Post a Comment