首先依赖注入 懒得下载autofac了 直接用程序集进行批量注入- private static WebApplicationBuilder builder;
- internal static void Load(WebApplicationBuilder web)
- {
- builder = web;
- builder.Services.Configure<ApiBehaviorOptions>(options => options.SuppressModelStateInvalidFilter = true);
- Assembly.Load("TestApplication").ExportedTypes.LoadImp();
- builder.Services.AddSingleton(typeof(ITestGren<>), typeof(TestGren<>));
- }
- private static void LoadImp(this IEnumerable<Type> type)
- {
- type= type.Where(d => d.IsClass&&!d.IsAbstract);
- foreach (var item in type)
- { ///检查是不是泛型类
- if (item.GetGenericArguments().Length is 0) item.GetInterfaces().LoadInter(item);
- }
- }
- private static void LoadInter(this Type[] type,Type ImpType)
- {
- foreach (var item in type)
- {
- if (item.GetGenericArguments().Length is 0) builder.Services.AddSingleton(item,ImpType);
- }
- }
复制代码 可惜泛型类不能直接注入 需要自己写,反正自己也不怎么写泛型类
然后我们的接口要验签就创建一个类来继承ValidationAttribute特性- [CheckSgValidation(ErrorMessage ="签名错误")]
- public class TestModel
- {
- public string Sg { get; set; }
- }
- public class CsModel:TestModel
- {
- public int MyProperty { get; set; }
- }
- [AttributeUsage(AttributeTargets.Class )]
- public class CheckSgValidation: ValidationAttribute
- {
- private string SgName = "Sg";
- public CheckSgValidation(string sgName)
- {
- this.SgName = sgName;
- }
- public CheckSgValidation() { }
- public override bool IsValid(object? value)
- {
- StringBuilder sb = new StringBuilder();
- foreach (var item in value.GetType().GetProperties())
- {
- if (item.Name == SgName) continue;
- sb.Append(item.GetValue(value));
- }
- return sb.ToString() == value.GetType().GetProperty(SgName).GetValue(value).ToString();
- }
- }
复制代码 我是用单独一个类来写上我们自定义的特性, 尝试过用表达式树进行验证的 可惜参数为object就不行 不知道怎么处理这个问题
这样用反射确实可以用

验证redis登录验证码或其他传入key,value的特性- internal interface IRedis
- {
- bool Get(string key);
- bool GetDelete(string key);
- }
- internal class Redis : IRedis
- {
- internal Redis(string conn) { }
- public bool Get(string key){ return true; }
- public bool GetDelete(string key){ return false; }
- }
- internal static class StaticSingle
- {
- public static readonly IRedis _redis;
- static StaticSingle()
- {
- ///本质上单列式一种多态 我们这个静态构造函数式启动时只运行一次 可以这样实现单例
- _redis = new Redis("127");
- }
- }
- [AttributeUsage(AttributeTargets.Class|AttributeTargets.Property)]
- public class CheckRedisValidation : ValidationAttribute
- {
- public string key = string.Empty;
- public string value = string.Empty;
- public bool IsDel=true;
- public sealed override bool IsValid(object? value)
- {
- ///为空说明是放入在属性上
- if(this.key is null)
- {
- var array= value.ToString().Split(',');
- if (array.Length is 1) return false;
- ///value: "key,value"
- ///这里自己就不写读取比较了
- return IsDel ? StaticSingle._redis.GetDelete(array[0]) : StaticSingle._redis.Get(key);
- }
- else
- {
- key = value.GetType().GetProperty(key).GetValue(value).ToString();
- this.value = value.GetType().GetProperty(this.value).GetValue(value).ToString().ToLower();
- return IsDel ? StaticSingle._redis.GetDelete(key) : StaticSingle._redis.Get(key);
- }
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |