/// <summary> /// Creates dynamic parameters for all parameters defined on a <see cref="NewSensorParameters"/> object. /// </summary> /// <param name="dictionary">The dynamic parameters dictionary to add parameters to.</param> private void AddParametersObjectDynamicParameters(RuntimeDefinedParameterDictionaryEx dictionary) { //If TypeAttribute.Class is null (i.e. creating the parameters are not actually supported) a SwitchParameter wouldn't have been created //and we won't actually get to this point var parametersType = Type.GetEnumAttribute <TypeAttribute>(true); var properties = ReflectionCacheManager.Get(parametersType.Class).Properties.Where(p => p.GetAttribute <PropertyParameterAttribute>() != null); var parameterConfig = Type.GetEnumAttribute <NewSensorAttribute>() ?? new NewSensorAttribute(); int?position = null; foreach (var property in properties) { var parameterAttributes = new List <Attribute>(); var isNameParameter = property.Property.Name == nameof(NewObjectParameters.Name); position = GetPosition(property, isNameParameter, parameterConfig, position); foreach (var set in ParameterSets.Where(s => PropertyIsAllowedInSet(property, s))) { parameterAttributes.Add(new ParameterAttribute { Mandatory = GetMandatory(property, isNameParameter, parameterConfig), Position = position.Value, ParameterSetName = $"{Type}{set.Name}" }); } var name = GetParameterName(property); var type = property.Property.PropertyType; if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>)) { var underlying = type.GetGenericArguments()[0]; type = underlying.MakeArrayType(); } dictionary.AddOrMerge(name, type, parameterAttributes.ToArray()); if (alternateSet != null) { IAlternateParameter alternateParameter; if (TryGetAlternateProperty(property, alternateSet.Name, out alternateParameter)) { var parameterAttribute = new ParameterAttribute { Mandatory = HasRequireValueTrue(property), Position = position.Value, ParameterSetName = $"{Type}{alternateSet.Name}" }; dictionary.AddOrMerge(alternateParameter.Name, alternateParameter.Type, parameterAttribute); } } } }
private void AddSensorTypeDynamicParameter(RuntimeDefinedParameterDictionaryEx dictionary) { var attributes = ParameterSets.Select(set => new ParameterAttribute { Mandatory = true, ParameterSetName = $"{Type}{set.Name}", Position = 0 //Force it to the front so it displays first in PowerShell help }).ToArray(); dictionary.AddOrMerge(Type.ToString(), typeof(SwitchParameter), attributes); }
public NewSensorDynamicParameterContainer(NewSensor newSensorCmdlet, PrtgClient client) { InitializeParameterCategories(); activeSensorType = new Lazy <NewSensorDynamicParameterCategory>(() => parameterCategories.Single(c => c.HasParameterSet(newSensorCmdlet))); this.newSensorCmdlet = newSensorCmdlet; this.client = client; Parameters = GetDynamicParameters(); }
private RuntimeDefinedParameterDictionaryEx GetDynamicParameters() { var dictionary = new RuntimeDefinedParameterDictionaryEx(); foreach (var parameterCategory in parameterCategories) { parameterCategory.AddDynamicParameters(newSensorCmdlet, dictionary); } return(dictionary); }
private void AddDestinationParameter(RuntimeDefinedParameterDictionaryEx dictionary) { var type = DestinationType == NewSensorDestinationType.DestinationId ? typeof(int) : typeof(Device); var attributes = ParameterSets.Select(set => new ParameterAttribute { Mandatory = true, ValueFromPipeline = DestinationType == NewSensorDestinationType.Device, ParameterSetName = $"{Type}{set.Name}" }).ToArray(); dictionary.AddOrMerge(DestinationType.ToString(), type, attributes); }
private int?GetHighestParameterPosition(RuntimeDefinedParameterDictionaryEx dictionary) { var invocationSets = ParameterSets.Where(set => set.Invoke).Select(set => set.Name).ToList(); var specifiedPositions = dictionary.SelectMany(p => p.Value.Attributes.OfType <ParameterAttribute>()) .Where(a => invocationSets.Contains(QualifiedToUnqualifiedSetName(a.ParameterSetName)) && a.Position >= 0).ToArray(); if (specifiedPositions.Length == 0) { return(null); } return(specifiedPositions.Max(a => a.Position)); }
public void AddDynamicParameters(NewSensor newSensorCmdlet, RuntimeDefinedParameterDictionaryEx dictionary) { GetInvoker(newSensorCmdlet); //Force initialize the invoker for the parameter sets var parametersType = Type.GetEnumAttribute <TypeAttribute>(true); //Don't add parameters for types that don't have them if (parametersType.Class == null) { return; } AddParametersObjectDynamicParameters(dictionary); if (makeCmdlet != null) { AddCmdletDynamicParameters(newSensorCmdlet, dictionary); } AddSensorTypeDynamicParameter(dictionary); AddDestinationParameter(dictionary); }
private void AddCmdletDynamicParameters(NewSensor newSensorCmdlet, RuntimeDefinedParameterDictionaryEx dictionary) { var properties = ReflectionCacheManager.Get(GetInvoker(newSensorCmdlet).CmdletToInvoke.GetType()).Properties .Where(p => p.GetAttribute <ParameterAttribute>() != null).ToList(); var highestPosition = GetHighestParameterPosition(dictionary); foreach (var property in properties) { var description = property.GetAttribute <DescriptionAttribute>(); var name = description?.Description ?? property.Property.Name; //Not cached so we can modify it var attributes = Attribute.GetCustomAttributes(property.Property, typeof(ParameterAttribute)).ToList(); foreach (ParameterAttribute attribute in attributes) { attribute.ParameterSetName = $"{Type}{attribute.ParameterSetName}"; if (attribute.Position >= 0 && highestPosition != null) { attribute.Position += highestPosition.Value + 1; } } var aliases = property.GetAttribute <AliasAttribute>(); if (aliases != null && !aliases.AliasNames.Contains(name)) { attributes.Add(aliases); } dictionary.AddOrMerge(name, property.Property.PropertyType, attributes.ToArray()); } }