using DeviceService.Core;
|
using System.Reflection;
|
using System.Windows.Controls;
|
|
namespace DeviceService
|
{
|
/// <summary>
|
/// 按命名规则创建设备配置控件:设备类名 CanDevice 对应 CanDeviceConfigControl。
|
/// </summary>
|
public static class DeviceConfigControlFactory
|
{
|
public static UserControl? Create(IDevice device)
|
{
|
if (device == null) throw new ArgumentNullException(nameof(device));
|
|
string controlName = device.GetType().Name + "ConfigControl";
|
|
try
|
{
|
Type? type = typeof(DeviceConfigControlFactory).Assembly.GetTypes()
|
.FirstOrDefault(t => t.Name == controlName
|
&& typeof(UserControl).IsAssignableFrom(t)
|
&& !t.IsAbstract);
|
|
return type == null ? null : (UserControl?)Activator.CreateInstance(type);
|
}
|
catch (ReflectionTypeLoadException ex)
|
{
|
Type? type = ex.Types.FirstOrDefault(t => t?.Name == controlName
|
&& typeof(UserControl).IsAssignableFrom(t)
|
&& !t.IsAbstract);
|
return type == null ? null : (UserControl?)Activator.CreateInstance(type);
|
}
|
}
|
}
|
}
|