Validating custom attribute parameters during design / build

I have an attribute CustomAuthorizethat checks if a user has access to functions (a user or a role can be associated with elements from a hierarchical set of functions).

For this action method ...

[CustomAuthorize("Security.Admin.ManageWidgets.Update")]

This works, but I am concerned that changes to the Security object may cause problems that will not be detected before execution. I understand that I can write unit tests to mitigate this risk, but I would like to know if the attribute parameter can be checked at compile time. I also like when Intellisense helps me type this expression.

Ideally, I could pass in a lambda expression.

[CustomAuthorize(i => i.Admin.ManageWidgets.Update)]

Unfortunately, this one is currently not possible ( additional information from Microsoft ).

I also tried to encapsulate the expression, hoping that it would be evaluated and then passed to the attribute as a string, but it also could not be compiled with the same error (the expression cannot contain anonymous methods or lambda expressions).

[CustomAuthorize(LambdaToString(i => i.Admin.ManageWidgets.Update))]

How to add some development time / runtime support for custom attribute parameters?

+5
source share
3 answers

T4 templates , , BennyM, .

+1

.

public static class Rights
{
    public const string UpdateWidgets = "UpdateWidgets";
}

unittests , , .

[CustomAuthorize(Rights.UpdateWidgets)]
+3

, - , , .

, :

[CustomAuthorize(typeof(Security.Admin.ManageWidgets), "Update")]

, , , , .

+1

All Articles