Files
RookieStation/RookieStation/Utils/CommonUtils.cs
2021-06-24 11:42:33 +08:00

86 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Autodesk.Revit.UI;
using RookieStation.PackAreaModule;
using RookieStation.UserClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace RookieStation.Utils
{
internal static class CommonUtils
{
public static String GetEnumDescription(this Type e, int? value)
{
FieldInfo[] fields = e.GetFields();
for (int i = 1, count = fields.Length; i < count; i++)
{
if ((int)System.Enum.Parse(e, fields[i].Name) == value)
{
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])fields[i].
GetCustomAttributes(typeof(DescriptionAttribute), false);
if (EnumAttributes.Length > 0)
{
return EnumAttributes[0].Description;
}
}
}
return "";
}
public static string GetEnumDescription(Enum enumObj)
{
FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
var descriptionAttr = fieldInfo
.GetCustomAttributes(false)
.OfType<DescriptionAttribute>()
.Cast<DescriptionAttribute>()
.SingleOrDefault();
if (descriptionAttr == null)
{
return enumObj.ToString();
}
else
{
return descriptionAttr.Description;
}
}
/// <summary>
/// 打开窗口解决引用UI库样式丢失,构造函数不具有参数时
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T GenerateWindow<T>() where T : System.Windows.Window, new()
{
string AddInPath = typeof(RsApp).Assembly.Location;
string dirAssembly = System.IO.Path.GetDirectoryName(AddInPath);
AssemblyLoader loader = new AssemblyLoader(dirAssembly);
T win = null;
try
{
loader.HookAssemblyResolve();
win = new T();
System.Windows.Interop.WindowInteropHelper mainUI = new System.Windows.Interop.WindowInteropHelper(win)
{
Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle
};
win.ShowDialog();
}
catch (Exception ex)
{
Log.WriteLog(ex.Message);
}
finally
{
loader.UnhookAssemblyResolve();
}
return win;
}
}
}