C# 不能对ContainsGenericParameters为true的类型或方法执行后期绑定操作。在.NETCore3.1中
我尝试在.NETCore3.1应用程序中使用装饰器模式和泛型类实现内存缓存。我得到了以下运行时异常 System.InvalidOperationException:“不能对ContainsGenericParameters为true的类型或方法执行后期绑定操作。” 请查找我使用过的相关类、接口和方法C# 不能对ContainsGenericParameters为true的类型或方法执行后期绑定操作。在.NETCore3.1中,c#,asp.net-core,generics,caching,decorator-pattern,C#,Asp.net Core,Generics,Caching,Decorator Pattern,我尝试在.NETCore3.1应用程序中使用装饰器模式和泛型类实现内存缓存。我得到了以下运行时异常 System.InvalidOperationException:“不能对ContainsGenericParameters为true的类型或方法执行后期绑定操作。” 请查找我使用过的相关类、接口和方法 public interface IDataService<T> where T: class { ICollection<T> fetch
public interface IDataService<T> where T: class
{
ICollection<T> fetchData();
}
公共接口IDataService,其中T:class
{
ICollection fetchData();
}
公共类数据服务:IDataService
T:在哪里上课
{
私有只读服务器ViceProvider\u服务提供商;
公共数据服务(IServiceProvider服务提供商)
{
_服务提供者=服务提供者;
}
公共ICollection fetchData()
{
使用(var context=new ApplicationDBContext(_serviceProvider.GetRequiredService())
{
ICollection result=context.Set().ToList();
返回结果;
}
}
}
公共类DataServiceCacheDecorator:IDataService
T:在哪里上课
{
私有只读IDataService(数据服务);
私有只读IMemoryCache\u memoryCache;
公共DataServiceCacheDecorator(IDataService dataService,IMemoryCache memoryCache)
{
_数据服务=数据服务;
_memoryCache=memoryCache;
}
公共ICollection fetchData()
{
常量字符串cacheKey=“数据键”;
if(_memoryCache.TryGetValue(cacheKey,out var data))
{
返回数据;
}
data=_dataService.fetchData();
var cacheEntryOptions=new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(10));
_Set(cacheKey、data、cacheEntryOptions);
返回数据;
}
}
ServiceExtensions类是用于启动的静态帮助器类。下面介绍了配置it缓存的相关方法。在这个方法中,我希望在创建实例时在运行时将类型绑定到泛型类
public static void ConfigureCache(this IServiceCollection services)
{
services.AddMemoryCache();
services.AddScoped(typeof(IDataService<>), typeof(DataService<>));
services.AddScoped(typeof(IDataService<>), typeof(DataServiceCachingDecorator<>));
var serviceProvider = services.BuildServiceProvider();
var memoryCache = serviceProvider.GetService<IMemoryCache>();
Type genericBase = typeof(IDataService<>);
var cacheingDecoratorType = genericBase.MakeGenericType(typeof(DataServiceCachingDecorator<>));
dynamic cacheingDecorator = Activator.CreateInstance(cacheingDecoratorType, memoryCache);
cacheingDecorator.Add("fetchData");
}
公共静态无效配置缓存(此IServiceCollection服务)
{
services.AddMemoryCache();
addScope(类型化(IDataService),类型化(数据服务));
addScope(typeof(IDataService),typeof(DataServiceCachingDecorator));
var serviceProvider=services.BuildServiceProvider();
var memoryCache=serviceProvider.GetService();
类型genericBase=typeof(IDataService);
var cacheingdecortotype=genericBase.MakeGenericType(typeof(DataServiceCachingDecorator));
dynamic CachingDecorator=Activator.CreateInstance(CachingDecoratorType,memoryCache);
cacheingDecorator.Add(“fetchData”);
}
需要在不同的表中缓存多个数据。例:客户名单
public class CustomerLogic
{
private IMapper _mapper;
private readonly IDataService<Customer> _dataService;
public CustomerLogic(IMapper mapper, IDataService<Customer> dataService)
{
_mapper = mapper;
_dataService = dataService;
}
public IEnumerable<CustomerDto> GetCustomersList()
{
IEnumerable<CustomerDto> customersList = _dataService.fetchData().Select(_mapper.Map<Customer, CustomerDto>);
return customersList;
}
公共类CustomerLogic
{
专用图像映射器;
私有只读IDataService(数据服务);
公共CustomerLogic(IMapper映射器、IDataService数据服务)
{
_映射器=映射器;
_数据服务=数据服务;
}
公共IEnumerable GetCustomerList()
{
IEnumerable CustomerList=\u dataService.fetchData().Select(\u mapper.Map);
返回客户列表;
}
如果有人知道如何解决此问题,请告诉我。非常感谢您的帮助。不清楚您正在尝试做什么,但
Activator.CreateInstance(cacheingDecoratorType,memoryCache);
无法工作,因为cacheingDecoratorType
类似于IDataService
,所以并不是所有的泛型参数都被填充,所以无法实例化。
public static void ConfigureCache(this IServiceCollection services)
{
services.AddMemoryCache();
services.AddScoped(typeof(IDataService<>), typeof(DataService<>));
services.AddScoped(typeof(IDataService<>), typeof(DataServiceCachingDecorator<>));
var serviceProvider = services.BuildServiceProvider();
var memoryCache = serviceProvider.GetService<IMemoryCache>();
Type genericBase = typeof(IDataService<>);
var cacheingDecoratorType = genericBase.MakeGenericType(typeof(DataServiceCachingDecorator<>));
dynamic cacheingDecorator = Activator.CreateInstance(cacheingDecoratorType, memoryCache);
cacheingDecorator.Add("fetchData");
}
public class CustomerLogic
{
private IMapper _mapper;
private readonly IDataService<Customer> _dataService;
public CustomerLogic(IMapper mapper, IDataService<Customer> dataService)
{
_mapper = mapper;
_dataService = dataService;
}
public IEnumerable<CustomerDto> GetCustomersList()
{
IEnumerable<CustomerDto> customersList = _dataService.fetchData().Select(_mapper.Map<Customer, CustomerDto>);
return customersList;
}