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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:34:44+00:00 2026-06-11T10:34:44+00:00

I want to write a simple Validate Method like FluentValidation I wrote this simple

  • 0

I want to write a simple Validate Method like FluentValidation

I wrote this simple code:

public static class ExtensionMethods<T> where T : class 
{
    public static bool Validate(Func<T, bool> expression)
    {
        //What I should write?????
    }
}

I don’t know what should I write in method body and how I can use that.


EDIT 1 :

I want to write such this code for all objects:

ExtensionMethods<MyObject>.Validate(o=>o.ID == 1).AddMessage(....
  • 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-11T10:34:45+00:00Added an answer on June 11, 2026 at 10:34 am

    First of all, the key to fluent interfaces is that your methods – Validate, in this case – don’t return bool, but rather return an object that can be called again. So let’s say it’s T here. Additionally, you’re not really using extension method syntax, you’re just defining a static method. And one last thing before we start – you set the generic parameter T in your class definition, but that’s not the right thing to do for static classes, since they are never instantiated with any specific generic parameter. You need to add it to the method signature, not the class signature:

    So here’s your method with those two changes:

    public static class ExtensionMethods
    {
        public static T Validate<T>(this T myObj, Func<T, bool> expression) 
             where T : class
        { 
        }
    }
    

    Now, what you want to do in there, you just call the validation expression. It’s a delegate, so you just call it as if it were a function, passing it the first parameter, your MyObject, as the parameter. If it returns false, I assume the whole statement will throw an exception:

    public static T Validate(this T myObj, Func<T, bool> expression)
    {
        if (expression(myObj))
        {
             return myObj; // Return the object for further processing.
        }
        else
        {
             throw new OhNoValidationFailedException("aaaaaaaaahhh!");
        }
    }
    

    You can now use it like this:

    MyObject myObject = new MyObject();
    myObject.Validate(o => o.MyProperty == 7).Do().Something().Else();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a simple web proxy, for exercise. Here's the code I
I want to write a simple task which will update and commit source code
I was trying to write a simple method: boolean validate(MyObject o) { // propertyA
I want to write a simple program which can customize iphone icons like their
I want to write a simple debug method in Ruby for my Rails app:
I just want too write simple .asm code for TASM that work as for
I want write a simple query which will fetch data from a table (which
I want to write simple script to copy/backup directory then remove on server startup.
I'm new to Java programming. I want to write simple Java network protocol. I
I want to write a simple chat application (for test use). The users and

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.