简体   繁体   English

“is”表达式的结果在运行时返回false,但在检查时为true

[英]Result of “is” expression returns false when run, but true when inspected

I have the following code. 我有以下代码。 The CustomControlHelper generates an instance of an object via reflection. CustomControlHelper通过反射生成对象的实例。 At this stage we don't know what type of object we are dealing with. 在这个阶段,我们不知道我们正在处理什么类型的对象。 We do know it will be a CustomControl , but we don't know if it implements any particular interface or if it extends any other classes. 我们知道它将是一个CustomControl ,但我们不知道它是否实现了任何特定的接口,或者它是否扩展了任何其他类。 The following code is trying to establish whether the loaded control implements the IRichAdminCustomControl interface. 以下代码尝试确定加载的控件是否实现IRichAdminCustomControl接口。

Object obj = CustomControlHelper.GetControl(cc.Id, cc.ControlClass);            
if(obj != null)
{
    bool isWhatWeWant = (obj is IRichAdminCustomControl);
    return isWhatWeWant;
}

That's all fine, but I've noticed that when I know I have an object that implements IRichAdminCustomControl , the expression evaluates to false. 这一切都很好,但我注意到,当我知道我有一个实现IRichAdminCustomControl的对象时,表达式的计算结果为false。

Okay, this is where it gets really weird. 好吧,这就是它变得非常奇怪的地方。 If I inspect the code when debugging, the expression evaluates to true, but then if I immediately let the code run and inspect the result, it evaluates to false (I've attached an animated gif below to illustrate). 如果我在调试时检查代码,表达式的计算结果为true,但如果我立即让代码运行并检查结果,则计算结果为false(我在下面附上了一个动画gif来说明)。

在此输入图像描述

Has anyone come across weirdness like this before and if so, what on earth is causing it? 有没有人像以前那样遇到过这样的奇怪现象,如果有的话,究竟是什么导致了它呢?

Incidentally, I believe the product I'm using uses Spring.NET to provide dependency injection in the CustomControlHelper. 顺便说一句,我相信我使用的产品使用Spring.NET在CustomControlHelper中提供依赖注入。

If you are using Visual Studio 2010 SP1, I came across this bug: 如果您使用的是Visual Studio 2010 SP1,我遇到了这个错误:

Misreporting of variable values when debugging x64 code 调试x64代码时误报变量值

There is a workaround on that page, posted by Microsoft: 该页面上有一个解决方法,由Microsoft发布:

You can either set all projects to compile to x86, or create an intermediate initialised variable declaration to ensure the debugger reports the correct value of the variable being examined. 您可以将所有项目设置为编译为x86,也可以创建中间初始化变量声明以确保调试器报告正在检查的变量的正确值。

Try this as a workaround: 试试这个解决方法:

bool isWhatWeWant = true;
isWhatWeWant &= (obj is IRichAdminCustomControl);
bool finalValue = isWhatWeWant; // this line should fix isWhatWeWant too in the debugger
return finalValue;

EDIT: seems like VS2012 also encounters similar problems in specific conditions. 编辑:似乎VS2012在特定条件下也遇到类似的问题。

This happened to me once and even though I never came to a conclusion as to why it was happening I believed the PDB files that were being loaded with the debugging symbols where out of sync. 这件事发生在我身上,即使我从来没有得出结论为什么会发生这种情况我相信PDB文件正在加载调试符号不同步。 So, by "cleaning" the solution and then rebuilding the solution this weird issue went away. 因此,通过“清理”解决方案,然后重建解决方案,这个奇怪的问题就消失了。

Two possibilities come to mind. 想到两种可能性。 The first is that your interface name is generic enough that it could already be in the namespace somewhere. 第一个是你的接口名称足够通用,它可能已经在某个名称空间中。 Try fully qualifying the interface in the is clause. 尝试完全限定is子句中的接口。 The second possibility is that you might be running the code as part of a constructor, or being called indirectly by a constructor. 第二种可能性是您可能将代码作为构造函数的一部分运行,或者由构造函数间接调用。 Any reflection like stuff needs to be done after we are certain the application has fully loaded. 在我们确定应用程序已完全加载之后,需要完成任何类似的反射。

So I found the answer. 所以我找到了答案。 It was because I had two copies of the dll in different locations. 这是因为我在不同的位置有两个dll副本。 I had one copy in the bin of my back-end application and one in a shared external directory that gets dynamically loaded by the backend app. 我在后端应用程序的bin中有一个副本,在后端应用程序动态加载的共享外部目录中有一个副本。

I should explain; 我应该解释一下; this application consists of two apps running in tandem, a frontend app and a backend app. 此应用程序包含两个串联运行的应用程序,一个前端应用程序和一个后端应用程序。 Ordinarily, you place "Custom Controls" into your frontend app. 通常,您将“自定义控件”放入您的前端应用程序。 These controls are then copied on application start to a external directory that is accessible to the backend app. 然后将这些控件在应用程序启动时复制到后端应用程序可访问的外部目录。

In this case, I had logic in my Custom Control library that needed to be accessed in the backend app - so I had to make a reference to it... which ended up with the backend app having two references to the same class. 在这种情况下,我的自定义控件库中的逻辑需要在后端应用程序中访问 - 所以我不得不引用它...最终后端应用程序有两个引用同一个类。 D'oh! D'哦! And OF COURSE that's going to work when you're debugging. 当你正在调试时,那些可以正常工作的课程。

Solution was to split my extra logic into its own project and reference THAT in the backend app. 解决方案是将我的额外逻辑拆分为自己的项目,并在后端应用程序中引用它。

I'm not going to "accept" my own answer here because, although it solved my specific problem, the solution is a bit TOO specific to the apps I'm working with and would be unlikely to help anyone else. 我不会在这里“接受”我自己的答案,因为虽然它解决了我的具体问题,但解决方案对于我正在使用的应用程序来说有点过于特定,并且不太可能帮助其他任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使if语句为true,函数也会返回false - Functions returns false even when the if statement is true 布尔比较在应为true时返回false - Boolean comparision returns false when it should be true 当它应该为真时,LINQ Query返回false - LINQ Query returns false when it should be true IsAssignableFrom() 在应该返回 true 时返回 false - IsAssignableFrom() returns false when it should return true 当语句应该为真时,Roslyn CSharpScript EvaluateAsync 返回 False - Roslyn CSharpScript EvaluateAsync Returns False When Statement Should Be True 当= =为空值返回true时,为什么> =返回false? - Why does >= return false when == returns true for null values? 为什么当每个单独的属性都返回 true 时,整个 model 的比较返回 false - Why do9es comparison of entire model returns false when each Individual properties returns true 当ping.Result.Options.DontFragment发送为true时为false时是否正确? - Is it correct when ping.Result.Options.DontFragment gives false when sent true? 使用AS投放广告时,“表情总是假的” - “Expression is always false” when casting with AS ASP.NET @Html.DropDownListFor() 选择值设置为 true 但提交到 controller 时返回 false - ASP.NET @Html.DropDownListFor() selected value is set to true but returns false when submitted to the controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM