我有几个 WCF 服务都实现了 All() 方法,所以我创建了一个通用基类来实现 All() 方法,但是每个 WCF 服务调用都需要自己的安全角色。
public class QueryService<T>
where T : class
{
public IQueryable<T> All()
{
// query data
return data;
}
}
public class CarService : QueryService<Car>
{
[PrincipalPermission(SecurityAction.Demand, Role = "Car_R")]
public new IQueryable<Car> All()
{
return base.All();
}
}
public class BikeService : QueryService<Bike>
{
[PrincipalPermission(SecurityAction.Demand, Role = "Bike_R")]
public new IQueryable<Bike> All()
{
return base.All();
}
}
有没有办法将 PrincipalPermission 注释移动到基类 (QueryService) 并在派生类中定义 Role 字符串,这样我就不必重写基类的每个方法(并调用 base.xxx)。
像这样的东西:(虽然不起作用......)
public class QueryService<T>
where T : class
{
public const string RoleFromDerivedClass = "Default";
[PrincipalPermission(SecurityAction.Demand, Role = RoleFromDerivedClass)]
public IQueryable<T> All()
{
// query data
return data;
}
}
public class CarService : QueryService<Car>
{
public const string RoleFromDerivedClass = "Car_R";
}
public class BikeService : QueryService<Bike>
{
public const string RoleFromDerivedClass = "Bike_R";
}