How can I pass an attribute parameter type with List<string> in C#?

user3431414

How can I pass a List to a construtor?

It shows a message:

Error 14 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

public class CustomAuthorize : AuthorizeAttribute {
    private List<string> multipleProgramID;

    //constructor
    public CustomAuthorize(List<string> _multipleProgramID) {
        multipleProgramID = _multipleProgramID;
    }
}

[CustomAuthorize(new List<string>(new string[] { ProgramList.SURVEY_INPUT, ProgramList.SURVEY_OUTPUT } ))]
[HttpPost]
public ActionResult DeleteWaterQualityItems(string sourceID, string wqID) {
    // ..other code...
}

public class ProgramList {
    public const string SURVEY_INPUT = "A001";
    public const string SURVEY_INPUT = "A002";
}
Jon Skeet

The problem isn't passing a List<string> to a constructor in general - the problem is that you're trying to use it for an attribute. You basically can't do that, because it's not a compile-time constant.

It looks like ProgramList is effectively an enum - so you might want to make it an enum instead:

 [Flags]
 public enum ProgramLists
 {
     SurveyInput,
     SurveyOutput,
     ...
 }

Then make your CustomAuthorizeAttribute (which should be named like that, with a suffix of Attribute) accept a ProgramLists in the constructor. You'd specify it as:

[CustomAuthorize(ProgramLists.SurveyInput | ProgramLists.SurveyOutput)]

You can then have a separate way of mapping each ProgramLists element to a string such as "A001". This could be done by applying an attribute to each element, or maybe having a Dictionary<ProgramLists, string> somewhere.

If you really want to keep using strings like this, you could make CustomAuthorizeAttribute accept a single comma-separated list, or make it an array instead of a list and use a parameter array:

[AttributeUsage(AttributeTargets.Method)]
public class FooAttribute : Attribute
{
    public FooAttribute(params string[] values)
    {
        ...
    }
}

[Foo("a", "b")]
static void SomeMethod()
{
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I pass multiline String parameter?

From Dev

How to pass parameter that can be of type int/double/string/int[] from C# to native C?

From Dev

How can I pass parameter of type HttpContent in web api?

From Dev

How can I cast an Object to a generic List I don't know the type parameter of in C++/CLI?

From Dev

How can I pass List in my method parameter?

From Dev

How can i pass list of objects as a parameter on WebApi Odata?

From Dev

How do I pass an objects attribute as a parameter?

From Dev

How do I pass an objects attribute as a parameter?

From Dev

How can I do html multi elements list with type attribute?

From Dev

Can I pass a Class type as a procedure parameter

From Dev

Can I pass a type as parameter in PHP?

From Dev

Can I pass a Class type as a procedure parameter

From Dev

Can I pass a type as a parameter to this function?

From Dev

How I can pass lambda expression to c++ template as parameter

From Dev

How can i pass a file as a parameter in C++?

From Dev

How can i pass a file as a parameter in C++?

From Dev

How to pass empty list with type parameter?

From Dev

How can I pass a template to a directive in an attribute?

From Dev

How can I use an F# discriminated union type as a TestCase attribute parameter?

From Dev

How can I get type double array from List<string>?

From Dev

how can i hook if return type is a list<string> in xposed?

From Dev

How can I pass view as parameter in android?

From Java

How can I pass a parameter to a setTimeout() callback?

From Dev

How can I pass the parameter in the function num()?

From Dev

How can I pass EventHandler as a method parameter

From Dev

how can i pass variable in parameter as a function?

From Dev

How can I pass argument with parameter to gulp?

From Dev

How can I pass a struct to a function as parameter?

From Dev

How can I pass a Method as a parameter in Android?

Related Related

  1. 1

    How can I pass multiline String parameter?

  2. 2

    How to pass parameter that can be of type int/double/string/int[] from C# to native C?

  3. 3

    How can I pass parameter of type HttpContent in web api?

  4. 4

    How can I cast an Object to a generic List I don't know the type parameter of in C++/CLI?

  5. 5

    How can I pass List in my method parameter?

  6. 6

    How can i pass list of objects as a parameter on WebApi Odata?

  7. 7

    How do I pass an objects attribute as a parameter?

  8. 8

    How do I pass an objects attribute as a parameter?

  9. 9

    How can I do html multi elements list with type attribute?

  10. 10

    Can I pass a Class type as a procedure parameter

  11. 11

    Can I pass a type as parameter in PHP?

  12. 12

    Can I pass a Class type as a procedure parameter

  13. 13

    Can I pass a type as a parameter to this function?

  14. 14

    How I can pass lambda expression to c++ template as parameter

  15. 15

    How can i pass a file as a parameter in C++?

  16. 16

    How can i pass a file as a parameter in C++?

  17. 17

    How to pass empty list with type parameter?

  18. 18

    How can I pass a template to a directive in an attribute?

  19. 19

    How can I use an F# discriminated union type as a TestCase attribute parameter?

  20. 20

    How can I get type double array from List<string>?

  21. 21

    how can i hook if return type is a list<string> in xposed?

  22. 22

    How can I pass view as parameter in android?

  23. 23

    How can I pass a parameter to a setTimeout() callback?

  24. 24

    How can I pass the parameter in the function num()?

  25. 25

    How can I pass EventHandler as a method parameter

  26. 26

    how can i pass variable in parameter as a function?

  27. 27

    How can I pass argument with parameter to gulp?

  28. 28

    How can I pass a struct to a function as parameter?

  29. 29

    How can I pass a Method as a parameter in Android?

HotTag

Archive