0

I have read this

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/base.

but I still don't understand what this line means.

public CustomAuthorizeAttribute(string role) : base(typeof(CustomAuthorize))

CustomAuthorize is a class that extends IAuthorizationFilter.

what does base(typeof(CustomAuthorize)) mean?

Is it trying to call the constructor of the IAuthorizationFilter? why is it using typeof ?

The full class is:

public class CustomAuthorizeAttribute : TypeFilterAttribute
 {
        public CustomAuthorizeAttribute(string role) : base(typeof(CustomAuthorize))
       {
            Arguments = new object[] { role };
       }
 }

public class CustomAuthorize : IAuthorizationFilter
    {
        private readonly string role;

        public CustomAuthorize(string role)
        {
            this.role = role;
        }

        public void OnAuthorization(AuthorizationFilterContext context)
        {
         Some code

The question differs from other similar questions as i want to know specifically what "base(typeof(CustomAuthorize))" means.

Mr.Gomer
  • 501
  • 5
  • 14
  • Interfaces cannot have constructors. Only structs and classes can have constructors. – gunr2171 May 01 '22 at 18:35
  • Does this answer your question? [What does the "base" syntax mean?](https://stackoverflow.com/questions/10503255/what-does-the-base-syntax-mean) – gunr2171 May 01 '22 at 18:35
  • What's the signature of your class? What class or struct does it inherit? Yes it might also _implement_ an interface, but you can only call `base` for the inherited type. – gunr2171 May 01 '22 at 18:38
  • I edited the question to make it more clear – Mr.Gomer May 01 '22 at 18:42
  • `typeof` returns a `Type`, but your base class's constructor wants a `string`. Did you mean to use `typeof().Name`, or `nameof`? What actual information do you want passed to the base class's constructor? – gunr2171 May 01 '22 at 18:46

1 Answers1

2

base(...) is used to call a member of your base class. In this case, it seems like you are calling the constructor of the class you inherit from.

typeof(...) returns the Type of a defined type.

To sum it up, the command

public CustomAuthorizeAttribute(string role) : base(typeof(CustomAuthorize))

calls the constructor of the class, CustomAuthorizeAttribute inherits from by passing the parameter typeof(CustomAuthorize). This passed type then may be used to instanciate it for usage later on in the base class.

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • Ok but then can't you just use ": base(IAuthorizationFilter)" instead ? – Mr.Gomer May 01 '22 at 19:09
  • No, because ```typeof(CustomAuthorize)``` is not the same like ```IAuthorizationFilter``` – Fruchtzwerg May 01 '22 at 19:11
  • I'm confused tbh. Also this "calls the constructor of the class, CustomAuthorizeAttribute inherits from by passing the parameter typeof(CustomAuthorize). This passed type then may be used to instanciate it for usage later on in the base class." was a bit confusing .. can it be rephrased? im sorry – Mr.Gomer May 01 '22 at 19:14
  • Not really ... You need to think step by step here. Check also the constructor of your base class to understand it. Maybe just ignore the last sentence. – Fruchtzwerg May 01 '22 at 20:06