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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:13:22+00:00 2026-06-11T20:13:22+00:00

I was looking at another question that stated how Expression can be significantly faster

  • 0

I was looking at another question that stated how Expression can be significantly faster than reflection since it can be precompiled to IL.

I’m not really sure how to use it though. Here is some code used in a base class for a Value Oject (in the DDD sense) where the basic idea is to use the values of all public properties to determine equality, which it gets via reflection. By using this base class, you needn’t implement equality for subclasses that have Value Object.

protected virtual bool HasSameObjectSignatureAs(BaseObject compareTo) 
{
var signatureProperties = GetType().GetProperties();
foreach (var property in signatureProperties)
{
    var valueOfThisObject = property.GetValue(this, null);
    var valueOfCompareTo = property.GetValue(compareTo, null);

    if (valueOfThisObject == null && valueOfCompareTo == null) {
        continue;
    }

    if ((valueOfThisObject == null ^ valueOfCompareTo == null) ||
        (!valueOfThisObject.Equals(valueOfCompareTo))) {
            return false;
    }
}

How would this code be re-written using Expression?

Cheers,
Berryl

  • 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-11T20:13:24+00:00Added an answer on June 11, 2026 at 8:13 pm

    You can do this by building up a nested And expression for each property you want to compare:

    protected Expression<Func<BaseObject, bool>> CreatePropertiesEqualExpression(BaseObject other)
    {
        if (! other.GetType().IsSubclassOf(this.GetType())) throw new ArgumentException();
    
        var properties = this.GetType().GetProperties();
        Expression trueExpr = Expression.Constant(true);
        Expression thisExpr = Expression.Constant(this);
        ParameterExpression paramExpr = Expression.Parameter(typeof(BaseObject), "compareTo");
        Expression downCastExpr = Expression.Convert(paramExpr, other.GetType());
    
        MethodInfo eqMethod = typeof(object).GetMethod("Equals", BindingFlags.Public | BindingFlags.Static);
    
        Expression propCompExpr = properties.Aggregate(trueExpr, (expr, prop) =>
        {
            Expression thisPropExpr = Expression.Property(thisExpr, prop);
            Expression compPropExpr = Expression.Property(downCastExpr, prop);
            Expression eqExpr = Expression.Call(null, eqMethod, Expression.Convert(thisPropExpr, typeof(object)), Expression.Convert(compPropExpr, typeof(object)));
    
            return Expression.And(expr, eqExpr);
        });
    
        return Expression.Lambda<Func<BaseObject, bool>>(propCompExpr, paramExpr);
    }
    

    You can then use it like this:

    public class SubObject : BaseObject
    {
        public int Id { get; set; }
        public string Name { get; set; }
        private Func<BaseObject, bool> eqFunc;
    
        public bool IsEqualTo(SubObject other)
        {
            if(this.eqFunc == null)
            {
                var compExpr = this.CreatePropertiesEqualExpression(other);
                this.eqFunc = compExpr.Compile();
            }
            return this.eqFunc(other);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is related to another Delphi-version question but still different; I'm looking for a
I was looking to implement a python script that called another script and captured
Yes another question that is a little Unique because I cant find the same
EDIT I realised my question was not stated clearly enough and have edited it
I am looking for another book beside Head First Servlets and JSP . The
I'm looking for another way of doing the following: function call_any_function(func, parameters){ // func
Looking to produce some effects using particles bleeding into one another and slowly fading
I'm looking to copy a row from one sheet to another based upon having
I am looking for the regex to stet if a string starts with another
I'm looking for a clean way to import one git repository from another, where

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.