首先寫一個類,再寫一個可讀寫的屬性:
代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Member
/// </summary>
namespace Insus.NET
{
public class Member
{
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public Member()
{
//
// TODO: Add constructor logic here
//
}
}
}
Insus.NET一向是寫asp.net程序,練習也是在站點進行。
創(chuàng)建一個網(wǎng)頁,引用兩個namespace:
代碼如下:
using Insus.NET;
using System.Reflection;
讀寫屬性:
代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//實例化類
Member objMember = new Member();
//對屬性set值
PropertyInfo pi = objMember.GetType().GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
if (null != pi && pi.CanWrite)
{
pi.SetValue(objMember, "Insus.NET", null);
}
//對屬性get值
PropertyInfo pii = objMember.GetType().GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
if (null != pii && pi.CanRead)
{
object obj_Name = pii.GetValue(objMember, null);
Response.Write(obj_Name.ToString());
}
}
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com