权限管理、用户权限系统、开源用户权限系统、信息化建设标准基础数据管理平台
代码改变世界

反射技术在通用权限管理系统组件源码中的实际应用参考【附源码】

2010-12-19 15:06  通用C#系统架构  阅读(5595)  评论(34编辑  收藏  举报

这个是我们系统中的菜单与对应的窗体之间的关联关系,一般一个菜单明确指向其中的某个窗体,由于窗体很多,都把窗体分别放在不同的包不同的命名空间不同的DLL里,这样将来好管理,好分类,好定位。

 

其中窗体的名字,除了前4个字符外,对应了相应窗体的名字。

 

菜单的Tag属性对应了动态连接库的名字,DLL的名称

 

 

所有的菜单,都共用了一个Click事件,在这个事件里统一处理用户的按键事件。

 

在这个Click事件里,可以获得用户需要调用哪个DLL里的哪个窗体,下面是获取的具体代码参考。

 

这里是循环判断,当前窗口是否已经被打开了。

 

这里是动态创建窗体的方法,其中用了缓存技术+反射技术,反射主要体现在这个代码片段里。

 

附源码如下,供参考

 

代码
//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace DotNet.WinForm
{
    
using DotNet.Service;
    
using DotNet.WinForm.Utilities;
    
using DotNet.Utilities;
    
using DotNet.WinForm.Message;
    
using DotNet.WinForm.ResourcePermission;
    
using DotNet.WinForm.WorkFlow;

    
/// <summary>
    
/// MainForm
    
/// 
    
/// 修改纪录
    
/// 
    
///        2009.11.11 版本:2.0 JiRiGaLa 美化图标。
    
///        2007.10.29 版本:1.0 JiRiGaLa 创建。
    
///        
    
/// 版本:1.0
    
///
    
/// <author>
    
///        <name>JiRiGaLa</name>
    
///        <date>2007.10.29</date>
    
/// </author> 
    
/// </summary>
    public partial class SDIMainForm : BaseForm, IBaseMainForm // Form
    {
        
public SDIMainForm()
        {
            InitializeComponent();
            
if (!this.DesignMode)
            {
                
// 获得图标
                this.GetIcon();
            }
        }

        
new public void GetIcon()
        {
            
if (System.IO.File.Exists(BaseSystemInfo.AppIco))
            {
                
this.notifyIcon.Icon = new Icon(BaseSystemInfo.AppIco);
            }
        }

        
public void InitService()
        {
            Program.InitService(UserInfo);
            
// Thread Thread = new Thread(new ThreadStart(Program.InitService));
            
// Thread.Start();
        }

        
#region private void LoadChildrenForm(string assemblyName, string formName)
        
/// <summary>
        
/// 加载选定的子窗口
        
/// </summary>
        
/// <param name="assemblyName">程序集</param>
        
/// <param name="selectedNodeName">当前选择的窗体名称</param>
        private void LoadChildrenForm(string assemblyName, string formName)
        {
            
if (formName.Length == 0)
            {
                
return;
            }
            
// 是否已经打开了?若已经打开了这个窗体,那就不用重复打开了
            foreach (Form childrenForm in this.OwnedForms)
            {
                
if (childrenForm.Name == formName)
                {
                    childrenForm.Visible 
= true;
                    childrenForm.Activate();
                    
return;
                }
            }
            
// 一些特殊的页面处理,重新登录时,需要把其他页面都自动关闭了。
            switch (formName)
            {
                
case "FrmLogOn":
                    
// 关闭所有打开的窗口
                    foreach (Form childrenForm in this.MdiChildren)
                    {
                        childrenForm.Close();
                    }
                    
this.ShowChildrenForm(assemblyName, formName);
                    
break;
                
default:
                    
this.ShowChildrenForm(assemblyName, formName);
                    
return;
            }
        }
        
#endregion

        
/// <summary>
        
/// 反射显示窗体
        
/// </summary>
        
/// <param name="assemblyName">命名空间</param>
        
/// <param name="formName">窗体名称</param>
        
/// <returns>对象</returns>
        private object CreateInstance(string assemblyName, string formName)
        {
            
// 这里用了缓存技术,若已经被创建过就不反复创建了
            Assembly assembly = CacheManager.Instance.Load(assemblyName);
            Type type 
= assembly.GetType(assemblyName + "." + formName, truefalse);
            
return Activator.CreateInstance(type);
        }

        
private void ShowChildrenForm(string assemblyName, string formName)
        {
            Form childrenForm 
= (Form)this.CreateInstance(assemblyName, formName);
            
if (childrenForm != null)
            {
                childrenForm.Name 
= formName;
                childrenForm.Owner 
= this;
                childrenForm.ShowInTaskbar 
= false;
                childrenForm.Show();
            }
        }

        
private void mitm_Click(object sender, EventArgs e)
        {
            
// 这里是获取表单的名称
            string formName = ((ToolStripMenuItem)sender).Name.Substring(4);
            
// 这里是获取命名空间
            string assemblyName = BaseSystemInfo.MainAssembly;
            
if (((ToolStripMenuItem)sender).Tag != null)
            {
                assemblyName 
= ((ToolStripMenuItem)sender).Tag.ToString();
            }
            
// 这里是加载窗体,不会重复加载窗体
            this.LoadChildrenForm(assemblyName, formName);
        }

        
private void mitmFrmLogOn_Click(object sender, EventArgs e)
        {
            
// 按配置的登录页面进行登录
            this.LoadChildrenForm(BaseSystemInfo.LogOnAssembly, BaseSystemInfo.LogOnForm);
        }

        
private DateTime LastChekDate = DateTime.MinValue;

        
public void CallMessager()
        {
            
this.CallMessager(true);
        }

        
/// <summary>
        
///  即时通讯
        
/// </summary>
        
/// <param name="show">显示</param>
        public void CallMessager(bool show)
        {
            
if (Program.frmMessage == null)
            {
                Program.frmMessage 
= new FrmMessage();
            }
            
if (show)
            {
                Program.frmMessage.Visible 
= true;
                
// Program.FrmMessage.ShowInTaskbar = true;
                Program.frmMessage.Show();
                Program.frmMessage.WindowState 
= FormWindowState.Normal;
                Program.frmMessage.Activate();
            }
            
else
            {
                
// 加载窗体
                
// Program.FrmMessage.WindowState = FormWindowState.Minimized;
                Program.frmMessage.Show();
                Program.frmMessage.Hide();
            }
        }

        
private void btnMessage_Click(object sender, EventArgs e)
        {
            
this.CallMessager();
        }

        
private void mitmFrmMessage_Click(object sender, EventArgs e)
        {
            
this.CallMessager();
        }

        
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
// FrmWorkFlowTest frmWorkFlowTest = new FrmWorkFlowTest();
            
// frmWorkFlowTest.Show();
            
            
// FrmWorkFlowStatr frmWorkFlowStatr = new FrmWorkFlowStatr();
            
// frmWorkFlowStatr.Show();

            
// 用户权限(项目权限)
            
// FrmProjectUserPermissionAdmin frmUserPermissionAdmin = new FrmProjectUserPermissionAdmin();
            
// frmUserPermissionAdmin.Show();

            
// 角色权限(项目权限)
            
// FrmProjectRolePermissionAdmin frmRolePermissionAdmin = new FrmProjectRolePermissionAdmin();
            
// frmRolePermissionAdmin.Show();

            
// 设置权限参考
            
// FrmResourceSetPermission frmResourceSetPermission = new FrmResourceSetPermission();
            
// frmResourceSetPermission.Show();

            
// 树型资源权限
            
// FrmTreeResourcePermission frmTreeResourcePermission = new FrmTreeResourcePermission();
            
// frmTreeResourcePermission.Show();

            
// 用户权限(字段权限)
            
// FrmTableColumnsUserPermissionAdmin frmUserPermissionAdmin = new FrmTableColumnsUserPermissionAdmin();
            
// frmUserPermissionAdmin.Show();

            
// 角色权限(字段权限)
            
// FrmTableColumnsRolePermissionAdmin frmRolePermissionAdmin = new FrmTableColumnsRolePermissionAdmin();
            
// frmRolePermissionAdmin.Show();

            
this.CallMessager();
        }

        
#region private void CheckMenu() 获得的菜单
        
/// <summary>
        
/// 获得的菜单
        
/// </summary>
        public void CheckMenu()
        {
            
this.contextMenuStrip.Tag = null;
            
// 申请账户
            this.CheckMenuItem(this.mitmFrmRequestAnAccount);
            
// 账户审核
            this.CheckMenuItem(this.mitmFrmUserAudit);
            
// 用户管理
            this.CheckMenuItem(this.mitmFrmUserAdmin);
            
this.mtspUserAdmin.Visible = this.mitmFrmRequestAnAccount.Visible || this.mitmFrmUserAudit.Visible || this.mitmFrmUserAdmin.Visible;

            
// 扮演用户
            this.CheckMenuItem(this.mitmFrmImpersonation);
            
// 用户授权
            this.CheckMenuItem(this.mitmFrmAccredit);
            
this.mtspImpersonation.Visible = this.mitmFrmAccredit.Visible || this.mitmFrmImpersonation.Visible;

            
// 组织机构管理
            this.CheckMenuItem(this.mitmFrmOrganizeAdmin);
            
// 职员管理
            this.CheckMenuItem(this.mitmFrmStaffAdmin);
            
// 角色管理
            this.CheckMenuItem(this.mitmFrmRoleAdmin);

            
this.mtspStaff.Visible = this.mitmFrmOrganizeAdmin.Visible
                
|| this.mitmFrmStaffAdmin.Visible
                
|| this.mitmFrmRoleAdmin.Visible;

            
// 权限部分
            this.CheckMenuItem(this.mitmFrmPermissionItemAdmin);
            
this.CheckMenuItem(this.mitmFrmModuleAdmin);
            
this.CheckMenuItem(this.mitmFrmUserPermissionAdmin);
            
this.mtspPermission.Visible = this.CheckMenuItem(this.mitmFrmPermissionItemAdmin)
                
|| this.CheckMenuItem(this.mitmFrmModuleAdmin)
                
|| this.CheckMenuItem(this.mitmFrmUserPermissionAdmin);

            
// 我的联系方式
            this.CheckMenuItem(this.mitmFrmStaffAddressEdit);
            
// 内部通讯录
            this.CheckMenuItem(this.mitmFrmStaffAddressAdmin);
            
// 没有员工帐号,这个就不显示了
            this.mitmFrmStaffAddressEdit.Visible = !String.IsNullOrEmpty(UserInfo.StaffId);
            
            
this.mtspStaffAddress.Visible = this.mitmFrmStaffAddressAdmin.Visible
                
|| this.mitmFrmStaffAddressEdit.Visible;

            
// 发送消息
            this.CheckMenuItem(this.mitmFrmMessageSend);
            
// 即时通讯
            this.CheckMenuItem(this.mitmFrmMessage);
            
this.mtspMessage.Visible = this.mitmFrmMessageSend.Visible || this.mitmFrmMessage.Visible;
            
            
// 业务应用
            this.CheckMenuItem(this.mitmAppliction);
            
if (this.mitmAppliction.Visible)
            {
                
// 工作日志管理
                this.CheckMenuItem(this.mitmFrmWorkReportAdd);
                
this.CheckMenuItem(this.mitmFrmWorkReportList);
                
this.CheckMenuItem(this.mitmFrmWorkReportManger);
                
this.mtspWorkReport.Visible = this.CheckMenuItem(this.mitmFrmWorkReportAdd) || this.CheckMenuItem(this.mitmFrmWorkReportList) || this.CheckMenuItem(this.mitmFrmWorkReportManger);

                
// 文档管理
                this.CheckMenuItem(this.mitmFrmFileAdmin);
                
// this.mtspFileAdmin.Visible = this.CheckMenuItem(this.mitmFrmFileAdmin);
                this.mtspFileAdmin.Visible = this.CheckMenuItem(this.mitmFrmFileAdmin) && (this.CheckMenuItem(this.mitmFrmBusinessCardAdmin));
                
                
// 名片管理
                this.CheckMenuItem(this.mitmFrmBusinessCardAdmin);

                
this.mtspStaffAddress.Visible = this.mtspWorkReport.Visible || this.mtspFileAdmin.Visible || this.CheckMenuItem(this.mitmFrmBusinessCardAdmin);
            }

            
// 工作流管理
            this.CheckMenuItem(this.mitmFrmWorkFlowWaitForAudit);
            
this.CheckMenuItem(this.mitmFrmWorkFlowMonitor);
            
this.CheckMenuItem(this.mitmFrmWorkFlowProcessAdmin);
            
this.mitmWorkFlow.Visible = this.CheckMenuItem(this.mitmFrmWorkFlowWaitForAudit)
                
|| this.CheckMenuItem(this.mitmFrmWorkFlowMonitor)
                
|| this.CheckMenuItem(this.mitmFrmWorkFlowProcessAdmin);

            
// 基础编码管理
            this.CheckMenuItem(this.mitmFrmItemsAdmin);
            
// 这个默认情况下,不会显示,显示状态也不能赋值,要注意
            this.CheckMenuItem(this.mitmFrmSequenceAdmin);
            
// 代码生成器
            this.CheckMenuItem(this.mitmFrmCodeGenerator);
            
this.mtspAppliction.Visible = this.mitmAppliction.Visible
                
|| this.mitmFrmCodeGenerator.Visible
                
|| this.CheckMenuItem(this.mitmFrmItemsAdmin)
                
|| this.CheckMenuItem(this.mitmFrmSequenceAdmin);

            
// 系统日志部分
            this.CheckMenuItem(this.mitmLogAdmin);
            
if (this.mitmLogAdmin.Visible)
            {
                
// 这个默认情况下,不会显示,显示状态也不能赋值,要注意
                this.CheckMenuItem(this.mitmFrmLogGeneral);
                
this.CheckMenuItem(this.mitmFrmLogByDate);
                
this.CheckMenuItem(this.mitmFrmLogByModule);
                
this.CheckMenuItem(this.mitmFrmLogByUser);
                
this.CheckMenuItem(this.mitmFrmExceptionAdmin);
                
this.mitmLogAdmin.Visible = this.CheckMenuItem(this.mitmFrmLogGeneral) || this.CheckMenuItem(this.mitmFrmLogByDate) || this.CheckMenuItem(this.mitmFrmLogByModule) || this.CheckMenuItem(this.mitmFrmLogByUser);
                
this.mitmLogAdmin.Enabled = this.mitmLogAdmin.Visible;
            }            
            
            
this.mtspLog.Visible = this.mitmLogAdmin.Visible;
                        
            
// 我的权限
            this.CheckMenuItem(this.mitmFrmUserPermission);            
            
// 修改密码
            this.CheckMenuItem(this.mitmFrmUserChangePassword);            

            
// 在线支持
            this.CheckMenuItem(this.mitmFrmOnlineEmailSupport);
            
// 关于本软件
            this.CheckMenuItem(this.mitmFrmAboutThis);
            
this.mtspSupport.Visible = this.mitmFrmOnlineEmailSupport.Visible || this.mitmFrmAboutThis.Visible;
        }
        
#endregion

        
#region private bool CheckMenuItem(ToolStripMenuItem toolStripMenuItem)
        
/// <summary>
        
/// 检查菜单的权限及有效性
        
/// </summary>
        
/// <param name="toolStripMenuItem">菜单项</param>
        private bool CheckMenuItem(ToolStripMenuItem toolStripMenuItem)
        {
            
bool returnValue = true;
            
string moduleCode = toolStripMenuItem.Name.Substring(4);
            returnValue 
= this.ModuleIsVisible(moduleCode);
            toolStripMenuItem.Visible 
= returnValue;
            
// 已经是可见的,再判断是否有效的?若这么写,就错了
            
// if (toolStripMenuItem.Visible)
            
//{
            if (UserInfo.IsAdministrator)
            {
                toolStripMenuItem.Enabled 
= true;
            }
            
else
            {
                toolStripMenuItem.Enabled 
= this.ModuleIsAuthorized(moduleCode);
                
// 没有权限的,进行隐藏处理
                
// toolStripMenuItem.Visible = toolStripMenuItem.Enabled;
            }
            
// }
            return returnValue;
        }
        
#endregion

        
// 定义与方法同签名的委托
        private delegate void CallFormMessager(bool show);

        
#region public void InitForm() 显示状态信息
        
/// <summary>
        
/// 初始化窗体
        
/// </summary>
        public void InitForm()
        {
            
// 是总共有几个模块
            ClientCache.Instance.DTMoule = ServiceManager.Instance.ModuleService.GetDT(UserInfo);
            
//if (!UserInfo.IsAdministrator)
            
//{
            
            
// 当前的用户可以访问的模块的
            ClientCache.Instance.DTUserMoule = ServiceManager.Instance.PermissionService.GetModuleDTByUser(UserInfo, UserInfo.Id);
            
//}
            
            
// 获得当前操作员的权限
            ClientCache.Instance.DTPermission = null;
            ClientCache.Instance.GetPermission(UserInfo);

            
// 获得页面的权限
            
// this.GetPermission();

            
// CallFormMessager CallFormMessager = new CallFormMessager(CallMessager);
            
// CallFormMessager.BeginInvoke(false, null, null);
            
// 这里是呼叫即时通讯部分
            this.CallMessager(false);
        }
        
#endregion

        
private void MainForm_Load(object sender, EventArgs e)
        {
            
// this.ucAttachment.DataBind(UserInfo, "10000000", false);
            
// this.InitForm();
        }

        
private void contextMenuStrip_Opened(object sender, EventArgs e)
        {
            
if (this.contextMenuStrip.Tag == null)
            {
                
this.CheckMenu();
                
// this.contextMenuStrip.Tag = "Loaded";
            }
        }

        
private void mItemExit_Click(object sender, EventArgs e)
        {
            
this.ExitApplication = true;
            
this.Close();
        }

        
protected override void WndProc(ref System.Windows.Forms.Message message)
        {
       
            
switch (message.Msg)
            {
                   
                
case 17:
                    
// 退出应用程序
                    
                    
this.ExitApplication = true;
                    ServiceManager.Instance.LogOnService.OnExit(UserInfo);
                    
this.Dispose();
                    
break;
                
default:
                    
base.WndProc(ref message);
                    
break;
            }
        }

        
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            
if (!this.ExitApplication)
            {
                e.Cancel 
= true;
                
this.Hide();
            }
            
else
            {
                
if (MessageBox.Show(AppMessage.MSG0204, AppMessage.MSG0000, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    e.Cancel 
= true;
                    
return;
                }
                
if (Program.frmMessage != null)
                {
                    Program.frmMessage.ExitApplication 
= true;
                    Program.frmMessage.Dispose();
                }
                
// 退出应用程序
                ServiceManager.Instance.LogOnService.OnExit(UserInfo);
            }
        }
    }
}

 

 



C# ASP.NET 通用权限设计、通用权限管理、通用权限组件、单点登录、集中式权限管理、统一授权体系、分级管理分级授权


微信扫一扫加好友