求各位大神详细解答:
    我现在定义了很多个图形的类,比如Triangle,Rectangular,Circle等,这些类有坐标,大小等的属性,然后又定义了一个List<object>的容器,把不同类的对象存到容器里面,现在如何遍历这个list容器来获取存在容器中的对象的及其属性呢?
    在线跪求解答!!!!!!!!!求指导,先谢谢了。

解决方案 »

  1.   

    List<object> list =objlist
    foreach(object obj in list)
    {
       //obj.Triangle  访问
    }
    或者Linq to object
      

  2.   

    不好意思,我要求解的就是访问的问题,三个不同的类都被转换为object放在List里面了,foreach里面的obj是object类,那我怎么用它访问比如Tiangle类的属性呢?在这里我要得到List中的每个对象的所有属性!
      

  3.   

    你不应该用List<object>来存这些对象,而应该抽象出Triangle,Rectangular,Circle这几个类的基类或接口来,把三个类的坐标,大小等公共属性放到这个基类或接口中,然后用List<基类>或List<接口>来存储
      

  4.   

    但是不行啊,因为在一个事件中用到了system.Object[]类型的数据,也是加到list中。
      

  5.   

    http://bbs.csdn.net/topics/390309464
    看看我弄的这个,弄好了就可以解决你的问题
      

  6.   

    那就只能用反射了。嫌麻烦的话,试试dynamic。
      

  7.   


                Triangle tri = new Triangle { Width = 3, Length = 4 };
                Rectangular rec = new Rectangular { X = 1, Y = 3 };
                Circle cir = new Circle { Radius = 4 };            List<object> list = new List<object>();//这不是List发挥作用的地方,如果非要这样还是使用ArrayList
                list.Add(tri);
                list.Add(rec);
                list.Add(cir);            foreach (object item in list)
                {
                    if (item is Triangle)
                    {
                        int width = ((Triangle)item).Width;
                        int length = ((Triangle)item).Length;
                    }
                    else if (item is Rectangular)
                    {
                        int x = ((Rectangular)item).X;
                        int y = ((Rectangular)item).Y;
                    }
                    else if (item is Circle)
                    {
                        int radius = ((Circle)item).Radius;
                    }
                }
      

  8.   

    我觉得或许可以这样,因为事先有哪几个类你都知道,所以,你可以通过反射来获取List中对象实例的类名,然后与预先定义好的类名进行比对,看看符合哪个,所以,你就知道是哪个类了。
      

  9.   


       //类定义如下
        public class Triangle
        {
            public int Width { get; set; }
            public int Length { get; set; }
        }    public class Rectangular
        {
            public int X { get; set; }
            public int Y { get; set; }
        }    public class Circle
        {
            public int Radius { get; set; }
        }
      

  10.   

    恩,但是list中的对象都是被强制转换为object类型之后存进去的,所以反射回来的类型都是System.Object[],应该怎么做?这样是不是访问不了那些对象中的内容?
      

  11.   

    foreach(object obj in 你的集合)
    {
        if(obj is Triangle)
        {
            Triangle t = obj as Triangle;
            t.xxx....
            t.yyy....
        }
        else if(obj is Rectangular)
        {
            Rectangular r = obj as Rectangular;
            r.xxx....
            r.yyy....
        }
        //....
    }
      

  12.   

    如3楼
    你应该写一个 比如 图形类而 那些画图的 继承于这个类
    list 类型为 基类
    也就是图形类
      

  13.   


    class Program
        {
            static void Main(string[] args)
            {
                User user = new User("aaa");
                List<object> list = new List<object>();
                list.Add(new User("user"));
                list.Add(new Person("female"));
                object o = list[0];
                if (o.GetType()==typeof(User))
                {
                    Console.WriteLine(((User)o).Name);
                }
                else if (o.GetType() == typeof(Person))
                {
                    Console.WriteLine(((Person)o).Gender);
                }
                Console.ReadLine();
            }
        }    class User
        {
            public string Name{ get;set;}        public User(string name)
            {
                this.Name = name;
            }
        }    class Person
        {
            public string Gender {get; set;}        public Person(string gender)
            {
                this.Gender = gender;
            }
        }测试结果是“user”,没问题。List<object>中返回的是详细的类名,而不是object数组
      

  14.   

    只能is as一个个判断了 虽然有点麻烦
      

  15.   

    只要给图形数据类写一个基类,增加一个判断类型的函数。
    从list取出之后先转换到基类,判断类型。
      

  16.   

    我还是把代码贴出来吧:
     private void button8_Click(object sender, EventArgs e)
            {
                 object  obj = propertyGrid1.SelectedObjects;//就是每个图形类的属性窗口的内容,这个好像只能是system.object[]类型,不能转换;这个属性窗口中的属性是自己定义的。
                 mylist.addwidget(obj);//这个就是将每个图形的实例添加到List中。
            }
    所以用GetType返回的list容器中的内容都是object,这样有办法读取那些东西吗?下面是运行的一个窗口截图:
      

  17.   

    刚图没传上,这个才刚接触,刚开始做,所以真心不太懂,请指导。图中的属性框,是图形的属性,可以在那里改变属性来改变图形的样子,然后把它存到list容器中。
      

  18.   


    我觉得是这样的,即使一个User类在强制转换为object后再放入到List中后,当取出后采用反射,还是会反射到User类型的,这个我测试了,我可以确定没问题。我现在怀疑,propertyGrid1.SelectedObjects返回的就是object的实例。 propertyGrid1是个什么控件,SelectedObjects返回的又是什么?
      

  19.   

     propertyGrid1是属性框,就是上图右边的那个控件,selectedObjects就是当前处理的控件的属性。这样说的不太清楚,代码贴上来又太多了,你方便告诉我一个邮箱吗,我发到你的邮箱里,麻烦你给看看。谢谢了。
      

  20.   


    [email protected]  不知道现在这邮箱还好不好用