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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:31:55+00:00 2026-06-06T09:31:55+00:00

I am trying to write a NotAttribute validator attribute for my MVC3 project. I

  • 0

I am trying to write a NotAttribute validator attribute for my MVC3 project. I want to use it like this:

[Not("00000000-0000-0000-0000-000000000000", typeof(Guid), ErrorMessage = "You must select a valid id.")]
public Guid ObjectId { get; set; }

Now I don’t want to tie the NotAttribute to a Guid, but the object should be parseable from a string; I want to look for a TryParse method first, then a Parse method; if both should fail, and the type is not already a string, I want to throw an error. So far, so good, no complications.

My class looks like this:

public class NotAttribute : ValidationAttribute, IClientValidatable
{
    public Type PropertyType { get; set; }
    public string NotEqualTo { get; private set; }
    public bool IgnoreWhitespace { get; set; }
    public bool CaseInsensitive { get; set; }

    public NotAttribute(string notEqualTo)
    {
        NotEqualTo = notEqualTo;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (PropertyType == null) PropertyType = typeof(string);
        if (PropertyType == typeof(string))
        {
            //this is the easy case.  We can compare easy enough
            var leftSide = value.ToStringOrEmpty();
            var rightSide = NotEqualTo ?? string.Empty;

            if (IgnoreWhitespace)
            {
                leftSide = leftSide.Trim();
                rightSide = rightSide.Trim();
            }

            if (CaseInsensitive)
            {
                leftSide = leftSide.ToUpperInvariant();
                rightSide = rightSide.ToUpperInvariant();
            }

            if (leftSide != rightSide) return null; // all is well
        }

        var tryParseMethod = PropertyType.GetMethod("TryParse",
            Reflection.BindingFlags.Static | Reflection.BindingFlags.Public, null,
            new Type[] {typeof(string), PropertyType.MakeByRefType()}, null);

        //This is where I get lost


        return base.IsValid(value, validationContext);
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        throw new NotImplementedException();
    }
}

I’m pretty sure my tryParseMethod will return a reference to the object method that I could theoretically invoke, but here’s where I run into problems.

How would I invoke it, passing in a type determined at runtime? This seems to be screaming for a generics implementation, but I have no idea where I could inject that here. Also, once I have the value I need to compare against the “not” value, I need to somehow cast it at runtime, which also screams for generics.

How can I make this work? Am I doing this completely the wrong way?

  • 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-06T09:31:56+00:00Added an answer on June 6, 2026 at 9:31 am

    It’s a static method, so you should just be able to invoke it without needing an instance of a class of the type PropertyType to invoke on (See the MSDN page). So, you can just call Invoke on tryParseMethod.

    However, the TryParse method itself will need an instance of a class of the type PropertyType as it’s out parameter though, so you’ll need to use System.Activator.CreateInstance() for that.

    So, presumably something like

    var parsedObject = System.Activator.CreateInstance(PropertyType);
    bool parseSuceeded = tryParseMethod.Invoke(null, new [] { value, parsedObject });
    

    will do the job.

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

Sidebar

Related Questions

Trying to write a chat, like on facebook, I wondered if two clients can
Trying to write this small program to help me in my Stats class, everything
Trying to write .ply parser to use .ply models in OpenGL. Trying to begin
I referred to this article http://www.codeproject.com/KB/security/DotNetCrypto.aspx and I am trying write an encrypted string
As part of an HTTP WebServer project for academic purposes, I'm trying write my
Trying to write a GLOBAL CBT HOOK, this is My code, but my hooking
Trying to write a Lua script for Scite (something like lua-users wiki: Scite Comment
Im trying to write an algorithm using the algorithm package, but when I use
I'm trying write a script that detects whether a div is visible or not
Trying to write a conditional with jQuery that basically states, if div.gathering does not

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.