属性 存在于调试器中但在编译时不存在

Property exists in debugger but not when compiling

Context:我需要检查 window 在显示之前是否已被处理(如果有其他代码调用 App.Shutdown)。 How do you tell if a WPF Window is closed?中给出的解决方案是行不通的,因为当时IsLoaded仍然是false。

让我不解的地方:
在尝试显示 WPF window 之前设置断点时,我可以在 window 的基础上访问 IsDisposed 属性,window.IsDisposed 也可以。但是,当我尝试在代码中使用 window.IsDisposed 时,它无法编译。

调试器的屏幕截图:

错误信息:

'Stw.Tools.Zugriffsrechteantrag.Windows.UserWindow' does not contain a definition for 'IsDisposed' and no extension method 'IsDisposed' accepting a first argument of type 'Stw.Tools.Zugriffsrechteantrag.Windows.UserWindow' could be found (are you missing a using directive or an assembly reference?).

我尝试添加对 System.Windows.Forms 的引用,因为表单 window 包含 IsDisposed 属性 但这并没有使代码编译成功。

问题: 为什么 public 属性 在调试器中可以访问但在代码中不能访问?

Question: how can it be that a Property is accessible in the debugger but not in the code?

因为调试器基本上可以显示您的代码无权访问的私有成员、内部成员和受保护成员。您可以自己查看 - 只需声明一个私有字段,您就会在调试器中看到该字段,但即使在同一项目中,您也无法从其他 类 访问它。

在这种情况下,属性 是 internal,如下所示:

using System;
using System.Reflection;

class Test
{
    static void Main()
    {
        var type = typeof(System.Windows.Window);
        var property = type.GetProperty("IsDisposed", BindingFlags.Instance | BindingFlags.NonPublic);
        foreach (var accessor in property.GetAccessors(nonPublic: true))
        {
            Console.WriteLine($"{accessor.Name}: {accessor.Attributes}");
        }
    }
}

输出:

get_IsDisposed: PrivateScope, Assembly, HideBySig, SpecialName

我不确定为什么调试器将它显示给您,就好像它是 public - 当我创建一个 Window 实例时,我将其视为内部实例,如下所示: