Esempio n. 1
0
        /// <summary>
        ///     Extract the input types for a Cmdlet.
        /// </summary>
        /// <param name="cmdletType">
        ///     The CLR type that implements the Cmdlet.
        /// </param>
        /// <returns>
        ///     A list of values, which may be empty or null.
        /// </returns>
        public List <CommandValue> GetCmdletInputTypes(TypeInfo cmdletType)
        {
            if (cmdletType == null)
            {
                throw new ArgumentNullException(nameof(cmdletType));
            }

            var inputTypeAttributes = cmdletType.GetCustomAttributes <CmdletInputTypeAttribute>().ToList();

            if (inputTypeAttributes.Count == 0)
            {
                return(null);
            }

            var inputTypes = new List <CommandValue>();

            foreach (var attribute in inputTypeAttributes)
            {
                inputTypes.Add(new CommandValue
                {
                    DataType =
                    {
                        Name = attribute.IsCLRType ? MamlGenerator.PowerShellIfyTypeName(attribute.Type) : attribute.Name,
                        Uri  = attribute.Uri
                    },
                    Description = MamlGenerator.ToParagraphs(attribute.Description)
                });
            }

            return(inputTypes);
        }
Esempio n. 2
0
        /// <summary>
        ///     Extract the input types for a Cmdlet.
        /// </summary>
        /// <param name="cmdletType">
        ///     The CLR type that implements the Cmdlet.
        /// </param>
        /// <returns>
        ///     A list of values, which may be empty or null.
        /// </returns>
        public List <CommandValue> GetCmdletInputTypes(TypeInfo cmdletType)
        {
            if (cmdletType == null)
            {
                throw new ArgumentNullException(nameof(cmdletType));
            }

            var assemblyDoc = GetAssemblyDocumentation(cmdletType);

            if (assemblyDoc == null)
            {
                return(null);
            }
            var inputElements = assemblyDoc.GetInputs(cmdletType);

            var inputTypes = new List <CommandValue>();

            foreach (var element in inputElements)
            {
                var see         = element.Element("sees");
                var paras       = element.Elements("para")?.Select(e => MamlGenerator.ToParagraphs(e?.Value?.Trim())).ToList() ?? new List <List <string> >();
                var description = new List <string>();

                if (see == null && paras.Count == 0)
                {
                    description.AddRange(MamlGenerator.ToParagraphs(element.Value?.Trim()));
                }
                else
                {
                    foreach (var list in paras)
                    {
                        description.AddRange(list);
                    }
                }

                if (see == null || !see.HasAttributes)
                {
                    var first = description[0];
                    description.RemoveAt(0);
                    element.Add(
                        new CommandValue
                    {
                        DataType    = { Name = first },
                        Description = description
                    }
                        );
                    continue;
                }

                var name = see.Attribute("cref")?.Value?.Trim() ?? string.Empty;
                if (name != null)
                {
                    try {
                        var type = Type.GetType(name);
                        if (type != null)
                        {
                            name = MamlGenerator.PowerShellIfyTypeName(type);
                        }
                    } catch (ArgumentException)
                    {
                        // Ignore this exception
                    } catch (TypeLoadException)
                    {
                        // Ignore this exception
                    }
                }

                element.Add(
                    new CommandValue
                {
                    DataType =
                    {
                        Name        = name ?? string.Empty,
                        Uri         = see.Attribute("uri")?.Value?.Trim(),
                        Description = MamlGenerator.ToParagraphs(see.Value?.Trim())
                    },
                    Description = description
                }
                    );
            }

            return(inputTypes);
        }