我意識到您無法在屬性本身中獲取目標實體,但在使用CodeAccessSecurityAttribute時關聯的Permission對象又如何? Permission對象在運行時被調用,所以看起來應該有一種方法,但我很茫然。檢索CodeAccessSecurityAttribute中的目標元素
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?
}
}
是的,這是唯一的解決方案,我可以拿出來,但反射並不是一個項目的大小和頻率它需要發生的選項。感謝您的反饋。 – Mike 2008-12-10 16:07:06