System.IO.FileInfo 有一个 Target 成员。
使用 Get-Item -Path * -Include 't.txt' | Get-Member
显示它有一个 Target
属于 CodeProperty
的成员.
使用 GetType()
显示它是 List`1
C:>Get-Item -Path * -Include 't.txt' | ForEach-Object { $_.Target.GetType() }
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True List`1 System.Object
C:>Get-Item -Path * -Include 't.txt' | % { $_.Target.GetType() | % { $_.FullName } }
System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
最佳答案
vonPryz在评论中提供了关键指针:List`1
是 .NET 对 generic type 的表示命名为 List
与 arity ( `
) 1
,即具有 1 个类型参数的泛型类型 .
(在此上下文中使用 `
与 PowerShell 使用 `
作为转义字符无关)。
在您的情况下,System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
表示泛型类型以 System.String
类型关闭(实例化) .
撇开装配资格( mscorlib, Version = ...
),等效的 PowerShell 表示是 System.Collections.Generic.List`1[[string]]
,但是可以通过两种方式简化:
`1
, 可以省略,因为 [...]
中的类型参数隐含了元数, [string]
. [...]
围绕类型参数列表。 因此,您可以只使用
System.Collections.Generic.List[string]
,或者,表示为 PowerShell 类型文字 ( [...]
),[System.Collections.Generic.List[string]]
可选阅读:在 PowerShell 中缩短类型名称和文字:
[System.Collections.Generic.List[string]]
有点笨拙,有两种方法可以缩短它:System.
任何类型的命名空间的一部分 , 所以 [Collections.Generic.List[string]]
也可以。using namespace
声明 , 类似于 C# 的 using
陈述: # Note:
# * `using namespace` must be at the *start* of the script (potentially
# preceded by other `using` statements and comments only)
# * The 'System.' part of a namespace must *not* be omitted.
using namespace System.Collections.Generic
[List[string]] # short for: [System.Collections.Generic.List[string]]
此外,PowerShell 有 内置类型加速器 对于某些经常使用的类型,它们是引用特定类型的单组件名称,而无需指定其来源 namespace ;例如,
[xml]
是 [System.Xml.XmlDocument]
的类型加速器.This TechNet blog post显示可以使用以下命令来列出所有内置类型加速器 :
[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::
get.GetEnumerator() | Sort-Object Key
如TheIncorrigible1指出,你甚至可以使用 ::Add()
定义您自己的类型加速器方法 ;例如,以下命令定义 [cmdinfo]
作为 [System.Management.Automation.CommandInfo]
类型的加速器:[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::
Add('cmdinfo', [System.Management.Automation.CommandInfo])
新的加速器将在 session 全局范围内可用(但仅适用于当前 session ),即使调用是从子范围进行的。也就是说,有充分的理由不这样做 :
System.Management.Automation.TypeAccelerators
不是公共(public)类型,因此它不是 PowerShell 的公共(public) API 的一部分,因此不能保证将来会存在(以目前的形式)。using namespace
方法更可取 .关于powershell - 什么是 List`1 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54961238/