Comments
2 comments
-
Hi.
I don't think that the generated code is faulty. Both pieces of code return the result of ConnectionString, and both pieces of code only take the lock if the compare result is >= 0. The difference is simply that Reflector isn't generating a body that has an embedded return statement. -
Additionally, if you have the optimizer turned on while compiling (on by default) then it may change your code to that that was disassembled.
Add comment
Please sign in to leave a comment.
Disassemble of the following class:
internal static class ConfigProvider { private static DateTime UpdatedTime { get; set; } private static string ConnectionString { get; set; } private static object _syncRoot = new object(); internal static string GetConnectionString() { // within valid cache time if (DateTime.Compare(DateTime.Now, UpdatedTime.AddMinutes(1)) < 0) return ConnectionString; // refresh data lock (_syncRoot) { } return ConnectionString; } }Gives the following disassemble faulty code:// Methods internal static string GetConnectionString() { if (DateTime.Compare(DateTime.Now, UpdatedTime.AddMinutes(1.0)) >= 0) { lock (_syncRoot) { } } return ConnectionString; }Notice how the DateTime.Compare test is disassembled!!
If I remove the lock statement the disassembled code is correct so seems to be connected to Montor.Enter/Exit statements in IL code.
Jonny Bekkum, Norway