根据生产bom拆分子工单代码调整

This commit is contained in:
DEVICE8\12494
2023-05-24 23:31:13 +08:00
parent 65072abc04
commit d12e05d737
16 changed files with 336 additions and 98 deletions

View File

@@ -1,4 +1,5 @@
using JNPF.DependencyInjection;
using System.Reflection;
using JNPF.DependencyInjection;
namespace JNPF.Common.Extension;
@@ -8,6 +9,7 @@ namespace JNPF.Common.Extension;
[SuppressSniffer]
public static class DictionaryExtensions
{
/// <summary>
/// 从字典中获取值,不存在则返回字典<typeparamref name="TValue"/>类型的默认值.
/// </summary>
@@ -55,4 +57,31 @@ public static class DictionaryExtensions
}
}
}
private static Dictionary<string, string[]> dicProperties = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// 字典转换成指定类型实例
/// added by ly on 20230524
/// </summary>
/// <typeparam name="T">转换的目标类型</typeparam>
/// <param name="dictionary">被转换的字典</param>
/// <returns>转换后的目标类型对象实例</returns>
public static T ToObject<T>(this Dictionary<String, Object> dictionary) where T : class, new()
{
var name = typeof(T).Name;
T instance = new();
if (!dicProperties.TryGetValue(name, out string[] properies))
{
properies = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Select(p => p.Name).ToArray();
dicProperties[name] = properies;
}
foreach (var pn in properies)
{
if (dictionary.ContainsKey(pn))
{
instance.PropertySetValue(pn, dictionary[pn]);
}
}
return instance;
}
}

View File

@@ -18,11 +18,22 @@ namespace JNPF.Common.Extension
}
setAction(instance, value);
}
public static void PropertySetValue<T>(this T instance, string propertyName, object value)
{
if (!PropertySet<T>.ValueFactories2.TryGetValue(propertyName, out Action<T, object> setAction))
{
setAction = PropertySet<T>.CreateSetPropertyValueAction2(propertyName);
PropertySet<T>.ValueFactories2.Add(propertyName, setAction);
}
setAction(instance, value);
}
}
public class PropertySet<T>
{
public static Dictionary<string, Action<object, object>> ValueFactories = new Dictionary<string, Action<object, object>>(StringComparer.OrdinalIgnoreCase);
public static Dictionary<string, Action<T, object>> ValueFactories2 = new Dictionary<string, Action<T, object>>(StringComparer.OrdinalIgnoreCase);
public static Action<object, object> CreateSetPropertyValueAction(string propertyName)
{
@@ -35,5 +46,17 @@ namespace JNPF.Common.Extension
return Expression.Lambda<Action<object, object>>(setPropertyValue, target, propertyValue).Compile();
}
public static Action<T, object> CreateSetPropertyValueAction2(string propertyName)
{
var property = typeof(T).GetProperty(propertyName);
var target = Expression.Parameter(typeof(T));
var propExp = Expression.Property(target, propertyName);
var propertyValue = Expression.Parameter(typeof(object));
var castPropertyValue = Expression.Convert(propertyValue, property!.PropertyType);
var assignExp = Expression.Assign(propExp, castPropertyValue);
return Expression.Lambda<Action<T, object>>(assignExp, new[] { target,propertyValue }).Compile();
}
}
}