示例#1
0
        /// <inheritdoc />
        public override string ToString()
        {
            var stringBuilder = new StringBuilder();

            if (Attributes != null && Attributes.Any())
            {
                foreach (var expression in Attributes)
                {
                    if (!(expression.Body is NewExpression newExpression))
                    {
                        continue;
                    }

                    foreach (var newExpressionArgument in newExpression.Arguments)
                    {
                        if (newExpressionArgument is ConstantExpression constantExpression)
                        {
                            stringBuilder.AppendLine(
                                $"\t[{expression.Compile().Invoke()}(\"{constantExpression.Value}\")]");
                        }
                    }
                }
            }

            if (CompiledAttributes != null && CompiledAttributes.Any())
            {
                foreach (var compiledAttribute in CompiledAttributes)
                {
                    var aggregate = compiledAttribute.GetType()
                                    .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                    .Where(i => i.PropertyType.Namespace != null && i.PropertyType.Namespace.StartsWith("System") &&
                                           i.CanWrite)
                                    .Select(i => new { i.Name, Value = i.GetValue(compiledAttribute) })
                                    .Where(i => i.Value != null)
                                    .Select(i => i.Value is string?$"{i.Name}=\"{i.Value}\"" : $"{i.Name}={i.Value}")
                                    .Aggregate((x, y) => $"{x}, {y}");
                    stringBuilder.AppendLine($"\t[{compiledAttribute}({aggregate})]");
                }
            }

            stringBuilder.AppendLine(
                $"\t{(IsInterface ? string.Empty : "public ")}{GetResolvedTypeName(Type)} {Name} {{ get;{(IsReadOnly ? string.Empty : " set;")} }}");

            return(stringBuilder.ToString());
        }
示例#2
0
        /// <summary>
        /// Processes the Attribute metadata to generate a CompiledCommandAttribute.
        /// </summary>
        /// <exception cref="MetadataException">
        /// If the attribute is a parameter attribute and another parameter attribute
        /// has been processed with the same parameter-set name.
        /// </exception>
        private void ProcessAttribute(
            string memberName,
            Attribute attribute,
            ref Collection <ValidateArgumentsAttribute> validationAttributes,
            ref Collection <ArgumentTransformationAttribute> argTransformationAttributes,
            ref string[] aliases)
        {
            if (attribute == null)
            {
                return;
            }

            CompiledAttributes.Add(attribute);

            // Now process the attribute based on it's type
            if (attribute is ParameterAttribute paramAttr)
            {
                ProcessParameterAttribute(memberName, paramAttr);
                return;
            }

            ValidateArgumentsAttribute validateAttr = attribute as ValidateArgumentsAttribute;

            if (validateAttr != null)
            {
                if (validationAttributes == null)
                {
                    validationAttributes = new Collection <ValidateArgumentsAttribute>();
                }
                validationAttributes.Add(validateAttr);
                if ((attribute is ValidateNotNullAttribute) || (attribute is ValidateNotNullOrEmptyAttribute))
                {
                    this.CannotBeNull = true;
                }

                return;
            }

            AliasAttribute aliasAttr = attribute as AliasAttribute;

            if (aliasAttr != null)
            {
                if (aliases == null)
                {
                    aliases = aliasAttr.aliasNames;
                }
                else
                {
                    var prevAliasNames = aliases;
                    var newAliasNames  = aliasAttr.aliasNames;
                    aliases = new string[prevAliasNames.Length + newAliasNames.Length];
                    Array.Copy(prevAliasNames, aliases, prevAliasNames.Length);
                    Array.Copy(newAliasNames, 0, aliases, prevAliasNames.Length, newAliasNames.Length);
                }

                return;
            }

            ArgumentTransformationAttribute argumentAttr = attribute as ArgumentTransformationAttribute;

            if (argumentAttr != null)
            {
                if (argTransformationAttributes == null)
                {
                    argTransformationAttributes = new Collection <ArgumentTransformationAttribute>();
                }
                argTransformationAttributes.Add(argumentAttr);
                return;
            }

            AllowNullAttribute allowNullAttribute = attribute as AllowNullAttribute;

            if (allowNullAttribute != null)
            {
                this.AllowsNullArgument = true;
                return;
            }

            AllowEmptyStringAttribute allowEmptyStringAttribute = attribute as AllowEmptyStringAttribute;

            if (allowEmptyStringAttribute != null)
            {
                this.AllowsEmptyStringArgument = true;
                return;
            }

            AllowEmptyCollectionAttribute allowEmptyCollectionAttribute = attribute as AllowEmptyCollectionAttribute;

            if (allowEmptyCollectionAttribute != null)
            {
                this.AllowsEmptyCollectionArgument = true;
                return;
            }

            ObsoleteAttribute obsoleteAttr = attribute as ObsoleteAttribute;

            if (obsoleteAttr != null)
            {
                ObsoleteAttribute = obsoleteAttr;
                return;
            }

            PSTypeNameAttribute psTypeNameAttribute = attribute as PSTypeNameAttribute;

            if (psTypeNameAttribute != null)
            {
                this.PSTypeName = psTypeNameAttribute.PSTypeName;
            }
        }