Access on attribute of extended class from SuperCl

2019-08-31 07:30发布

I have the following classes and I need to know if DocPage class have the attribute SessionRequired into the method Render():

public class DocPageBase
{
   void Render()
   {
      // Have extended class SessionRequired?
   }
}

[SessionRequired]
public class DocPage : DocPageBase
{
   // Some properties and methods
}

Thanks for your help!

Regards,

Gerard

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-31 07:49

You'd use GetType() to get the actual type, and then Type.IsDefined to check for the presence of the attribute.

void Render()
{
    if (GetType().IsDefined(typeof(SessionRequiredAttribute), false)
    {
        ...
    }
}
查看更多
登录 后发表回答