diff --git a/src/BootstrapBlazor/Extensions/LambdaExtensions.cs b/src/BootstrapBlazor/Extensions/LambdaExtensions.cs
index 4b96bdd27..c54c622db 100644
--- a/src/BootstrapBlazor/Extensions/LambdaExtensions.cs
+++ b/src/BootstrapBlazor/Extensions/LambdaExtensions.cs
@@ -3,6 +3,7 @@
// Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Components;
+using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using System.Reflection;
@@ -600,4 +601,24 @@ public static class LambdaExtensions
return false;
}
#endregion
+
+ ///
+ /// 获得 指定模型标记 的属性值
+ ///
+ ///
+ ///
+ ///
+ public static Expression> GetKeyValue(TModel model)
+ {
+ var type = model is not null ? model.GetType() : typeof(TModel);
+ Expression> ret = _ => default!;
+ var property = type.GetRuntimeProperties().FirstOrDefault(p => p.IsDefined(typeof(KeyAttribute)));
+ if (property != null)
+ {
+ var param = Expression.Parameter(typeof(TModel));
+ var body = Expression.Property(Expression.Convert(param, type), property);
+ ret = Expression.Lambda>(Expression.Convert(body, typeof(TValue)), param);
+ }
+ return ret;
+ }
}
diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs
index dce57592f..186edade2 100644
--- a/src/BootstrapBlazor/Services/CacheManager.cs
+++ b/src/BootstrapBlazor/Services/CacheManager.cs
@@ -301,6 +301,25 @@ internal class CacheManager : ICacheManager
});
invoker(model, value);
}
+
+ ///
+ /// 获得 指定模型标记 的属性值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static TValue GetKeyValue(TModel model)
+ {
+ var type = model is object o ? o.GetType() : typeof(TModel);
+ var cacheKey = ($"Lambda-GetKeyValue-{type.FullName}", typeof(TModel));
+ var invoker = Instance.GetOrCreate(cacheKey, entry =>
+ {
+ entry.SetDynamicAssemblyPolicy(type);
+ return LambdaExtensions.GetKeyValue(model).Compile();
+ });
+ return invoker(model);
+ }
#endregion
#region Lambda Sort
diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs
index e6586f194..e9944f00c 100644
--- a/src/BootstrapBlazor/Utils/Utility.cs
+++ b/src/BootstrapBlazor/Utils/Utility.cs
@@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
+using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq.Expressions;
using System.Reflection;
@@ -31,6 +32,15 @@ public static class Utility
///
public static string GetDisplayName(Type modelType, string fieldName) => CacheManager.GetDisplayName(modelType, fieldName);
+ ///
+ /// 获得 指定模型标记 的属性值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static TValue GetKeyValue(TModel model) => CacheManager.GetKeyValue(model);
+
///
///
///
diff --git a/test/UnitTest/Utils/UtilityTest.cs b/test/UnitTest/Utils/UtilityTest.cs
index e41a92be5..a3e9cd12e 100644
--- a/test/UnitTest/Utils/UtilityTest.cs
+++ b/test/UnitTest/Utils/UtilityTest.cs
@@ -18,6 +18,18 @@ public class UtilityTest : BootstrapBlazorTestBase
};
}
+ [Fact]
+ public void GetKeyValue_Ok()
+ {
+ var foo = new Foo() { Id = 1 };
+ var v = Utility.GetKeyValue(foo);
+ Assert.Equal(1, v);
+
+ object foo1 = new Foo() { Id = 2 };
+ v = Utility.GetKeyValue