Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6538373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:43:41+00:00 2026-05-25T10:43:41+00:00

I want to create a custom Data annotation Validator to check whether all the

  • 0

I want to create a custom Data annotation Validator to check whether all the items in the list is unique or not. For example

public class AnyClass{

  [Unique]
  public List<string> UniqueListOfStrings;
}

Now my Unique attribute look like this

public sealed class UniqueAttribute : ValidationAttribute
{

    public UniqueAttribute()
        : base("The items are not unique")
    {
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var listOfValues = (IList<object>)value;
        return listOfValues.Count != listOfValues.Distinct().Count()
                   ? new ValidationResult(ErrorMessageString)
                   : ValidationResult.Success;
    }
}

Till now it is fine, but I want to make the attribute more generic in a sense that I can pass object of any class implementing IEqualityComparer<T>. In that way my new IsValid method will look like

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    var listOfValues = (IList<object>)value;
    return listOfValues.Count !=
           (_comparerClass != null
                ? listOfValues.Distinct(_comparerClass).Count()
                : listOfValues.Distinct().Count())
               ? new ValidationResult(ErrorMessageString)
               : ValidationResult.Success;
}

The problem is I am no way able to send the object. Is there any workaround so that I can use comparer class intended to compare the objects.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:43:42+00:00Added an answer on May 25, 2026 at 10:43 am

    I didn’t find any solution to pass object to a custom Data annotation validator, but solved my problem of making a custom Data annotation Validator to check whether all the items in the list is unique or not. Here is the code:

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if(value == null)
            return ValidationResult.Success;
        var iComparableValues = ((IList)value).Cast<IComparable>().ToList();
        return AreElementsUnique(iComparableValues)
                   ? ValidationResult.Success
                   : new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }
    
    private static bool AreElementsUnique(IList<IComparable> listOfValues)
    {
        var length = listOfValues.Count;
        for(var i = 0;i< length;i++ )
        {
            for(var j =i+1; j< length;j++)
            {
                if(listOfValues[i].CompareTo(listOfValues[j]) ==0)
                    return false;
            }
        }
        return true;
    }
    

    What I did is, instead of assuming that comparing any class implementing IEqualityComparer<T>, I am now assuming I will compare any class implementing IComparable interface. This way I was able to implement the Unique custom attribute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a custom list in Flex for an interface prototype. The
I want to create a custom alert view within my iOS application. For example,
Wanting to create custom data annotation validation. Are there any useful guides / samples
I want to create a WebUserControl (not a custom control) in asp.NET (C#). I
I want to create a custom control in C#. But every time I have
I want to create a custom MSBuild task that changes my .cs files before
I want to create a custom set that will automatically convert objects into a
I want to create a custom property on one of my entities mapped from
If you want to create a custom attribute for MS test (say [Repeat(3)] how
Background : I want to create a custom VB compiler, extending the original compiler,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.