我意识到您无法在 Attribute 本身中获取目标实体,但是在使用 CodeAccessSecurityAttribute 时在关联的 Permission 对象中呢?Permission 对象在运行时被调用,所以似乎应该有办法,但我不知所措。
public sealed class MySecurityAttribute : CodeAccessSecurityAttribute
{
public override IPermission CreatePermission()
{
MySecurityPermission permission = new MySecurityPermission();
//set its properties
permission.Name = this.Name;
permission.Unrestricted = this.Unrestricted;
return permission;
}
}
public class MySecurityPermission : IPermission, IUnrestrictedPermission
{
public MySecurityPermission(PermissionState state)
{
// what method was the attribute decorating that
// created this MySecurityPermission?
}
public void Demand()
{
// Or here?
}
}