本文章转载:http://www.cnblogs.com/wuhuacong/archive/2011/05/08/2040620.html
至此,权限管理模块介绍已经完毕,下面给出一个调用例子Demo程序给大家参考学习,下载地址如下:
http://files.cnblogs.com/wuhuacong/SecurityDemo.rar
前一篇文件的字典组件模块调用例子Demo程序下载地址也一并提供下载,下载地址如下:
http://files.cnblogs.com/wuhuacong/DictionaryDemo.rar
40楼的评论:
不过可以先帖上权限的核心控制部分的代码: /// <summary>
/// 设置form上的组件的权限
/// </summary>
/// <param name="form">控件所在的窗体</param>
/// <param name="getPermissionsHandler">取得form里组件名及其Enabled属性(当前用户)的键值对</param>
public static void SetPermissions(this Form form, Func<Form, IDictionary<string, bool>> getPermissionsHandler)
{
Type t = form.GetType();
FieldInfo[] fs = t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
IDictionary<string, bool> permissions = getPermissionsHandler(form);
foreach (FieldInfo f in fs)
{
dynamic component = f.GetValue(form);
if (component is System.ComponentModel.Component && permissions.ContainsKey(f.Name))
{
try
{
component.Enabled = permissions[f.Name];
}
catch { }
}
}
}