202303241701Change
This commit is contained in:
23
XKRS.UI.Model.Winform/Helper/AttributeHelper.cs
Normal file
23
XKRS.UI.Model.Winform/Helper/AttributeHelper.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace XKRS.UI.Model.Winform
|
||||
{
|
||||
/// <summary>
|
||||
/// 菜单节点属性
|
||||
/// </summary>
|
||||
public class MenuNodeAttribute:Attribute
|
||||
{
|
||||
public string MenuCode { get; set; }
|
||||
public string MenuName { get; set; }
|
||||
public int MenuOrder { get; set; }
|
||||
public string ParentMenuCode { get; set; }
|
||||
/// <summary>
|
||||
/// 表示该窗体是实际显示窗体还是只是父节点
|
||||
/// </summary>
|
||||
public bool IsActualForm { get; set; }
|
||||
}
|
||||
}
|
106
XKRS.UI.Model.Winform/Helper/MenuFormFactory.cs
Normal file
106
XKRS.UI.Model.Winform/Helper/MenuFormFactory.cs
Normal file
@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using XKRS.Common.Interface;
|
||||
|
||||
namespace XKRS.UI.Model.Winform.Helper
|
||||
{
|
||||
public static class MenuFormFactory
|
||||
{
|
||||
|
||||
public static Dictionary<MenuNodeAttribute, Type> menuFrmTypeDict = null;
|
||||
public static Dictionary<MenuNodeAttribute,Type> MenuFrmTypeDict
|
||||
{
|
||||
get
|
||||
{
|
||||
if (menuFrmTypeDict == null)
|
||||
{
|
||||
var assm = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory)
|
||||
.GetFiles("*.dll")
|
||||
.Where(fi =>
|
||||
{
|
||||
return fi.Name.StartsWith("XKRS");
|
||||
})
|
||||
.Select(u =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return Assembly.LoadFrom(u.FullName);
|
||||
}
|
||||
catch { }
|
||||
return null;
|
||||
})
|
||||
.Where(a => a != null)
|
||||
.ToList();
|
||||
assm.AddRange(AppDomain.CurrentDomain.GetAssemblies());
|
||||
assm = assm.Distinct().ToList();
|
||||
|
||||
try
|
||||
{
|
||||
menuFrmTypeDict = assm.SelectMany(a => a.GetTypes())
|
||||
.Where(t =>
|
||||
{
|
||||
if (t.GetInterfaces().Contains(typeof(IMenuNode)))
|
||||
{
|
||||
var attr = t.GetCustomAttribute<MenuNodeAttribute>();
|
||||
return attr != null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}).ToDictionary(t => t.GetCustomAttribute<MenuNodeAttribute>(), t => t);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
return menuFrmTypeDict;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 从Menu配置项转为窗口
|
||||
/// </summary>
|
||||
/// <param name="frmCode"></param>
|
||||
/// <returns></returns>
|
||||
public static MenuFormBase GetMenuFrm(string frmCode)
|
||||
{
|
||||
Type frmType = GetMenuFrmType(frmCode);
|
||||
if (frmType == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var menuItem = Activator.CreateInstance(frmType);
|
||||
if (menuItem == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var menu = menuItem as MenuFormBase;
|
||||
if (menu == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
menu.Tag = frmCode;
|
||||
return menu;
|
||||
}
|
||||
|
||||
public static Type GetMenuFrmType(string frmCode)
|
||||
{
|
||||
Type frmType = null;
|
||||
foreach (KeyValuePair<MenuNodeAttribute,Type> pair in MenuFrmTypeDict)
|
||||
{
|
||||
if (pair.Key.MenuCode == frmCode)
|
||||
{
|
||||
frmType = pair.Value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return frmType;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user