Using Foolproof RequiredIf with Enum

We are trying to use Foolproof authentication cancellation [RequiredIf]to check if an email address is needed. We also created an enumeration to avoid using the lookup table identifier in the ViewModel. The code is as follows:

public enum NotificationMethods {
        Email = 1,
        Fax = 2
}

Then in the ViewModel:

[RequiredIf("NotificationMethodID", NotificationMethods.Email)]
public string email {get; set;}

In this scenario, we do not receive an error when the email is not filled out, but is selected as the notification type. Conversely, this works as expected:

[RequiredIf("NotificationMethodID", 1)]
public string email {get; set;}

The only link to this I found here: https://foolproof.codeplex.com/workitem/17245

+4
source share
1 answer

, NotificationMethodID int, , # enum , System.Enum.

var value = NotificationMethods.Email;
string s = value.GetType().Name;

, s "NotificationMethods" not "Int32".

int enum, :

var same = (1 == NotificationMethods.Email); // Gives the compiler error "Operator '==' cannot be applied to operands of type 'int' and 'NotificationMethods'"

enum int ( , , RequiredIfAttribute), , Equals() false, :

var same = ((object)1).Equals((object)NotificationMethods.Email);
Debug.WriteLine(same) // Prints "False".

, NotificationMethods.Email :

var same = ((object)1).Equals((object)((int)NotificationMethods.Email));
Debug.WriteLine(same); // Prints "True"

:

[RequiredIf("NotificationMethodID", (int)NotificationMethods.Email)]
public string email {get; set;}

const int :

public static class NotificationMethods
{
    public const int Email = 1;
    public const int Fax = 2;
}
+5

All Articles