Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
2023-11-23 14:41:15 +08:00
8 changed files with 217 additions and 37 deletions

View File

@@ -24,6 +24,15 @@ namespace JNPF.Common.Extension
setAction(instance, value);
}
public static object GetPropertyValue<T>(this T obj, string propertyName)
{
if (!ProperyGet<T>.ValueFactories.TryGetValue(propertyName, out var getDateValue))
{
getDateValue = ProperyGet<T>.PropertyGetValue(propertyName);
ProperyGet<T>.ValueFactories.Add(propertyName, getDateValue);
}
return getDateValue(obj);
}
/// <summary>
/// Lambda表达式拼接
/// </summary>
@@ -144,4 +153,17 @@ namespace JNPF.Common.Extension
}
}
public class ProperyGet<T> //where T : class, new()
{
public static Dictionary<string, Func<T, object>> ValueFactories = new Dictionary<string, Func<T, object>>(StringComparer.OrdinalIgnoreCase);
public static Func<T, Object> PropertyGetValue(string name)
{
var parameterExp = Expression.Parameter(typeof(T), "entity");
var propertyExp = Expression.Property(parameterExp, name);
var castExp = Expression.Convert(propertyExp, typeof(object));
var lambda = Expression.Lambda<Func<T, object>>(castExp, parameterExp);
return lambda.Compile();
}
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tnb.Common.Utils
{
public static class ObjectPropertyChecker
{
public static bool HasProperty<T>(T obj, string propertyName)
{
Type type = typeof(T);
var propertyInfo = type.GetProperty(propertyName);
return propertyInfo != null;
}
}
}