Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active March 11, 2017 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/5fc3ea1962e5ee5ccf5bc29e52d5bb21 to your computer and use it in GitHub Desktop.
Save stknohg/5fc3ea1962e5ee5ccf5bc29e52d5bb21 to your computer and use it in GitHub Desktop.
PowerShellのClassのFieldとPropertyの扱いに関するメモ
#
# PowerShell Class "Field" is converted to Property.
#
class PSClass
{
[string]$Field1 = "F1"
}
Write-Output "Get Field information from [PSClass].GetMembers() -> returns Property"
[PSClass].GetMembers() | ? { $_.Name -eq "Field1" } | select Membertype | Out-String
Write-Output "Get Field information from [PSClass] | Get-Members -> returns Property"
$y = [PSClass]::new()
$y | Get-Member | ? { $_.Name -eq "Field1" } | select Membertype | Out-String
#
# Added C# Class Field is not converted to Property, but Get-Members returns "Property".
#
$code = @"
public sealed class CSClass
{
public string Field1 = "F1";
}
"@
Add-Type -TypeDefinition $code
Write-Output "Get Field information from [CSClass].GetMembers() -> returns Field"
[CSClass].GetMembers() | ? { $_.Name -eq "Field1" } | select Membertype | Out-String
Write-Output "Get Field information from [CSClass] | Get-Members -> returns Property"
$t = [CSClass]::new()
$t | Get-Member | ? { $_.Name -eq "Field1" } | select Membertype | Out-String
@stknohg
Copy link
Author

stknohg commented Mar 11, 2017

PowerShell 5.1な環境での結果。

Get Field information from [PSClass].GetMembers() -> returns Property

MemberType
----------
  Property



Get Field information from [PSClass] | Get-Members -> returns Property

MemberType
----------
  Property



Get Field information from [CSClass].GetMembers() -> returns Field

MemberType
----------
     Field



Get Field information from [CSClass] | Get-Members -> returns Property

MemberType
----------
  Property

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment