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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:31:07+00:00 2026-05-13T21:31:07+00:00

I again need help by you, this time I struggle with covariance, contravariance, delegates

  • 0

I again need help by you, this time I struggle with covariance, contravariance, delegates and an simple idea blowing up…

I want to implement an attribute for our businessobject-properties that takes a delegate and the needed parameters for that one, so that I can work with reflection, read out the attribute and perform a validation on the property value.

The reason behind this is, we are using Windows.Forms with DataBinding and need to set the DataBinding update method to OnPropertyChanged, to get a properly working refresh on the GUI.
We do need however a way to react in the validating-events of the controls to validate the property correctly, to see if the user can actually e.g. save the object. But the Validating-Event of the control occurs only after writing the value to the property. Having a validation in the setter of the property would cause a crash and we could not provide the user exact information what is wrong unless we implement the validation a second time (or extract it to a method called from the setter).

To keep this most elegant and clean, I thought one of the following would be nice to have:

[PropertyValidator(ValidationHelper.ValidateString, new StringValidatorArgs(true, 3, 15))]

That way I could iterate via reflection over all properties, perform all validations we want them to and set a PropertyValidator-Attribute for with the correct Method. But I played with the idea a bit and do not get this anyway to work, here is what I have, might be you have an idea about how to achive this.

public delegate bool Validator(object validatee, ValidatorArgs v);

public class ValidatorArgs
{
}

public class StringValidatorArgs : ValidatorArgs
{
    public StringValidatorArgs(bool nullCheck, int minLength, int maxLength)
    {
        this.NullCheck = nullCheck;
        this.MinLength = minLength;
        this.MaxLength = maxLength;
    }

    public bool NullCheck { get; set; }
    public int MinLength { get; set; }
    public int MaxLength { get; set; }
}

public class MyClass
{
    [PropertyValidator(ValidationHelper.ValidateString, new StringValidatorArgs(true, 3, 15))]
    public string MyString { get; set; }
}

public static class ValidationHelper
{
    public static bool ValidateString(object validatee, StringValidatorArgs v)
    {
        return true;
    }
}

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
public class PropertyValidatorAttribute
    : Attribute
{
    #region Constructor

    private PropertyValidatorAttribute()
    {
    }

    public PropertyValidatorAttribute(Validator validator, ValidatorArgs args)
    {
        this.Validator = validator;
        this.Args = args;
    }

    #endregion

    #region Properties

    public Validator Validator
    {
        get;
        private set;
    }

    public ValidatorArgs Args
    {
        get;
        private set;
    }

    #endregion
}

Any hints welcome…

  • 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-13T21:31:08+00:00Added an answer on May 13, 2026 at 9:31 pm

    What about implementing IDataErrorInfo to provide validation information from your object, instead of (I’m assuming) throwing an exception from the setter on bad data? Most Windows Forms controls are IDataErrorInfo savvy, and will provide corresponding UI validation information on a per-property or per-object basis.

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

Sidebar

Related Questions

I need your help with this one again. I want to send special characters
Sorry to bother again, but I really need help transforming this Python2 code into
Once again I need your Help, am thanking you as every time you helped
This is my first time using a typedef enum and I need some help
I really need help with this. This time 2 days ago, my app was
This time, I need your help in something related to php. My users script
I need your help again :) I'm trying to do a plugin with jQuery
I need some help with jQuery script again :-) Just trying to play with
Morning again..., Sorry to bother everyone but I need more help... I haven't done
I need a form width a checkbox Don't show this again for my winform,

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.