- 创建MyTypeFilterAttribute过滤器
public class MyTypeFilterAttribute : Attribute, IFilterFactory, IFilterMetadata { private readonly Type _type; public MyServiceFilterAttribute(Type type) { this._type = type; } public bool IsReusable { get; set; } public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) { //通过反射实例化该过滤器的类 var objFactory = ActivatorUtilities.CreateFactory(_type, Type.EmptyTypes); return (IFilterMetadata)objFactory(serviceProvider, null); } }
- 创建MyServiceFilterAttribute过滤器
public class MyServiceFilterAttribute : Attribute, IFilterFactory, IFilterMetadata { private readonly Type _type; public MyServiceFilterAttribute(Type type) { this._type = type; } public bool IsReusable { get; set; } pu
.Net6中TypeFilter和ServiceFilter的实现原理
于 2022-03-26 23:39:25 首次发布