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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:49:43+00:00 2026-06-10T09:49:43+00:00

I’m making an object validation framework in my spare time to learn a few

  • 0

I’m making an object validation framework in my spare time to learn a few things and maybe use it for some school projects.

I have my generic Rule class, which looks something like this :

class Rule<T>
{
    string propertyName;
    Func<T, bool> ruleLambda;

    bool IsBroken(T value)
    {
        return ruleLambda(value);
    }
}

An object that would be validated would look a bit like this :

class Example
{
    List<Rule<?>> MyRules; // can take all types of rules

    List<Rule<T>> Validate<T>(string propertyName, T value)
    {
        List<Rule<T>> brokenRules = new List<Rule<T>>();
        foreach (Rule rule in MyRules.Where(r => r.propertyName == propertyName))
        {
            if (rule.IsBroken(value))
                brokenRules.Add(rule);
        }
        return brokenRules;
    }
}

Where the T value argument would be the value of one of the Example class’s properties, which can be of any type.

The Validate<T> method is called whenever a property is set.

The problem lies with the class’s list of rules. Specifically the List<Rule<?>> line above. I want to store all the rules for a given class in the same list.

Alas, C# doesn’t have a wildcard for generic types like in Java.

How should I do this?

A non-generic interface or base class utilizing objects instead of T could work, but how would I call the generic Rule’s IsBroken method and not the non-generic one?

  • 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-10T09:49:45+00:00Added an answer on June 10, 2026 at 9:49 am

    I’ve tried a few things and I’ve found something that works pretty well for my needs. I have Rule<T> inherit from a base abstract rule class, with a generic IsBroken method:

    abstract class Rule
    {
        string propertyName;
        Func<object, bool> objectRule;
    
        bool IsBroken<T>(T value)
        {
            Rule<T> rule = this as Rule<T>;
            if (rule == null)
                return objectRule(value);
    
            return rule.IsBroken(value);
        }
    }
    

    As you can see, I try to convert the base class to its generic counterpart using the generic type parameter in the IsBroken method.

    Also, when creating a Rule<T> instance, I send a Func<object, bool> to its base class protected constructor:

    public Rule(string propertyName, Func<T, bool> ruleLambda)
        : base(propertyName, ConvertToObjectFunc(ruleLambda))
    {
    }
    

    With the conversion method looking like this:

    static Func<object, bool> ConvertToObjectFunc(Func<T, bool> func)
    {
        return new Func<object, bool>(o => func((T)o));
    }
    

    However, if it can’t cast o to type T, it crashes. So I wrote this… thing:

    static Func<object, bool> ConvertToObjectFunc(Func<T, bool> func)
    {
        return new Func<object, bool>
        (
            o =>
            {
                try
                {
                    T obj = (T)o;
                    return func(obj);
                }
                catch { return true; } // rule is broken by default
            }
        );
    }
    

    It’s pretty ugly, but it works. Hope this can help anybody else.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm making a simple page using Google Maps API 3. My first. One marker

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.