Maslow
Maslow

Reputation: 18746

How do you get the value dynamically of a static class' properties?

I want to loop over a static class's properties. [EnvDTE.Constants] | get-member -static | where-object {$_.MemberType -eq "Property" -and $_.Name -like 'vsP*'}

Instead of then going and manually typing the names like: [EnvDTE.Constants]::vsProjectItemKindMisc

tried:

Upvotes: 2

Views: 618

Answers (3)

BartekB
BartekB

Reputation: 8650

First of all, filter left (here it probably does not change much, but its good habit):

[EnvDTE.Constants] | Get-Member -Static -MemberType Property -Name vsP*

One you have MemberDefinition objects:

| Foreach-Object { [EnvDTE.Constants]::"$($_.Name)" }

Your last attempt would work, if you would use subexpression there (though I recommend against it, Invoke-Expression should be used only if really necessary).

Upvotes: 3

Keith Hill
Keith Hill

Reputation: 201782

You can go the .NET BCL route:

[EnvDTE.Constants].UnderlyingSystemType.GetFields('Static,Public').Where({$_.Name -match 'vsP*'}).Foreach({$_.Name + " = " + $_.GetValue($null)})

Or perhaps a bit more PowerShelly:

[EnvDTE.Constants].UnderlyingSystemType.GetFields('Static,Public') | 
    Where Name -match vsP* | Foreach {$_.Name + " = " + $_.GetValue($null)}

Or:

[EnvDTE.Constants] | gm -static -MemberType Property -Name vsP* | 
    Foreach { invoke-expression "'$($_.Name) = ' + [$($_.TypeName)]::$($_.Name)" }

Upvotes: 1

Tim Ferrill
Tim Ferrill

Reputation: 1674

The first example you gave is correct, but doesn't appear to work for that namespace. Both of these work:

[system.math] | Get-Member
[system.net.webrequest] | Get-Member

If you have the dll file you can load it manually.

Upvotes: 0

Related Questions