using Bro.UI.Config.MenuForms;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Reflection;
|
|
namespace Bro.UI.Model.Helper
|
{
|
public static class MenuFormFactory
|
{
|
private static Dictionary<MenuNodeAttribute, Type> menuFrmTypeDict = null;
|
public static Dictionary<MenuNodeAttribute, Type> MenuFrmTypeDict
|
{
|
get
|
{
|
if (menuFrmTypeDict == null)
|
{
|
menuFrmTypeDict = AppDomain.CurrentDomain.GetAssemblies().ToList()
|
.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);
|
}
|
|
return menuFrmTypeDict;
|
}
|
}
|
|
public static MenuFrmBase GetMenuFrm(string frmCode)
|
{
|
Type frmType = GetMenuFrmType(frmCode);
|
|
if (frmType == null)
|
{
|
return null;
|
}
|
|
var menu = Activator.CreateInstance(frmType) as MenuFrmBase;
|
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;
|
}
|
}
|
}
|