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";
}
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()
{
}
You can't use List<T>.
Attributes have restrictions on parameters and property types, because they have to be available at compile-time. Using attributes in C#
Use an array instead:
//constructor
public CustomAuthorize(string[] _multipleProgramID)
{
...
}
// usage
[CustomAuthorize(new string[] { ProgramList.SURVEY_INPUT, ProgramList.SURVEY_OUTPUT })]
我试图做类似的事情,最终传递了一个逗号分隔的string
并在属性构造函数中使用string.Spilt(',')
将其转换为数组。
You just need to use "params" keyword in your custom attribute constructor and make your programList "enum"
[CustomAttribute(ProgramList.SURVEY_INPUT, ProgramList.SURVEY_OUTPUT)]
[HttpPost]
public ActionResult DeleteWaterQualityItems(string sourceID, string wqID) {
// ..other code...
}
in you custom attribute class use this
public class CustomAuthorize : AuthorizeAttribute {
//Constructor
public CustomAuthorize (params ProgramList[] programListTypes) {
multipleProgramID= programListTypes;
}
private ProgramList[] multipleProgramID;
}
and you enum class
public enum ProgramList : int
{
SURVEY_INPUT = 001;
SURVEY_OUTPUT =002;
}
One possible option:
public class ConstraintExpectedValues : ConstraintAttribute
{
public ConstraintExpectedValues(object[] expectedValues)
{
this.ExpectedValues = expectedValues;
}
public object[] ExpectedValues { get; set; }
}
Usage:
[ConstraintExpectedValues(new object[] {5,7,9})]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.