File size: 5,503 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
namespace Microsoft.PowerShell.Commands.ShowCommandExtension
{
/// <summary>
/// Implements a facade around CommandInfo and its deserialized counterpart.
/// </summary>
public class ShowCommandCommandInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="ShowCommandCommandInfo"/> class
/// with the specified <see cref="CommandInfo"/>.
/// </summary>
/// <param name="other">
/// The object to wrap.
/// </param>
public ShowCommandCommandInfo(CommandInfo other)
{
ArgumentNullException.ThrowIfNull(other);
this.Name = other.Name;
this.ModuleName = other.ModuleName;
this.CommandType = other.CommandType;
this.Definition = other.Definition;
// In a runspace with restricted security settings we catch
// PSSecurityException when accessing ParameterSets because
// ExternalScript commands may be evaluated.
try
{
this.ParameterSets =
other.ParameterSets
.Select(static x => new ShowCommandParameterSetInfo(x))
.ToList()
.AsReadOnly();
}
catch (PSSecurityException)
{
// Since we can't access the parameter sets of this command,
// populate the ParameterSets property with an empty list
// so that consumers don't trip on a null value.
this.ParameterSets = new List<ShowCommandParameterSetInfo>().AsReadOnly();
}
catch (ParseException)
{
// Could not parse the given command so don't continue initializing it
this.ParameterSets = new List<ShowCommandParameterSetInfo>().AsReadOnly();
}
if (other.Module != null)
{
this.Module = new ShowCommandModuleInfo(other.Module);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ShowCommandCommandInfo"/> class
/// with the specified <see cref="PSObject"/>.
/// </summary>
/// <param name="other">
/// The object to wrap.
/// </param>
public ShowCommandCommandInfo(PSObject other)
{
ArgumentNullException.ThrowIfNull(other);
this.Name = other.Members["Name"].Value as string;
this.ModuleName = other.Members["ModuleName"].Value as string;
this.Definition = other.Members["Definition"].Value as string;
this.ParameterSets = other.Members["ParameterSets"].Value as ICollection<ShowCommandParameterSetInfo>;
if (this.ParameterSets != null)
{
// Simple case - the objects are still live because they came from in-proc. Just cast them back
this.CommandType = (CommandTypes)(other.Members["CommandType"].Value);
this.Module = other.Members["Module"].Value as ShowCommandModuleInfo;
}
else
{
// Objects came in their deserialized form - recreate the object graph
this.CommandType = (CommandTypes)((other.Members["CommandType"].Value as PSObject).BaseObject);
var parameterSets = (other.Members["ParameterSets"].Value as PSObject).BaseObject as System.Collections.ArrayList;
this.ParameterSets = GetObjectEnumerable(parameterSets).Cast<PSObject>().Select(static x => new ShowCommandParameterSetInfo(x)).ToList().AsReadOnly();
if (other.Members["Module"]?.Value is PSObject)
{
this.Module = new ShowCommandModuleInfo(other.Members["Module"].Value as PSObject);
}
}
}
/// <summary>
/// Builds a strongly typed IEnumerable{object} out of an IEnumerable.
/// </summary>
/// <param name="enumerable">
/// The object to enumerate.
/// </param>
internal static IEnumerable<object> GetObjectEnumerable(System.Collections.IEnumerable enumerable)
{
foreach (object obj in enumerable)
{
yield return obj;
}
}
/// <summary>
/// A string representing the definition of the command.
/// </summary>
public string Name { get; }
/// <summary>
/// A string representing module the command belongs to.
/// </summary>
public string ModuleName { get; }
/// <summary>
/// A reference to the module the command came from.
/// </summary>
public ShowCommandModuleInfo Module { get; }
/// <summary>
/// An enumeration of the command types this command belongs to.
/// </summary>
public CommandTypes CommandType { get; }
/// <summary>
/// A string representing the definition of the command.
/// </summary>
public string Definition { get; }
/// <summary>
/// A string representing the definition of the command.
/// </summary>
public ICollection<ShowCommandParameterSetInfo> ParameterSets { get; }
}
}
|