c# - Custom "At least" attribute -
i'm trying create attribute validate model.
in model have list. list must have determined number of items matches criteria, "at least 1 active item" or "at least 1 active item "john" name".
my code this:
public class foo { [atleast(1, new tuple<string, object>("active", true))] public list<item> listofsomething { get; set; } [atleast(1, new tuple<string, object>("active", true), new tuple<string, object>("name", "john"))] public list<item> anotherlist { get; set; } } public class item { public string name { get; set; } public bool active { get; set; } } public class atleastattribute : validationattribute { public int minlength { get; set; } public tuple<string, object>[] propertiesandvalues { get; set; } public atleastattribute(int minlength,params tuple<string, object>[] propsnvalues) { minlength = minlength; propertiesandvalues = propsnvalues; } }
i trying pass tuple<string, object>
property , wanted value. i'm getting error:
an attribute argument must constant expression, typeof expression or array creation expression of attribute parameter type
anyone has way this?
agreeing comment davidg; way supply kind of constant-create expressions limiting primitive types. if want implement constraint values specify should convertible string.
then can think of syntax like
name=john;active=true
but it's not simple, might want include ;
or =
in string values, need figure out string syntax , escape character:
name='john\\'s pizza'
or
name='john''s pizza'
then need parse information; regex might able it.
if can without string values containing characters use splitting, split string on ;
=
, presto.
Comments
Post a Comment