您好,登錄后才能下訂單哦!
--------------------------------------------------LimitAttribute.cs 自定義特性
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// LimitAttribute 的摘要說(shuō)明 /// </summary> //特性只作用與方法上 [AttributeUsage(AttributeTargets.Method)] public class LimitAttribute:Attribute { private string _name; public string Name { get { return _name; } } public LimitAttribute(string name) { this._name = name; } }
--------------------------------------------------Default.aspx
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Reflection; public partial class _Default : System.Web.UI.Page { public static readonly string _name = "李四"; protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 判斷該用戶是否有執(zhí)行權(quán)限 /// </summary> /// <param name="name">用戶名稱</param> /// <param name="method">方法名稱</param> /// <returns></returns> private bool IsLimit(string name, string method) { //獲取該類型 Type t = typeof(_Default); //查找該方法 MethodInfo mi = t.GetMethod(method); if (mi == null) return false; //獲取方法上的特性 LimitAttribute la = Attribute.GetCustomAttribute(mi, typeof(LimitAttribute)) as LimitAttribute; if (la == null) return false; //判斷用戶 if (la.Name == name) { return true; } else { return false; } } [Limit("張三")] public void btnView_Click(object sender, EventArgs e) { if (IsLimit(_name, "btnView_Click")) { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('我是查看')", true); } else { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('對(duì)不起,您沒(méi)有權(quán)限操作')", true); } } [Limit("李四")] public void btnEdit_Click(object sender, EventArgs e) { if (IsLimit(_name, "btnEdit_Click")) { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('我是修改')", true); } else { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('對(duì)不起,您沒(méi)有權(quán)限操作')", true); } } [Limit("李四")] public void btnAdd_Click(object sender, EventArgs e) { if (IsLimit(_name, "btnAdd_Click")) { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('我是添加')", true); } else { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('對(duì)不起,您沒(méi)有權(quán)限操作')", true); } } [Limit("王五")] public void btnDel_Click(object sender, EventArgs e) { if (IsLimit(_name, "btnDel_Click")) { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('我是刪除')", true); } else { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('對(duì)不起,您沒(méi)有權(quán)限操作')", true); } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。