完成参数映射及参数组的构建

This commit is contained in:
zengwenjie
2025-09-19 17:41:23 +08:00
parent e0bb807214
commit 432e2dad2e
3 changed files with 84 additions and 5 deletions

View File

@@ -1,20 +1,64 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Deedy.Activity
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class ParamDefineAttribute : Attribute, IInitialSortable
public class ParamDefineAttribute : Attribute, IInitialSortable, INotifyPropertyChanged
{
public int InitialSort { get; set; } = 0;
public ParamDefineAttribute() { }
public event PropertyChangedEventHandler? PropertyChanged;
#region
//public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName, AllowNull] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual T GetField<T>(ref T field, [CallerMemberName, AllowNull] string propertyName = null)
{
return field;
}
protected virtual bool SetField<T>(ref T field, T value, [CallerMemberName, AllowNull] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
#endregion
public int InitialSort { get; set; } = 0;
public ParamDefineAttribute(string title, string remark, string @default) { }
public string Name { get; set; } = "";
public string Title { get; set; } = "参数名称";
public string Remark { get; set; } = "参数描述";
public string Identify { get; set; } = "";
public string Default { get; set; } = "";
public string DesignValue { get; set; } = "";
public string GroupName { get; set; } = "";
protected internal string _DesignValue = "";
/// <summary>
/// 设计期值
/// </summary>
public string DesignValue
{
get { return GetField<string>(ref _DesignValue); }
set
{
if (SetField<string>(ref _DesignValue, value))
{
// 这里可以加入属性变更后的处理逻辑
}
}
}
public string GroupName { get; set; } = ParamGroupAttribute.DefaultParams;
public object? Target { get; protected internal set; }
public PropertyInfo? Property { get; protected internal set; }
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -16,5 +17,23 @@ namespace Deedy.Activity
{
private readonly SortedList<int, ParamDefineAttribute> _SoredList = new();
internal ParamDefineCollection() { }
public bool TryGetParamDefine(string name, [NotNull] out ParamDefineAttribute value)
{
ParamDefineAttribute pda = new();
bool result = false;
foreach (var item in this)
{
if (item.Name == name)
{
pda = item;
result = true;
break;
}
}
value = pda;
return result;
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
@@ -98,9 +99,24 @@ namespace Deedy.Activity
/// <param name="output">消息输出器</param>
public static void Help_BuildParamMapping(this IElement element, Output? output = null)
{
//TODO构建元素的参数映射表
if (element == null) return;
if (element.ParamsMapping == null) element.ParamsMapping = new();
var props = element.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty);
foreach (var prop in props)
{
if (prop.CanWrite && prop.CanRead)
{
var pda = prop.GetCustomAttribute<ParamDefineAttribute>();
if (pda != null)
{
pda.Name = prop.Name;
if (element.ParamsMapping.TryGetParamDefine(prop.Name, out var value)) pda = value;
else element.ParamsMapping.Add(pda);
value.Target = element;
value.Property = prop;
}
}
}
}
/// <summary>
/// 构建一个元素的参数分组,为设计器准备的辅助方法