gpt4 book ai didi

powershell - 如果 Get-Member 未列出哈希表 .Get_Item() 和 .Set_Item() 如何工作?

转载 作者:行者123 更新时间:2023-12-02 11:30:37 27 4
gpt4 key购买 nike

我遇到了Powershell hash table article它使用了 .Get_Item() 和 .Set_Item()。

两个问题:

1) 允许 .Get_Item() 和 .Set_Item() 语法工作的机制是什么?

2) 可以运行什么 Powershell 命令(如果有)来“发现”Get-Member 未列出的额外属性和方法?

示例程序:

$h = @{}
$h | get-member
$h | get-member -static

$h.add("copper", 29)

$h["copper"]
$h.item("copper")
$h.get_item("copper")

输出显示 Item(),但既不显示 Get_Item() 也不显示 Set_Item():

   TypeName: System.Collections.Hashtable

Name MemberType Definition
---- ---------- ----------
Add Method System.Void Add(System.Object key, System.Object value)
Clear Method System.Void Clear()
Clone Method System.Object Clone()
Contains Method bool Contains(System.Object key)
ContainsKey Method bool ContainsKey(System.Object key)
ContainsValue Method bool ContainsValue(System.Object value)
CopyTo Method System.Void CopyTo(array array, int arrayIndex)
Equals Method bool Equals(System.Object obj)
GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator()
GetHashCode Method int GetHashCode()
GetObjectData Method System.Void GetObjectData(System.Runtime.Serialization.Serializatio...
GetType Method type GetType()
OnDeserialization Method System.Void OnDeserialization(System.Object sender)
Remove Method System.Void Remove(System.Object key)
ToString Method string ToString()
Item ParameterizedProperty System.Object Item(System.Object key) {get;set;}
Count Property System.Int32 Count {get;}
IsFixedSize Property System.Boolean IsFixedSize {get;}
IsReadOnly Property System.Boolean IsReadOnly {get;}
IsSynchronized Property System.Boolean IsSynchronized {get;}
Keys Property System.Collections.ICollection Keys {get;}
SyncRoot Property System.Object SyncRoot {get;}
Values Property System.Collections.ICollection Values {get;}
TypeName: System.Collections.Hashtable

Name MemberType Definition
---- ---------- ----------
Equals Method static bool Equals(System.Object objA, System.Object objB)
ReferenceEquals Method static bool ReferenceEquals(System.Object objA, System.Object objB)
Synchronized Method static hashtable Synchronized(hashtable table)
29
29
29

最佳答案

如果不指定 get-member cmdlet 的 -force 参数,则不会显示任何对象的 get_ 和 set_ 方法 http://technet.microsoft.com/en-us/library/hh849928.aspx :

-Force
Adds the intrinsic members (PSBase, PSAdapted, PSObject, PSTypeNames) and the compiler-generated get_ and set_ methods to the display. By default, Get-Member gets these properties in all views other than "Base" and "Adapted," but it does not display them

关于powershell - 如果 Get-Member 未列出哈希表 .Get_Item() 和 .Set_Item() 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23639564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com