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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:05:31+00:00 2026-06-15T02:05:31+00:00

I want to create a generic extension method that will set a value to

  • 0

I want to create a generic extension method that will set a value to an Object or a Struct if their value equals to their default value.

so i have the following code:

public static void setIfNull<T>(this T i_ObjectToUpdate, T i_DefaultValue)
{
    if (EqualityComparer<T>.Default.Equals(i_ObjectToUpdate, default(T)))
    {
        i_ObjectToUpdate = i_DefaultValue;
    }
}

and here is a call example:

public OrganizationalUnit CreateOrganizationalUnit(OrganizationalUnit i_UnitToCreate)
{
    i_UnitToCreate.EntityCreationDate.setIfNull(DateTime.Now); //Here is a call
    i_UnitToCreate.EntityLastUpdateDate.setIfNull(DateTime.Now); //And another one
    m_Context.DomainEntities.Add(i_UnitToCreate);
    return i_UnitToCreate;
}

I don’t know if it have anything to do with it but i use entity framework and MVC.

What actually happens in a debugger I see that the line in the extension method i_ObjectToUpdate = i_DefaultValue; is working and the values are changes but when the debugger gets out of the extension method I see that the value of i_UnitToCreate.EntityCreationDate remains unchaged.

Any ideas what went wrong ?

  • 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-15T02:05:33+00:00Added an answer on June 15, 2026 at 2:05 am

    You have two references in your code. One is i_UnitToCreate.EntityCreationDate which points to some address in memory. And other is i_ObjectToUpdate in your extension method, which initially also points to that address (you are creating copy of address when passing i_UnitToCreate.EntityCreationDate reference to your method). Later you change second reference to point on other object in memory, but that does not change first reference, because they are independent.

    Workaround

    public static void SetIfDefault<T, TProperty>(this T arg,
        Expression<Func<T, TProperty>> propertySelector, TProperty value)
    {
        TProperty currentValue = propertySelector.Compile()(arg);
        EqualityComparer<TProperty> comparer = EqualityComparer<TProperty>.Default;
        if (!comparer.Equals(currentValue, default(TProperty)))
            return;
        PropertyInfo property = (PropertyInfo)((MemberExpression)propertySelector.Body).Member;
        property.SetValue(arg, value);
    } 
    

    You can use expression to pass property selector (not property value) to extension method. Retrieve value from compiled expression. If it is default value, then with reflection (you can easily get PropertyInfo by casting member expression) set new value. Usage:

    i_UnitToCreate.SetIfDefault(x => x.EntityCreationDate, DateTime.Now);
    i_UnitToCreate.SetIfDefault(x => x.EntityLastUpdateDate, DateTime.Now);
    

    PS There is one remark about my first answer – I thought about generic T type as a reference type, when talked about copying reference values. With value types (as DateTime) whole object is copied. It does not change result (you can’t assign new value), but needs to be mentioned.

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

Sidebar

Related Questions

I have created a Generic Extension method for DataRow object. The method takes no
I have a list that contains FrameworkElements and I want to create an extension
I am planning to create generic commenting module, that will work for any object
I'm trying to create a generic extension method, that works on typed data tables
I want to create a generic method to serizlize a class to text (for
I want to create a generic html table to excel file view that can
I want create a generic (general) method which accepts the return types of LINQ
I want to create an extension method of class IEnumerable and create a method
I need a way to create an extension method off of an IEnumerable that
I want to create a generic IEnumerable implementation, to make it easier to wrap

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.