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 ?