// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; namespace Microsoft.PowerShell.Cmdletization { /// /// Information about invocation of a method in an object model wrapped by an instance of /// public sealed class MethodInvocationInfo { /// /// Creates a new instance of MethodInvocationInfo. /// /// Name of the method to invoke. /// Method parameters. /// Return value of the method (ok to pass if the method doesn't return anything). public MethodInvocationInfo(string name, IEnumerable parameters, MethodParameter returnValue) { ArgumentNullException.ThrowIfNull(name); ArgumentNullException.ThrowIfNull(parameters); // returnValue can be null MethodName = name; ReturnValue = returnValue; KeyedCollection mpk = new MethodParametersCollection(); foreach (var parameter in parameters) { mpk.Add(parameter); } Parameters = mpk; } /// /// Name of the method to invoke. /// public string MethodName { get; } /// /// Method parameters. /// public KeyedCollection Parameters { get; } /// /// Return value of the method. Can be if the method doesn't return anything. /// public MethodParameter ReturnValue { get; } internal IEnumerable GetArgumentsOfType() where T : class { List result = new(); foreach (var methodParameter in this.Parameters) { if ((methodParameter.Bindings & MethodParameterBindings.In) != MethodParameterBindings.In) { continue; } if (methodParameter.Value is T objectInstance) { result.Add(objectInstance); continue; } if (methodParameter.Value is IEnumerable objectInstanceArray) { foreach (object element in objectInstanceArray) { if (element is T objectInstance2) { result.Add(objectInstance2); } } continue; } } return result; } } }