Access on attribute of extended class from SuperCl

2019-08-31 07:49发布

问题:

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:

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)
    {
        ...
    }
}