jeudi 5 novembre 2020

How to get constructor named arguments from attribute

I have the same problem as this question. The problem is that, even when using named arguments in the attribute's constructor, I still get an empty collection.

This is my Attribute definition:

public class MenuItemAttribute : Attribute
{
    ApplicationCategoryEnum ApplicationCategory { get; }
    string ControllerDisplayName { get; }
    bool IsDefaultRoute { get; }

    public MenuItemAttribute(ApplicationCategoryEnum applicationCategory, string controllerDisplayName, bool isDefaultRoute)
    {
        ControllerDisplayName = controllerDisplayName;
        ApplicationCategory = applicationCategory;
        IsDefaultRoute = isDefaultRoute;
    }
}

and this is how I use it:

[MenuItem(
    applicationCategory: ApplicationCategoryEnum.CONTROLPANEL,
    controllerDisplayName: "Account Management",
    isDefaultRoute: false)]
public class AccountController : Controller { /// }

This is how I get through the Controller's attributes:

var attributeData = controllerInfo
    .CustomAttributes
    .FirstOrDefault(attrs => attrs.AttributeType == typeof(MenuItemAttribute))
    .NamedArguments;

Arguments are there as I can see them if I use ConstructorArguments instead of NamedArguments.

What am I missing?





Aucun commentaire:

Enregistrer un commentaire