public class 父类
{
    public void Dosomething()
    {
        //在这里我希望得到子类新增的属性及对应的值
    }
}
public class 子类1 : 父类
{
    public 属性1;
    public 属性2;
}
public class 子类2 : 父类
{
    public 属性3;
    public 属性4;
}
可能会有多个子类
new 子类1().Dosomething()里面的对应的属性是子类1的或者不用这种继承的结构,只要能实现相同的功能就行

解决方案 »

  1.   

      protected void Page_Load(object sender, EventArgs e)
            {            B obj = new B();
                obj.str1 = "a";
                obj.str2 = "b";
                Response.Write(obj.DoSomethine());        } class A
        {
            public string DoSomethine()
            {
                string str = "";
                FieldInfo[] fs=  this.GetType().GetFields();
                foreach (FieldInfo f in fs)
                {
                    if (f.FieldType == typeof(string))
                    {
                        str += f.GetValue(this).ToString();
                    }
                }
                return str;
            }
        }
        class B:A
        {
            public string str1;
            public string str2;
        }
      

  2.   

    多谢 cpp2017有没有办法在B类的属性前面加点什么东西,给属性加个属性
      

  3.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Diagnostics;
    using System.Drawing;
    using System.Threading;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text.RegularExpressions;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using System.Net;
    using System.Reflection;
    namespace Csdn
    {    public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {            B obj = new B();
                obj.str1 = "a";
                obj.str2 = "b";
                Response.Write(obj.DoSomethine());        }    }
        class A
        {
            public string DoSomethine()
            {
                string str = "";
                FieldInfo[] fs=  this.GetType().GetFields();
                foreach (FieldInfo f in fs)
                {
                    if (f.GetCustomAttributes(typeof(MyAtt),true).Length > 0)
                    {
                        if (f.FieldType == typeof(string))
                        {
                            str += f.GetValue(this).ToString();
                        }
                    }
                }
                return str;
            }
        }
        class B:A
        {
            [MyAtt]
            public string str1;
          
            public string str2;
        }
        class MyAtt : Attribute
        { 
            
        }
    }