问题描述
大家好
我想在一个类的第一个方法中调用一个特殊的方法来授权。
是否具有自定义属性。例如,有一个名为TestClass的类,其中有两个方法:
公共类TestClass
{
public void TestMethod1()
{          
}
public void TestMethod2()
{          
}
}
并且,还有另一个类用于授权和检查当前用户的方法权限:
公共类UserAuthentication
{        
public static bool CheckPermission(string activityName)
      {           
DataService.UserManagment us = new
DataService.UserManagment();           var
hasPermission = us.HasPermission(Properties.Settings.Default.UserID,activityName);
        if(!hasPermission&&!Properties.Settings.Default.IsAdmin)          
{              
抛出新的AuthenticationException(" Permission Error");          
}
     返回true;
      }
}
所以,我想在TestClass上运行任何方法之前调用CheckPermission并将方法的名称作为activityname传递。
它可能有反射和自定义属性,但我对此没有任何线索。
Hi everybody
i wanna call an special method at the first of any methods of a class in order to authorizing.
whether with a custom attribute or not. for example there is a class named TestClass and two methods in there:
public class TestClass { public void TestMethod1() { } public void TestMethod2() { } }
and, there is another class for authorizing and check method permission for current user:
public class UserAuthentication { public static bool CheckPermission(string activityName) { DataService.UserManagment us = new DataService.UserManagment(); var hasPermission = us.HasPermission(Properties.Settings.Default.UserID, activityName); if (!hasPermission && !Properties.Settings.Default.IsAdmin) { throw new AuthenticationException("Permission Error"); } return true; } }
so, i wanna call CheckPermission before running any methods on TestClass and pass method's name as activityname.
it maybe possible with reflections and custom attribute but i don't have a clue about that.
这篇关于如何在Windows窗体应用程序项目中调用类上的任何方法之前调用特定方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!