title: How to dynamically define a class in C# PowerShell Cmdlet
body: <p>I have some data I'm getting from a datasource which is a bunch of name/value pairs that I store in a Dictionary&lt;string, object&gt;.</p>
  
  <p>I want to define a class on the fly, with properties that map to the key/value pairs from the dictionary and methods based on the type of data it represents.  This would allow the user of the cmdlet to access the values as properties of an object and also invoke methods on it.</p>
  
  <p>I see an example of this with Get-WmiObject.  It returns instances of ManagementObject (which is basically a generic property bag) but the user is able to access the properties and invoke methods on it directly (i.e. without having to call the GetPropertyValue/InvokeMethod methods on ManagementObject).</p>
  
  <pre><code>PS C:\temp&gt; $comp = Get-WmiObject Win32_ComputerSystem
  PS C:\temp&gt; $comp | Get-Member
  
     TypeName: System.Management.ManagementObject#root\cimv2\Win32_ComputerSystem
  
  Name                        MemberType   Definition
  ----                        ----------   ----------
  JoinDomainOrWorkgroup       Method       System.Management.ManagementBaseObject JoinDomainO
  Rename                      Method       System.Management.ManagementBaseObject Rename(Syst
  SetPowerState               Method       System.Management.ManagementBaseObject SetPowerSta
  UnjoinDomainOrWorkgroup     Method       System.Management.ManagementBaseObject UnjoinDomai
  AdminPasswordStatus         Property     System.UInt16 AdminPasswordStatus {get;set;}
  AutomaticManagedPagefile    Property     System.Boolean AutomaticManagedPagefile {get;set;}
  AutomaticResetBootOption    Property     System.Boolean AutomaticResetBootOption {get;set;}
  ... etc ...
  </code></pre>
  
  <p>How do I do this with my own objects?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Accepting Keith's answer which is a general .NET framework approach for generating code dynamically.  This should work for my scenario although I think it might be overkill.</p>
  
  <p>I was hoping someone would provide a clear example of doing this using the facilities provided by PowerShell.  It seems there should be a way to create a class dynamically by  extending the <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.psobject%28v=VS.85%29.aspx" rel="noreferrer">PSObject</a>, <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.psproperty%28v=VS.85%29.aspx" rel="noreferrer">PSProperty</a>, and <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.psmethod%28v=VS.85%29.aspx" rel="noreferrer">PSMethod</a> classes described in the <a href="http://msdn.microsoft.com/en-us/library/ms569889%28v=VS.85%29.aspx" rel="noreferrer">Powershell SDK</a>.</p>
  
  <p>Unfortunately the documentation around this seems pretty poor with a lot of ridiculous statements like "<em>Although it is possible to derive from this class, there is no established scenario for doing this and any attempt to do so may result in unexpected behavior.</em>"</p>
  
  <p>What made it worse is that all the links in MSDN explaining the PowerShell Extended Type System seem to be bad!  And the only examples I've seen on the web is how to do this from a PowerShell script, not for people developing cmdlets using C# and the SDK.</p>
  
  <p>Hello, anyone from the PowerShell team listening?</p>