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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:47:22+00:00 2026-05-30T06:47:22+00:00

I want to use the System.ComponentModel.DataAnnotations assembly to validate arguments (mapped to properties) for

  • 0

I want to use the System.ComponentModel.DataAnnotations assembly to validate arguments (mapped to properties) for a console app I’m working on. I’ll be using the “buddy class” metadata pattern; it’s worked well for me in the past.

One of the things I need to validate is that exactly one of two types of arguments are provided. In other words, argument foo can be specified, or argument bar, but not both, and not neither.

To this end I started writing a custom validation attribute, which seemed fairly straightforward, but I got a bit lost when I realized I needed to reach outside the property of the validation context, and traverse to a sibling property in the object I am validating (like the CompareAttribute). It seems this is a classic reflection case, but I am scratching my head as to how to proceed. This is what I have so far:

/// <summary>
/// This property, or the other specified, are required, but both cannot be set.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class XORAttribute : ValidationAttribute
{
    /// <summary>
    /// If validation should fail, return this error message.
    /// </summary>
    public string ErrorMessage { get; set; }
    /// <summary>
    /// The name of the other required property that is mutually exclusive of this one.
    /// </summary>
    public string OtherValueName { get; set; }

    public XORAttribute(string otherValueName)
    {
        this.OtherValueName = otherValueName;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        //Something needs to go here.
    }
}

Some assistance here would be appreciated.

  • 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-30T06:47:23+00:00Added an answer on May 30, 2026 at 6:47 am

    Examining other properties with an attribute isn’t all that easy. Other people on here have asked the same question (as seen in this link) and the answer’s, well, not super pretty. Another answer on here recommends using IDataErrorInfo to perform full class validation.

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

Sidebar

Related Questions

I'm trying to create a UniqueAttribute using the System.ComponentModel.DataAnnotations.ValidationAttribute I want this to be
I'm using System.Data.Linq.DataContext file for accessing a mdf Database I want to use the
is it possible to use System.ComponentModel.DataAnnotations and it's belong attribute(such as Required , Range
In ASP.NET MVC we can use the System.ComponentModel.DataAnnotations namespace to assist with form/ model
I want to read the html file.And for that I use System.IO.File.ReadAllText(path) .It can
I want to use some thing unique for a licensing system. i decided to
I want to use GenerationType.IDENTITY for primary keys in my production MySQL system. But
I want to use NSCalendarDate, which exists in /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/ System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSCalendarDate.h but is not present
I want to implement a paperless filing system and was looking to use WIA
I use the following code to layout network drives on a system. I want

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.