マスター・ページの変数と属性値の取得


マスター:
 
C#コード:
public partial class MasterPage : System.Web.UI.MasterPage
{

   
public int UserId {
       
get { return 342; }
   
}

   
public string UserName = "shenjk";
   
protected void Page_Load(object sender, EventArgs e)
   
{

   
}
}

 
ページで次の値を取得します.
C#コード:
    protected void Page_Load(object sender, EventArgs e)
   
{
       
PropertyInfo pl = this.Master.GetType().GetProperty("UserId");
       
object o = pl.GetValue(this.Master, null); //o=342
       

       
FieldInfo f = this.Master.GetType().GetField("UserName");
       
object o1 = f.GetValue(this.Master);  //o1=shenjk
       
   
}