父类获取子类属性

 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
    {
       
    }




posted on 2010-08-27 15:51  shaya  阅读(786)  评论(0编辑  收藏  举报

导航