Rider debugger: type of variable
I have this test code:
using System.Management.Automation;
namespace PSTestApp;
class Program
{
static void Main()
{
using (var powerShell = PowerShell.Create())
{
powerShell.AddScript("$PSVersionTable");
var result = powerShell.Invoke();
Console.WriteLine();
}
}
}When I set a breakpoint on the ConsoleWriteLine line, and run debug, the debugger shows:

I'm interested in BaseObject 'cause it seems to contain the output of the PowerShell script.
From what the debugger shows, I'd say that this member is of type PSVersionHashTable. Unfortunately (and I was told so by someone else, too), it is indeed of type Object (or PSObject?). That's why it has no indexer. The notation in the debugger, to me, indicates that there is an indexer, but for type PSVersionHashTable, which is correct (and reflected in the debugger).
So, what I cannot do is:
ConsoleWriteLine((result[0].BaseObject)["OS"]);Instead, I have to use a cast, which is correct 'cause the type of BaseObject is not PSVersionHashTable:
ConsoleWriteLine(((PSVersionHashTable)result[0].BaseObject)["OS"]);I want to understand what the debugger shows, and how I need to interpret this. What does:
BaseObject = {PSVersionHashTable}mean, when BaseObject is not of type PSVersionHashTable?
Please sign in to leave a comment.
Hello,
Thanks for contacting Rider support.
What you've observed is expected. A variable can have different compile-time and run-time types. The debugger always displays the runtime type. In this case, run-time type is PSVersionHashTable , while complile-time type is Object. Similarly, in the following sample, Compile-time type is Object, and Runtime type is Dog.
Object dog = new Dog();You can get the runtime type via GetType method.
Reference
Compile-time type and run-time type
Thanks,
Tom