为什么 if 语句的两边似乎都在执行?
Why would both sides of an if statement appear to execute?
我发现了一个 if 语句的两边都执行的场景。为什么?
我认为这在 C# 中是不可能的。
问题第二次出现我运行 XAF EF Code First 解决方案。在我删除最初创建的 .mdf 和 _log.ldf 文件后。
当我单步执行代码时,!database.Exists 语句的计算结果为真。
然后当我按F11时(在建立数据库后的代码片段中注释的点不存在)我在调试器中看到的下一步是在catch语句中.
代码片段来自 Dev Express .pdb 文件,不幸的是,catch 语句没有提供检查错误消息的方法。
protected virtual DatabaseSchemaState CheckDatabaseSchemaCompatibilityCore(Object context)
{
DatabaseSchemaState result = DatabaseSchemaState.SchemaExists;
if(context is DbContext)
{
Database database = ((DbContext)context).Database;
if(!database.Exists())
{
// If I press F11 here
result = DatabaseSchemaState.DatabaseMissing;
}
else
{
try
{
if (!database.CompatibleWithModel(false))
{
result = DatabaseSchemaState.SchemaRequiresUpdate;
}
}
catch
{
// the next step is here
result = DatabaseSchemaState.SchemaRequiresUpdate;
}
}
}
// etc
}
线程 window 显示两个步骤都在同一个线程上。
当我在第 318 行按 F11 时,几秒钟后我看到突出显示的下一步是第 327 行
异常时的调用栈是
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.CheckDatabaseSchemaCompatibilityCore(object context) Line 327 C#
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.CreateObjectContext(System.Collections.Generic.IList<System.IDisposable> disposableObjects) Line 182 + 0xc bytes C#
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.Init(System.Type contextType, DevExpress.ExpressApp.DC.ITypesInfo typesInfo, DevExpress.ExpressApp.EF.EFTypeInfoSource typeInfoSource, System.Data.Common.DbConnection connection, string connectionString, string metadataLocations, string providerName) Line 116 + 0xd bytes C#
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.EFObjectSpaceProvider(System.Type contextType, DevExpress.ExpressApp.DC.ITypesInfo typesInfo, DevExpress.ExpressApp.EF.EFTypeInfoSource typeInfoSource, string connectionString, string metadataLocations, string providerName) Line 209 + 0x15 bytes C#
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.EFObjectSpaceProvider(System.Type contextType, DevExpress.ExpressApp.DC.ITypesInfo typesInfo, DevExpress.ExpressApp.EF.EFTypeInfoSource typeInfoSource, string connectionString) Line 215 + 0x17 bytes C#
DXApplicationkg1.Win.exe!DXApplicationkg1.Win.DXApplicationkg1WindowsFormsApplication.CreateDefaultObjectSpaceProvider(DevExpress.ExpressApp.CreateCustomObjectSpaceProviderEventArgs args) Line 35 + 0x66 bytes C#
DevExpress.ExpressApp.v18.2.dll!DevExpress.ExpressApp.XafApplication.CreateObjectSpaceProviders(string connectionString) Line 294 + 0xc bytes C#
DevExpress.ExpressApp.v18.2.dll!DevExpress.ExpressApp.XafApplication.Setup() Line 1543 + 0x1e bytes C#
DXApplicationkg1.Win.exe!DXApplicationkg1.Win.Program.Main() Line 45 + 0xa bytes C#
未选中构建选项卡上的“优化代码”按钮。
代码来自 DevExpress .pdb 文件
[更新]
启用中断异常后我可以看到
提示用户登录失败
+ $exception {System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: Cannot open database "DXApplicationkg1" requested by the login. The login failed.
Login failed for user 'MYCOMPUTER\kirst'.
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.EntityClient.EntityConnection.<Open>b__2()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
--- End of inner exception stack trace ---
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()} System.Exception {System.Data.Entity.Core.EntityException}
[更新]
卡罗尔评论了
If both "sides" of IF statement are executed, it means that you
executed method twice.
但是,如果我在第 314 行打断,它只会到达一次。
我应该说我是 运行ning Windows 10 1809
我在 Dev Express
上问了一个相关问题
The docs explain here 检查调试选项中的 'Suppress JIT optimization on module load (Managed only)' 选项。
如果在调试器中执行了 if 语句,我只执行 else 部分。
我发现了一个 if 语句的两边都执行的场景。为什么?
我认为这在 C# 中是不可能的。
问题第二次出现我运行 XAF EF Code First 解决方案。在我删除最初创建的 .mdf 和 _log.ldf 文件后。
当我单步执行代码时,!database.Exists 语句的计算结果为真。
然后当我按F11时(在建立数据库后的代码片段中注释的点不存在)我在调试器中看到的下一步是在catch语句中.
代码片段来自 Dev Express .pdb 文件,不幸的是,catch 语句没有提供检查错误消息的方法。
protected virtual DatabaseSchemaState CheckDatabaseSchemaCompatibilityCore(Object context)
{
DatabaseSchemaState result = DatabaseSchemaState.SchemaExists;
if(context is DbContext)
{
Database database = ((DbContext)context).Database;
if(!database.Exists())
{
// If I press F11 here
result = DatabaseSchemaState.DatabaseMissing;
}
else
{
try
{
if (!database.CompatibleWithModel(false))
{
result = DatabaseSchemaState.SchemaRequiresUpdate;
}
}
catch
{
// the next step is here
result = DatabaseSchemaState.SchemaRequiresUpdate;
}
}
}
// etc
}
线程 window 显示两个步骤都在同一个线程上。
当我在第 318 行按 F11 时,几秒钟后我看到突出显示的下一步是第 327 行
异常时的调用栈是
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.CheckDatabaseSchemaCompatibilityCore(object context) Line 327 C#
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.CreateObjectContext(System.Collections.Generic.IList<System.IDisposable> disposableObjects) Line 182 + 0xc bytes C#
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.Init(System.Type contextType, DevExpress.ExpressApp.DC.ITypesInfo typesInfo, DevExpress.ExpressApp.EF.EFTypeInfoSource typeInfoSource, System.Data.Common.DbConnection connection, string connectionString, string metadataLocations, string providerName) Line 116 + 0xd bytes C#
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.EFObjectSpaceProvider(System.Type contextType, DevExpress.ExpressApp.DC.ITypesInfo typesInfo, DevExpress.ExpressApp.EF.EFTypeInfoSource typeInfoSource, string connectionString, string metadataLocations, string providerName) Line 209 + 0x15 bytes C#
DevExpress.ExpressApp.EF.v18.2.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.EFObjectSpaceProvider(System.Type contextType, DevExpress.ExpressApp.DC.ITypesInfo typesInfo, DevExpress.ExpressApp.EF.EFTypeInfoSource typeInfoSource, string connectionString) Line 215 + 0x17 bytes C#
DXApplicationkg1.Win.exe!DXApplicationkg1.Win.DXApplicationkg1WindowsFormsApplication.CreateDefaultObjectSpaceProvider(DevExpress.ExpressApp.CreateCustomObjectSpaceProviderEventArgs args) Line 35 + 0x66 bytes C#
DevExpress.ExpressApp.v18.2.dll!DevExpress.ExpressApp.XafApplication.CreateObjectSpaceProviders(string connectionString) Line 294 + 0xc bytes C#
DevExpress.ExpressApp.v18.2.dll!DevExpress.ExpressApp.XafApplication.Setup() Line 1543 + 0x1e bytes C#
DXApplicationkg1.Win.exe!DXApplicationkg1.Win.Program.Main() Line 45 + 0xa bytes C#
未选中构建选项卡上的“优化代码”按钮。
代码来自 DevExpress .pdb 文件
[更新] 启用中断异常后我可以看到
提示用户登录失败
+ $exception {System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: Cannot open database "DXApplicationkg1" requested by the login. The login failed.
Login failed for user 'MYCOMPUTER\kirst'.
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.EntityClient.EntityConnection.<Open>b__2()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
--- End of inner exception stack trace ---
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()} System.Exception {System.Data.Entity.Core.EntityException}
[更新]
卡罗尔评论了
If both "sides" of IF statement are executed, it means that you executed method twice.
但是,如果我在第 314 行打断,它只会到达一次。
我应该说我是 运行ning Windows 10 1809
我在 Dev Express
上问了一个相关问题The docs explain here 检查调试选项中的 'Suppress JIT optimization on module load (Managed only)' 选项。
如果在调试器中执行了 if 语句,我只执行 else 部分。