List of objects in c# and get there name in loop

Jan 13 2014 1:12 AM
I am trying to make a list of objects in C#. The list of object i have to pass to a method which extracts the object name, object properties and values for each object from list. following are the code.
//Class B
public class ClassB
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public int Prop3 { get; set; }
public int Prop4 { get; set; }
}
// Class C
public class ClassC
{
public string ClassCProp1 { get; set; }
public string ClassCProp2 { get; set; }
public int ClassCProp3 { get; set; }
public int ClassCProp4 { get; set; }
}
 
List<object> objList = new List<object>();
List<ClassB> inputToCsv = new List<ClassB>();
for (int i = 0; i < 5; i++)
{
ClassB CB = new ClassB();
CB.Prop1 = i.ToString();
CB.Prop2 = i.ToString() + ") ClassB _ RDS";
CB.Prop3 = 100000 + i;
CB.Prop4 = 199999 + i;
inputToCsv.Add(CB);
}
objList.Add(inputToCsv);
 
List<ClassC> inputToClassC = new List<ClassC>();
for (int i = 0; i < 5; i++)
{
ClassC CB = new ClassC();
CB.ClassCProp1 = i.ToString();
CB.ClassCProp2 = i.ToString() + ")ClassC _ RDS";
CB.ClassCProp3 = 100000 + i;
CB.ClassCProp4 = 199999 + i;
inputToClassC.Add(CB);
}
objList.Add(inputToClassC);
 now i have to pass this "objList" object to next method. and catch the object name from list. Here I am able to get the name using "Type objtype = new objList..GetType()". But i m getting List1 as name. I want ClassB / ClassC as name of object can u plz suggest something using generic solution for this.

Answers (2)