Friday, September 10, 2010

Get list of variables names in class

Sometimes we have lots of fields in a class and we want the field names.. in that situation we can get list of all field names using below code:
in below code "YourType" will be your class name of which fields name list you want.
for vs2008:
var fields = typeof(YourType).GetFields(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var names = Array.ConvertAll(fields, field => field.Name);

for vs2005:
FieldInfo[] fields = typeof(YourType).GetFields(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
string[] names = Array.ConvertAll(fields,
delegate(FieldInfo field) { return field.Name; });




Solution by:Rajesh Rolen

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates