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 6804959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:31:46+00:00 2026-05-26T19:31:46+00:00

We have Generics Property public class BE { private List<Admin_Fee> _Admin_Fee = new List<Admin_Fee>();

  • 0

We have Generics Property

 public class BE
    {
        private List<Admin_Fee> _Admin_Fee = new List<Admin_Fee>();
        [StringLengthValidator(3,
        MessageTemplate = "Fund City Can't be more than 3 Chars")]  
        public MyProperty<string> FUND_CITY { get; set; }

        public MyProperty<int> SomeOtherProperty { get; set; }

        public List<MyPropertyBase> MyDataPoints { get; set; }

    }

I want to put StingLengthValidator using VAB on Generic Property, and getting error that :

Value is not expected type

Can some one help?

  • 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-26T19:31:47+00:00Added an answer on May 26, 2026 at 7:31 pm

    The reason why you are getting an error is fairly straight forward: you are trying to use a StringLengthValidator against a type that isn’t a string (it is actually MyProperty<string>).

    The question is what to do to validate the property? It’s tricky because the design doesn’t really fit well within the Validation Application Block design.

    Typically you would just apply an ObjectValidator to validate the MyProperty class but that doesn’t really fit in this case since it looks like you aim to use MyProperty to hold various values each with different rules so you can’t really apply validator attributes to MyProperty.

    I think you can achieve what you want with custom validators. I’m thinking you can wrap existing validators inside your custom validator.

    Here I’m assuming MyProperty looks something like:

    public class MyProperty<T>
    {
        public T Value { get; set; }
    }
    

    Then you can create a custom validator MyPropertyValidator:

    public class MyPropertyValidatorAttribute : ValidatorAttribute
    {
        Microsoft.Practices.EnterpriseLibrary.Validation.Validator validator;
    
        public MyPropertyValidatorAttribute(Type validator, params object[] validatorArgs)
        {
            this.validator = Activator.CreateInstance(validator, validatorArgs) 
                as Microsoft.Practices.EnterpriseLibrary.Validation.Validator;    
        }
    
        protected override Microsoft.Practices.EnterpriseLibrary.Validation.Validator DoCreateValidator(Type targetType)
        {
            return new MyPropertyValidator(validator);
        }
    }
    
    public class MyPropertyValidator : Microsoft.Practices.EnterpriseLibrary.Validation.Validator
    {
        Microsoft.Practices.EnterpriseLibrary.Validation.Validator validator;
    
        public MyPropertyValidator(Microsoft.Practices.EnterpriseLibrary.Validation.Validator validator)
            : this(validator.MessageTemplate, validator.Tag)
        {
            this.validator = validator;
        }
    
        public MyPropertyValidator(string message, string tag) : base(message, tag)
        {
        }
    
        protected override string DefaultMessageTemplate
        {
            get { return ""; }
        }
    
        public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            var val = objectToValidate;
    
            Type t = objectToValidate.GetType();
            var propInfo = t.GetProperty("Value");
    
            if (propInfo != null)
            {
                val = propInfo.GetValue(objectToValidate, null);
            }
    
            validator.DoValidate(val, currentTarget, key, validationResults);
        }
    }
    

    Then you could annotate your class like so:

    public class BE
    {
        [MyPropertyValidator( 
            typeof(StringLengthValidator), 
            0, RangeBoundaryType.Ignore,
            3, RangeBoundaryType.Inclusive,
            "Fund City Can't be more than 3 Chars",
            false)]
        public MyProperty<string> FUND_CITY { get; set; }
    
        [MyPropertyValidator(
            typeof(RangeValidator),
            0, RangeBoundaryType.Inclusive,
            10, RangeBoundaryType.Inclusive,
            "Must be between 0 and 10", 
            false)]
           public MyProperty<int> SomeOtherProperty { get; set; }
    }
    

    I haven’t tested it extensively but it seems to work. It does have a few drawbacks:

    • It’s inflexible. For example it doesn’t support composite validators or other more complicated scenarios. You could maybe implement all those cases but it would get messy.
    • My implementation is using reflection which would be better to avoid.
    • Potential boxing of value types
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class MyClass containing a private List<MySecondClass> myList . The list is
I have this class: public class CampaignMaps : List<CampaignMap> { }; The CampaignMap object
Can anyone tell me why ActionScript 3, a statically typed language, doesn't have generics?
I have the following class in my model: public class Notebook : TableServiceEntity {
So, I have a list containing a custom class, MyClass MyClass has properties, which
I have a generic collection of type MyImageClass, and MyImageClass has an boolean property
Hashtables have a syncroot property but generic dictionaries don't. If I have code that
I have not used generics much and so cannot figure out if it is
In .NET, the Generics Lists have a sort function that accepts IComparer or Comparison
I have started using of generics in Delphi 2010 but I have a problem

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.