Пример #1
0
        /// <summary>
        /// Generates a resource path from the given object ID.
        /// </summary>
        /// <param name="currentCmdlet">The currently running cmdlet (which has the properties that represent URL placeholders)</param>
        /// <param name="id">The ID of the object being referenced</param>
        /// <returns>The generated URL.</returns>
        internal string GenerateResourcePath(ODataCmdletBase currentCmdlet, string id)
        {
            // Set the entity ID
            this._idProperty?.SetValue(this._cmdletInstance, id);

            // Set the placeholder properties
            foreach (PropertyInfo property in this._placeholderProperties)
            {
                // Get the equivalent property on the current cmdlet (if the type is different)
                PropertyInfo currentCmdletProperty = property;
                Type         currentCmdletType     = currentCmdlet.GetType();
                if (currentCmdletType != this._cmdletInstance.GetType())
                {
                    // The property name and type must match
                    currentCmdletProperty = currentCmdletType.GetProperty(property.Name, property.PropertyType);
                }

                // Get the value from the currently running cmdlet
                object propertyValue = currentCmdletProperty.GetValue(currentCmdlet);

                // Set the cached cmdlet instance's equivalent property to the same value
                property.SetValue(this._cmdletInstance, propertyValue);
            }

            // Create the relative resource path
            string path = this._cmdletInstance.GetResourcePath();

            return(path);
        }
Пример #2
0
        internal ReferencePathGenerator(ODataCmdletBase cmdletInstance)
        {
            this._cmdletInstance = cmdletInstance ?? throw new ArgumentNullException(nameof(cmdletInstance));

            // Get the type of this cmdlet
            Type cmdletType = this._cmdletInstance.GetType();

            // Get the "id" property
            this._idProperty = cmdletType.GetProperties()
                               .Where(prop => prop.GetCustomAttribute <ResourceIdParameterAttribute>() != null)
                               .FirstOrDefault();

            // Get the placeholder properties
            this._placeholderProperties = cmdletType.GetProperties()
                                          .Where(prop =>
                                                 // ID properties (except the entity ID property that we just retrieved)
                                                 (prop != this._idProperty && prop.GetCustomAttribute <IdParameterAttribute>() != null)
                                                 // Type cast properties
                                                 || prop.GetCustomAttribute <TypeCastParameterAttribute>() != null);
        }