英文:
Type different between .GetType() and Get-Member
问题
GetType() 方法不返回与 Get-Member 相同的类型是因为 GetType() 方法返回一个对象的实际运行时类型,而 Get-Member 用于获取对象的成员和类型信息。在这种情况下,$x 的实际运行时类型是 "Instance",而 Get-Member 显示了它的详细类型信息为 "Microsoft.VisualStudio.Setup.Instance"。
英文:
Why does GetType() not return the same type as Get-Member?
PS C:\> Find-Module -Name vssetup | Install-Module
PS C:\> $x = (Get-VSSetupInstance)[0]
PS C:\> $x.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False Instance System.Object
PS C:\> $x | Get-Member
TypeName: Microsoft.VisualStudio.Setup.Instance
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
...
答案1
得分: 2
以下是翻译的内容:
<!-- language-all: sh -->
The type being reported _is_ the same, it's just _its representation_ that differs.
报告的类型是相同的,只是其表示不同。
As of PowerShell 7.3.4, the _for-display_ formatting of the [`System.Reflection.TypeInfo`](https://learn.microsoft.com/en-US/dotnet/api/System.Reflection.TypeInfo) instance returned by `.GetType()` - unfortunately - doesn't reflect the type's _namespace-qualified_ name, whereas [`Get-Member`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Get-Member)'s output does.
截至到 PowerShell 7.3.4,由.GetType()
返回的System.Reflection.TypeInfo
实例的 for-display 格式 - 遗憾的是 - 不反映类型的 namespace-qualified 名称,而Get-Member
的输出则反映。
To get the type's namespace-qualified name based on a `.GetType()` call, simply access the returned object's `.FullName` property:
要根据.GetType()
调用获取类型的命名空间限定名称,只需访问返回的对象的.FullName
属性:
$x.GetType().FullName
$x.GetType().FullName
Note that `Get-Member`'s purpose is not _only_ to report a .NET type's _identity_, but also to report _its members_ - hence the additional information about `Equals`, `GetHashCode`, ... (mediated via the for-display formatting of the [`Microsoft.PowerShell.Commands.MemberDefinition`](https://learn.microsoft.com/en-US/dotnet/api/Microsoft.PowerShell.Commands.MemberDefinition) instances that `Get-Member` outputs).
请注意,Get-Member
的目的不仅仅是报告.NET类型的 identity,还包括报告 其成员 - 因此还提供了关于Equals
、GetHashCode
等的附加信息(通过Get-Member
输出的Microsoft.PowerShell.Commands.MemberDefinition
实例的 for-display 格式来中介)。
As such, the details of the for-display formatting are _by design_ different.
因此,for-display 格式的细节是有意设计成不同的。
As an aside:
-
Unfortunately,
Microsoft.PowerShell.Commands.MemberDefinition
instance, while providing useful, nicely formatted high-level information about .NET type members, they do not provide access to the full .NET reflection APIs that theSystem.Reflection.MemberInfo
-derived instances output by the reflection methods called directly on types (.GetType().GetMember(...)
, etc.) do. -
See this answer for function
Get-NativeMember
, which offers similar convenience toGet-Member
, but returns the actualSystem.Reflection.MemberInfo
-derived instances for full access to .NET's reflection API; note that it does so for type-native members only (not also for members provided via PowerShell's ETS (Extended Type System)).
顺便提一下:
* 不幸的是,`Microsoft.PowerShell.Commands.MemberDefinition`实例虽然提供了有用的、精美格式的关于.NET类型成员的 _高级_ 信息,但它们 _不_ 提供访问*完整的.NET反射API*的能力,而直接在类型上调用的反射方法(`.GetType().GetMember(...)`等)输出的是派生自[`System.Reflection.MemberInfo`](https://learn.microsoft.com/en-US/dotnet/api/System.Reflection.MemberInfo)的实例。
* 请查看[此答案](https://stackoverflow.com/a/76884750/45375),其中介绍了函数`Get-NativeMember`,它提供了与`Get-Member`类似的便利性,但返回了实际的`System.Reflection.MemberInfo`派生实例,以完全访问.NET的反射API;请注意,它仅针对类型-_native_成员提供此功能(而不适用于通过PowerShell的ETS(Extended Type System)提供的成员)。
英文:
<!-- language-all: sh -->
The type being reported is the same, it's just its representation that differs.
As of PowerShell 7.3.4, the for-display formatting of the System.Reflection.TypeInfo
instance returned by .GetType()
- unfortunately - doesn't reflect the type's namespace-qualified name, whereas Get-Member
's output does.
To get the type's namespace-qualified name based on a .GetType()
call, simply access the returned object's .FullName
property:
# -> 'Microsoft.VisualStudio.Setup.Instance'
$x.GetType().FullName
Note that Get-Member
's purpose is not only to report a .NET type's identity, but also to report its members - hence the additional information about Equals
, GetHashCode
, ... (mediated via the for-display formatting of the Microsoft.PowerShell.Commands.MemberDefinition
instances that Get-Member
outputs).
As such, the details of the for-display formatting are by design different.
As an aside:
-
Unfortunately,
Microsoft.PowerShell.Commands.MemberDefinition
instance, while providing useful, nicely formatted high-level information about .NET type members, they do not provide access to the full .NET reflection APIs that theSystem.Reflection.MemberInfo
-derived instances output by the reflection methods called directly on types (.GetType().GetMember(...)
, etc.) do. -
See this answer for function
Get-NativeMember
, which offers similar convenience toGet-Member
, but returns the actualSystem.Reflection.MemberInfo
-derived instances for full access to .NET's reflection API; note that it does so for type-native members only (not also for members provided via PowerShell's ETS (Extended Type System)).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论