how can i acquire the values of an anonymous collection of objects?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • faelnefal
    New Member
    • Nov 2008
    • 16

    #1

    how can i acquire the values of an anonymous collection of objects?

    i have this code...

    Code:
       List<Sample> list = new List<Sample>();
                list.Add(new Sample("balloons"));
                list.Add(new Sample("candies"));
    
                IEnumerable sample = list;
    
                Type type = sample.GetType().GetGenericArguments()[0];
                FieldInfo[] fieldInfos = type.GetFields();
                PropertyInfo[] propertyInfos = type.GetProperties();
    
                foreach (var x in sample)
                {
                    Type a = x.GetType();
                    Console.WriteLine( a.GetField(fieldInfos[0].Name).GetValue(a));
                    
                }
    and i have this class

    Code:
     class Sample
        {
            public string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public Sample(string name)
            {
                this.name = name;
            }
        }
    i want to be able to display the contents of the anonymous IEnumerable even though i do not know its field names... can anybody help me?
Working...