I have some data that I get from a data source, which is a group of name / value pairs that I store in the <string, object> dictionary.
I want to define a class on the fly with properties that map to key / value pairs from the dictionary and methods based on the type of data it represents. This would allow the cmdlet user to access the values ββas properties of the object and also call methods on it.
I see an example of this with Get-WmiObject. It returns instances of ManagementObject (which is basically the total amount of properties), but the user can access the properties and call methods on it directly (i.e., without calling GetPropertyValue / InvokeMethod methods in ManagementObject).
PS C:\temp> $comp = Get-WmiObject Win32_ComputerSystem PS C:\temp> $comp | Get-Member TypeName: System.Management.ManagementObject
How to do this with my own objects?
UPDATE
Accepting Keith's answer, which is a common .NET Framework approach for dynamically generating code. This should work for my scenario, although I think it might be redundant.
I was hoping someone would provide a vivid example of this using the tools provided by PowerShell. There seems to be a way to dynamically create a class by extending the PSObject , PSProperty, and PSMethod described in the Powershell SDK .
Unfortunately, the documentation around this seems rather poor with a lot of ridiculous statements like "Although this class can be derived from this class, there is no established script for this, and any attempt to do this may lead to unexpected behavior."
What's even worse is that all the MSDN links explaining the PowerShell extended type system seem bad! And the only examples I've seen on the Internet is to do this from a PowerShell script, and not for people developing cmdlets using the C # and SDK.
Hi, is anyone on the PowerShell team listening?