[pwsh] Two ways to view variable types

[pwsh] Two ways to view variable types

It is not necessary to display the declared variable type in powershell. This assignment method is more convenient, but it also causes some problems of unclear types. Knowing how to view variable types can help us learn faster and easily obtain the inheritance chain of types.



GetType method 1

GetTypeThe method is an additional method that all variables in pwsh have. By learning to write *.type.xmlfiles or Add-Membercommands, we can even customize the extension method of the specified type.

$YouObject.GetType()                    # 变量YouObject类型的全表
$YouObject.GetType().BaseType           # 全表中BaseType列的次级列表
$YouObject.GetType().Name               # 全表中Name列的内容
$YouObject.GetType().FullName           # 全表中Name列全名
$YouObject.GetType().BaseType.Name      # 次级列表中Name列的内容
$YouObject.GetType().BaseType.FullName  # 次级列表中Name列的全名
$YouObject.GetType().BaseType.BaseType  # 次级列表中BaseType列的内容

PSTypeNames attribute 2

Compared with GetTypethe method, the pstypenames attribute can visually see the inheritance chain of the object, which will have a greater heuristic effect when touching a new type that has not been learned.

$YouObject.pstypenames # 变量类型继承链

reference


  1. Types and Strong Types of Powershell Variables ↩︎

  2. Powershell equivalent of C# "is" operator? ↩︎

Guess you like

Origin blog.csdn.net/qq_41755979/article/details/91419190