You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

72 lines
1.9 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
namespace ButcherFactory.BO.Utils
{
public static class FormUtil
{
public static Form CreateFrom()
{
#if DEBUG
var dll = @"C:\BwpB3Project\src\ButcherFactorySolution\ButcherFactory.Form\bin\Debug\ButcherFactory.Form.dll";
#endif
#if !DEBUG
var dll = Path.Combine(Directory.GetCurrentDirectory(), "ButcherFactory.Form.dll");
#endif
if (!File.Exists(dll))
throw new Exception("缺少必要的程序集文件 ButcherFactory.Form.dll");
var formType = typeof(IWithRoleForm);
Form form = null;
foreach (var type in Assembly.LoadFile(dll).GetTypes())
{
if (formType.IsAssignableFrom(type))
{
var instance = (IWithRoleForm)Activator.CreateInstance(type);
foreach (var item in instance.RoleName)
{
if (AppContext.User.Login)
{
if (LoginUtil.UserIsInRole(item))
{
if (!AppContext.User.RoleList.Contains(item))
AppContext.User.RoleList.Add(item);
form = instance.Generate();
break;
}
else if (AppContext.User.RoleList.Contains(item))
{
AppContext.User.RoleList.Remove(item);
}
}
else
{
if (AppContext.User.RoleList.Contains(item))
form = instance.Generate();
break;
}
}
}
if (form != null)
break;
}
if (AppContext.User.Login)
LoginUtil.UpdateUserRole();
return form;
}
}
public interface IWithRoleForm
{
List<string> RoleName { get; }
Form Generate();
}
}