How to get string value of custom constant of C # resource class?

I created the following custom ValidationAttribute attribute:

public class DateRangeAttribute : ValidationAttribute, IClientValidatable { public DateTime MinimumDate = new DateTime(1901, 1, 1); public DateTime MaximumDate = new DateTime(2099, 12, 31); public DateRangeAttribute(string minDate, string maxDate, string errorMessage) { MinimumDate = DateTime.Parse(minDate); MaximumDate = DateTime.Parse(maxDate); ErrorMessage = string.Format(errorMessage, MinimumDate.ToString("MM/dd/yyyy"), MaximumDate.ToString("MM/dd/yyyy")); } } 

which I would like to use in my MVC4 view model as follows:

 [DateRange(Resources.MinimumDate, Resources.MaximumDate, "Please enter a date between {0} and {1}")] 

Resources is a class of generated resources based on a set of parameters stored in an SQL database. A simplified version of the generated code for the above two resource properties:

 public class Resources { public const string MinimumDate = "PropMinimumDate"; public static string PropMinimumDate { get { return "12/15/2010" } } public const string MaximumDate = "PropMaximumDate"; public static string PropMaximumDate { get { return "12/15/2012" } } } 

While I do not understand how this works, I understand that the typical use of resources in ValidationAttributes automatically maps Resources.MinimumDate to PropMinimumDate and returns the value "12/15/2010".

I cannot figure out how to manually complete this program leap so that I can pass two date values โ€‹โ€‹to my custom ValidatorAttribute attribute. As at present, "PropMinimumDate" and "PropMaximumDate" are the values โ€‹โ€‹of the minDate and maxDate (respectively) parameter passed to the DateRangeAttribute constructor.

If i try

 [DateRange(Resources.PropMinimumDate, Resources.PropMaximumDate, "Please enter a date between {0} and {1}")] 

I get a compilation error:

The attribute argument must be a constant expression, a typeof expression, or an array creation attribute type attribute expression

Is there a way to accomplish this task, or am I trying to do the impossible?

+4
source share
2 answers

You need to take the Type of the resource class as an argument, and then use reflections to get the property value.

 public class DateRangeAttribute : ValidationAttribute, IClientValidatable { public DateTime MinimumDate = new DateTime(1901, 1, 1); public DateTime MaximumDate = new DateTime(2099, 12, 31); private Type _resourceType; public DateRangeAttribute(string minDate, string maxDate, string errorMessage, Type resourceType) { _resourceType = resourceType; var minDateProp = _resourceType.GetProperty(minDate, BindingFlags.Static | BindingFlags.Public); var minDateValue = (string) minDateProp.GetValue((object) null, (object[]) null)); MinimumDate = DateTime.Parse(minDateValue); // similarly get the value for MaxDate ErrorMessage = string.Format(errorMessage, MinimumDate.ToString("MM/dd/yyyy"), MaximumDate.ToString("MM/dd/yyyy")); } } 

For instance,

 [DateRange(Resources.MinimumDate, Resources.MaximumDate, "Please enter a date between {0} and {1}", typeof(Resources))] 
+1
source

You are not trying to do the impossible, but you have to get around the restriction only a little. So, to match the compiler, we have two options, and the first is the most ideal, change the generated Resources class.

 public class Resources { public const string PropMinimumDate = "12/15/2010"; public const string PropMaximumDate = "12/15/2012"; } 

Now, if we canโ€™t do this, we will go the other way, let us modify the class in which the decorated property is located, and add a pair of const .

 public class EntityClass { private const string MinimumDate = "12/15/2010"; private const string MaximumDate = "12/15/2012"; [DateRange(MinimumDate, MaximumDate, "Please enter a date between {0} and {1}")] } 

However, if you cannot generate an entity class, the latter type of option violates your need to pull values โ€‹โ€‹from the database. So hopefully you can change the Resources class.

0
source

Source: https://habr.com/ru/post/1440171/


All Articles