现在的位置: 首页 > 综合 > 正文

由父对象实例生成子对象实例

2013年10月20日 ⁄ 综合 ⁄ 共 897字 ⁄ 字号 评论关闭

 

            p p1 = new p();
            p1.ID = 12;
            p1.Name = "wo shi p";

            s s1 = new s();
            s1.BaseModel = p1;
            s1.Age = 15;

    public class p
    {
        private int id;
        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

    }

    public class s : p
    {
        public p BaseModel
        {
            get { return (p)this;}
            set
            {
                PropertyInfo[] propertys =value.GetType().GetProperties();
                for (int i = 0; i < propertys.Length; i++)
                {
                    this.GetType().GetProperty(propertys[i].Name).SetValue(this,propertys[i].GetValue(value, null),null);
                }
            }
        }

        private int age;
        public int Age
        {
            get { return age; }
            set { age = value; }
        }

    }

抱歉!评论已关闭.