Context: I need to check if a window was disposed before it is shown (if some other code called App.Shutdown). The solution given in How do you tell if a WPF Window is closed? does not work because IsLoaded
is still false at that time.
What leaves me puzzeled:
When setting a breakpoint before trying to show the WPF window I can access a IsDisposed
Property in the base of the window, window.IsDisposed
works as well. However when I try to use window.IsDisposed
in the code it doesn't compile.
Error message:
'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?).
I tried adding a reference to System.Windows.Forms
because a Forms window contains the IsDisposed
Property but that didn't make the code compile successfully.
Question: how can it be that a public Property is accessible in the debugger but not in the code?
Question: how can it be that a Property is accessible in the debugger but not in the code?
Because the debugger can show private, internal and protected members that your code doesn't have access to, basically. You can see this for yourself - just declare a private field, and you'll see that field in the debugger, but you wouldn't be able to access it from other classes even within the same project.
In this case, the property is internal
, as demonstrated here:
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}");
}
}
}
Output:
get_IsDisposed: PrivateScope, Assembly, HideBySig, SpecialName
I'm not sure why the debugger is showing it to you as if it's public - when I create a Window
instance, I see it as internal, as shown here:
See more on this question at Stackoverflow