我正在尝试编写一个自定义授权服务,我在其中注册一个模块来检查代表可用活动的特定枚举的权限:
public interface IAuthorizationService
{
void Register<T>(IAuthorizationModule module);
bool Authorize<T>(T activity);
}
public class AuthorizationService : IAuthorizationService
{
private readonly Dictionary<Type, IAuthorizationModule> activities;
public AuthorizationService()
{
activities = new Dictionary<Type, IAuthorizationModule>();
}
public void Register<T>(IAuthorizationModule module)
{
activities[typeof(T)] = module;
}
public bool Authorize<T>(T activity)
{
return activities[typeof(T)].Authorize(activity);
}
}
public interface IAuthorizationModule
{
bool Authorize<T>(T activity);
}
public class OrderAuthModule : IAuthorizationModule
{
public bool Authorize<OrderActivity>(OrderActivity activity)
{
return activity == OrderActivity.Cancel;
}
}
public enum OrderActivity
{
Place,
Cancel,
Refund
}
此代码无法编译,出现以下异常:
'OrderActivity' is a 'type parameter', which is not valid in the given context
上线:return activity == OrderActivity.Cancel;
。
我无法声明接口,IAuthorizationModule<T>
因为我需要一个字典<Type, IAuthorizationModule>
来注册将响应类型T
(枚举)的模块。
我在这里缺少什么?