// 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) { if (name == null) throw new ArgumentNullException(nameof(name)); if (parameters == null) throw new ArgumentNullException(nameof(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; } var objectInstance = methodParameter.Value as T; if (objectInstance != null) { result.Add(objectInstance); continue; } var objectInstanceArray = methodParameter.Value as IEnumerable; if (objectInstanceArray != null) { foreach (object element in objectInstanceArray) { var objectInstance2 = element as T; if (objectInstance2 != null) { result.Add(objectInstance2); } } continue; } } return result; } } }