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

  • SEARCH
  • Home
  • 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 8113623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:53:16+00:00 2026-06-06T02:53:16+00:00

I have this custom validation attribute for validating a collection. I need to adapt

  • 0

I have this custom validation attribute for validating a collection. I need to adapt this to work with IEnumerable. I tried making the attribute a generic, but you can’t have a generic attribute.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class CollectionHasElements : System.ComponentModel.DataAnnotations.ValidationAttribute 
{
     public override bool IsValid(object value)
     {
         if (value != null && value is IList)
         {
             return ((IList)value).Count > 0;
         }
         return false;
     }
}

I’m having trouble casting it to an IEnumerable so that I can check its count() or any().

Any ideas?

  • 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-06-06T02:53:18+00:00Added an answer on June 6, 2026 at 2:53 am

    Try this

    var collection = value as ICollection;
    if (collection != null) {
        return collection.Count > 0;
    }
    
    var enumerable = value as IEnumerable;
    if (enumerable != null) {
        return enumerable.GetEnumerator().MoveNext();
    }
    
    
    return false;
    

    or, since C# 7.0 with pattern matching:

    if (value is ICollection collection) {
        return collection.Count > 0;
    }
    if (value is IEnumerable enumerable) {
        return enumerable.GetEnumerator().MoveNext();
    }
    return false;
    

    Note: Testing for ICollection.Count is more efficient than getting an enumerator and beginning to enumerate the enumerator. Therefore I try to use the Count property whenever possible. However, the second test would work alone, since a collection always implements IEnumerable.

    The inheritance hierarchy goes like this: IEnumerable > ICollection > IList. IList implements ICollection and ICollection implements IEnumerable. Therefore IEnumerable will work for any well designed type of collection or enumeration but not IList. For instance Dictionary<K,V> does not implement IList but ICollection and therefore also IEnumeration.


    The .NET naming conventions say that an attribute class name should always end in "Attribute". Therefore your class should be named CollectionHasElementsAttribute. When applying the attribute you can drop the "Attribute" part.

    [CollectionHasElements]
    public List<string> Names { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my custom model binder. I have my breakpoint set at BindModel but
Created a custom validation attribute and I would like to get it to work
I have written a custom credit card validation attribute that checks CardNumber property is
I have created a Custom Validation Attribute: public sealed class DateAttribute : DataTypeAttribute {
I need some help - I am trying to use a custom validation attribute
I have created a custom validation attribute by subclassing ValidationAttribute. The attribute is applied
I have a custom asp.net mvc class validation attribute. My question is how can
I have build my own custom validation framework for WPF using attribute based validation.
I have created custom validation attribute [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited =
I want to apply scope limiter in my custom validation I have this Product

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.