The only thing I can think of is a race condition but both the calling function and the line of code are synchronous to my knowledge.
/// <summary>
/// Gets the log format string for an info-level log.
/// </summary>
public static string Info<T>(string action, T obj)
{
var stringBuilder = new StringBuilder(String.Format(
"Action: {0} \tObject: {1} \tUser: {2} \tJson: ",
action, typeof(T).Name, User
));
// Set all virtual properties to null. This should stop circular references of navigation properties.
var virtualProperties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(x => x.GetSetMethod().IsVirtual && !x.PropertyType.IsPrimitive);
foreach (var propInfo in virtualProperties)
{
propInfo.SetValue(obj, null); // This Line is the culprit.
}
GetJsonSerializer().Serialize(obj, stringBuilder);
return stringBuilder.ToString();
}
The line propInfo.SetValue(obj, null)
will execute if I just breakpoint before the loop and step through one-by-one (or just break on that line) however if I don't use a breakpoint it never sets the property(ies) to null. Why is this?
Specific Details:
- If I don't use a breakpoint it doesn't work.
- If I place a breakpoint at the top of the foreach and hit f5 it doesn't work.
- If I place a breakpoint at the top of the foreach and step-through via f10 it does work.
- If I place a breakpoint on the line of code
propInfo.SetValue(obj, null);
it does work. - A breakpoint after the loop still shows the values as not null.
- If I change
null
to5
(which isn't a valid value) it throws an exception telling my that it's not a valid value.
To clarify, "Doesn't work" means that it doesn't set the property to null.
What I've tried:
- Restarting Visual Studio (2013)
- Changing the line of code (Used to be
default(T)
) - Project Properties -> Build -> Optimize Code (initially off)
EDIT
It's been narrowed down that EF Navigation properties are the cause of this behavior. The code is running but for some reason the navigation properties refuse to become null. So what about navigation properties are causing this behavior?
T
types? Maybe it's possible that entity framework isn't "accepting" the property change and we might be able to pin down that reason.