/// <summary> /// Determines whether the given ODataType is the known type or subtype of the resources /// this cmdlet interacts with. /// </summary> /// <param name="odataType">The ODataType to check.</param> /// <returns>True if the OData type is known, otherwise false.</returns> internal bool IsKnownODataType(string odataType) { ODataTypeAttribute cmdletTypeAttribute = this.GetODataTypeAttribute(); return(cmdletTypeAttribute != null && ( cmdletTypeAttribute.TypeFullName == odataType || cmdletTypeAttribute.SubTypeFullNames.Contains(odataType))); }
/// <summary> /// Gets the currently selected OData type. /// </summary> /// <returns>The OData type.</returns> /// <exception cref="PSArgumentException">If neither the ODataType property nor any of the type switches are set.</exception> private string GetODataType() { // Get the attribute which specifies the valid types ODataTypeAttribute cmdletTypeAttribute = this.GetODataTypeAttribute(); // Check if the ODataType parameter was set to a valid value string userProvidedODataType = this.ODataType?.TrimStart('#'); bool isODataTypeSetToValidValue = // Make sure the ODataType parameter is not null or empty !string.IsNullOrWhiteSpace(userProvidedODataType) && // Make sure that this cmdlet has a set of known valid OData types cmdletTypeAttribute != null && ( // Validate the given type against the set of known valid OData types cmdletTypeAttribute.TypeFullName == userProvidedODataType || cmdletTypeAttribute.SubTypeFullNames.Contains(userProvidedODataType) ); // If ODataType was not set to a valid value, pick the appropriate value based on the parameter set selector that was set if (!isODataTypeSetToValidValue) { // Try to get the switch parameter which represents the OData type IEnumerable <PropertyInfo> typeSelectorPropertyInfos = this.GetParameterSetSelectorProperties(); // If no parameter set selector was set, try to use the cmdlet's OData type name (this may be the case for cmdlets which only deal with 1 OData type) if (!typeSelectorPropertyInfos.Any()) { return($"#{cmdletTypeAttribute.TypeFullName}"); } else { // If more than 1 parameter set selector was set, throw an exception PropertyInfo typeSelectorPropertyInfo = typeSelectorPropertyInfos.SingleOrDefault(); if (typeSelectorPropertyInfo == null) { throw new PSArgumentException($"Multiple type switches were set, but only 1 type switch is allowed per invocation of this cmdlet - these are the type switches that were set: [{string.Join(", ", "'" + typeSelectorPropertyInfos.Select(info => info.Name) + "'")}]"); } // Get the ParameterSetSelector attribute ParameterSetSelectorAttribute typeSelectorSwitchAttribute = typeSelectorPropertyInfo .GetCustomAttributes <ParameterSetSelectorAttribute>() .SingleOrDefault(); // Get the OData type name from the "ParameterSetSelector" attribute (parameter set name is the OData type name) return($"#{typeSelectorSwitchAttribute.ParameterSetName}"); } } else { // If ODataType was set, make sure that no parameter set selector was set if (this.GetParameterSetSelectorProperties().Any()) { throw new PSArgumentException($"Type switches cannot be used if the '{nameof(this.ODataType)}' parameter is set"); } // Set the result to the value of the ODataType parameter return(userProvidedODataType); } }