用“:base(typeof(classname))”扩展 class 的含义 (C#)

Meaning of extending a class with " : base(typeof(classname))" (C#)

我读过这个

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

但是我还是不明白这行是什么意思

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

CustomAuthorize 是 class 扩展 IAuthorizationFilter.

base(typeof(CustomAuthorize)) 是什么意思?

它是否试图调用 IAuthorizationFilter 的构造函数?为什么使用 typeof ?

完整的class是:

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

该问题与其他类似问题不同,因为我想具体了解“base(typeof(CustomAuthorize))”的含义。

base(...)用于称呼你基地的成员class。在这种情况下,您似乎正在调用您继承自的 class 的构造函数。

typeof(...) returns the Type 已定义类型。

总而言之,命令

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

调用了class的构造函数,CustomAuthorizeAttribute通过传递参数继承自typeof(CustomAuthorize)。然后可以使用这个传递的类型来实例化它,以便稍后在基础 class.

中使用