File size: 10,257 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Management.Automation;
using System.Management.Automation.Internal;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Class with member information that this cmdlet writes to the pipeline.
/// </summary>
public class MemberDefinition
{
/// <summary>
/// Returns the member definition.
/// </summary>
public override string ToString()
{
return Definition;
}
/// <summary>
/// Initializes a new instance of the <see cref="MemberDefinition"/> class.
/// </summary>
public MemberDefinition(string typeName, string name, PSMemberTypes memberType, string definition)
{
Name = name;
Definition = definition;
MemberType = memberType;
TypeName = typeName;
}
/// <summary>
/// Type name.
/// </summary>
public string TypeName { get; }
/// <summary>
/// Member name.
/// </summary>
public string Name { get; }
/// <summary>
/// Member type.
/// </summary>
public PSMemberTypes MemberType { get; }
/// <summary>
/// Member definition.
/// </summary>
public string Definition { get; }
}
/// <summary>
/// This class implements get-member command.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Member", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096704", RemotingCapability = RemotingCapability.None)]
[OutputType(typeof(MemberDefinition))]
public class GetMemberCommand : PSCmdlet
{
/// <summary>
/// The object to retrieve properties from.
/// </summary>
[Parameter(ValueFromPipeline = true)]
public PSObject InputObject { get; set; }
/// <summary>
/// The member names to be retrieved.
/// </summary>
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty]
public string[] Name { get; set; } = new string[] { "*" };
/// <summary>
/// The member types to be retrieved.
/// </summary>
[Parameter]
[Alias("Type")]
public PSMemberTypes MemberType { get; set; } = PSMemberTypes.All;
/// <summary>
/// View from which the members are retrieved.
/// </summary>
[Parameter]
public PSMemberViewTypes View { get; set; } = PSMemberViewTypes.Adapted | PSMemberViewTypes.Extended;
private bool _staticParameter = false;
/// <summary>
/// True if we should return static members.
/// </summary>
[Parameter]
public SwitchParameter Static
{
get { return _staticParameter; }
set { _staticParameter = value; }
}
/// <summary>
/// Gets or sets the force property.
/// </summary>
/// <remarks>
/// Gives the Member matcher guidance on how vigorous the Match should be.
/// If set to true all members in a given view + membertype are displayed.
/// This parameter is added to hide Get/Set property accessor methods by default.
/// If a user wants to see these methods, -force should be set to true.
/// </remarks>
[Parameter]
public SwitchParameter Force
{
get
{
return (_matchOptions == MshMemberMatchOptions.IncludeHidden);
}
set
{
if (value)
{
// Include hidden members if force parameter is set
_matchOptions = MshMemberMatchOptions.IncludeHidden;
}
else
{
_matchOptions = MshMemberMatchOptions.None;
}
}
}
private MshMemberMatchOptions _matchOptions = MshMemberMatchOptions.None;
private readonly HybridDictionary _typesAlreadyDisplayed = new();
/// <summary>
/// This method implements the ProcessRecord method for get-member command.
/// </summary>
protected override void ProcessRecord()
{
if (this.InputObject == null || this.InputObject == AutomationNull.Value)
{
return;
}
Type baseObjectAsType = null;
string typeName;
Adapter staticAdapter = null;
if (this.Static == true)
{
staticAdapter = PSObject.DotNetStaticAdapter;
object baseObject = this.InputObject.BaseObject;
baseObjectAsType = baseObject as System.Type ?? baseObject.GetType();
typeName = baseObjectAsType.FullName;
}
else
{
var typeNames = this.InputObject.InternalTypeNames;
if (typeNames.Count != 0)
{
typeName = typeNames[0];
}
else
{
// This is never used for display. It is used only as a key to typesAlreadyDisplayed
typeName = "<null>";
}
}
if (_typesAlreadyDisplayed.Contains(typeName))
{
return;
}
else
{
_typesAlreadyDisplayed.Add(typeName, string.Empty);
}
PSMemberTypes memberTypeToSearch = MemberType;
PSMemberViewTypes viewToSearch = View;
if (((View & PSMemberViewTypes.Extended) == 0) &&
(!typeof(PSMemberSet).ToString().Equals(typeName, StringComparison.OrdinalIgnoreCase)))
{
// PSMemberSet is an internal memberset and its properties/methods are populated differently.
// PSMemberSet instance is created to represent PSExtended, PSAdapted, PSBase, PSObject hidden
// properties. We should honor extended properties for such case.
// request is to search dotnet or adapted or both members.
// dotnet,adapted members cannot be Script*,Note*,Code*
memberTypeToSearch ^= (PSMemberTypes.AliasProperty | PSMemberTypes.CodeMethod | PSMemberTypes.CodeProperty
| PSMemberTypes.MemberSet | PSMemberTypes.NoteProperty | PSMemberTypes.PropertySet | PSMemberTypes.ScriptMethod
| PSMemberTypes.ScriptProperty);
}
if (((View & PSMemberViewTypes.Adapted) == 0) && (View & PSMemberViewTypes.Base) == 0)
{
// base and adapted are not mentioned in the view so ignore respective properties
memberTypeToSearch ^= (PSMemberTypes.Property | PSMemberTypes.ParameterizedProperty | PSMemberTypes.Method);
}
if (((View & PSMemberViewTypes.Base) == PSMemberViewTypes.Base) &&
(InputObject.InternalBaseDotNetAdapter == null))
{
// the input object don't have a custom adapter..
// for this case adapted view and base view are the same.
viewToSearch |= PSMemberViewTypes.Adapted;
}
PSMemberInfoCollection<PSMemberInfo> membersToSearch;
if (this.Static == true)
{
membersToSearch = staticAdapter.BaseGetMembers<PSMemberInfo>(baseObjectAsType);
}
else
{
Collection<CollectionEntry<PSMemberInfo>> memberCollection = PSObject.GetMemberCollection(viewToSearch);
membersToSearch = new PSMemberInfoIntegratingCollection<PSMemberInfo>(this.InputObject, memberCollection);
}
foreach (string nameElement in this.Name)
{
ReadOnlyPSMemberInfoCollection<PSMemberInfo> readOnlyMembers;
readOnlyMembers = membersToSearch.Match(nameElement, memberTypeToSearch, _matchOptions);
MemberDefinition[] members = new MemberDefinition[readOnlyMembers.Count];
int resultCount = 0;
foreach (PSMemberInfo member in readOnlyMembers)
{
if (!Force)
{
if ((member is PSMethod memberAsPSMethod) && (memberAsPSMethod.IsSpecial))
{
continue;
}
}
members[resultCount] = new MemberDefinition(typeName, member.Name, member.MemberType, member.ToString());
resultCount++;
}
Array.Sort<MemberDefinition>(members, 0, resultCount, new MemberComparer());
for (int index = 0; index < resultCount; index++)
{
this.WriteObject(members[index]);
}
}
}
private sealed class MemberComparer : System.Collections.Generic.IComparer<MemberDefinition>
{
public int Compare(MemberDefinition first, MemberDefinition second)
{
int result = string.Compare(first.MemberType.ToString(), second.MemberType.ToString(),
StringComparison.OrdinalIgnoreCase);
if (result != 0)
{
return result;
}
return string.Compare(first.Name, second.Name, StringComparison.OrdinalIgnoreCase);
}
}
/// <summary>
/// This method implements the End method for get-member command.
/// </summary>
protected override void EndProcessing()
{
if (_typesAlreadyDisplayed.Count == 0)
{
ErrorRecord errorRecord = new(
new InvalidOperationException(GetMember.NoObjectSpecified),
"NoObjectInGetMember",
ErrorCategory.CloseError,
null);
WriteError(errorRecord);
}
}
}
}
|