C#中的自定义条件方法属性

时间:2010-10-15 10:45:26

标签: methods c#-4.0 custom-attributes

我想知道我是否可以在c#中执行类似的操作:

    public CustomerManagerScreen() 
    {
        [TestAttirubute("CustomerManagerScreen_Load")]
            private void CustomerManagerScreen_Load(object sender, EventArgs e)
            {
                CustomerLoad();
            }
    }

如您所见,方法名称是TestAttribute的参数,我想要实现的是CustomerManagerScreen_Load将被丢弃,具体取决于TestAttirubute的结果

这是属性类......

public class TestAttirubute: System.Attribute
    {
        private string _MethodName = string.Empty;

        public TestAttirubute(string MethodName)
        {
            this._MethodName = MethodName;
        }

        public bool hasPermission()
        {
            return (SessionManager.CurrentUser.UserRole.Role.Rights.Where(a => a.Resource.Code == this._MethodName).Count() != 0) ? true: false;
        }
    }

1 个答案:

答案 0 :(得分:0)

不是那样,但你可以使用ConditionalAttribute

然而,一个限制是您的方法必须返回void

相关问题