示例#1
0
        /// <summary>
        /// Adds the supplied <see cref="AttributeField"/>s to the <see cref="ArgumentDictionary"/>.
        /// </summary>
        /// <param name="attr">The <see cref="ArgumentAttribute"/> to use the <see cref="ArgumentAttribute.LongName"/> or <see cref="AttributeField.Attr.ShortName"/> of.</param>
        /// <param name="fields">The <see cref="AttributeField"/>s to add to the <see cref="ArgumentDictionary"/>.</param>
        public void Add(ArgumentAttribute attr, IEnumerable <AttributeField> fields)
        {
            if (this.innerContainer.ContainsKey(attr.LongName))
            {
                this.innerContainer[attr.LongName].AddRange(fields);
            }
            else
            {
                this.innerContainer.Add(attr.LongName, fields.ToList());
            }

            if (this.innerContainer.ContainsKey(attr.ShortName))
            {
                this.innerContainer[attr.ShortName].AddRange(fields);
            }
            else
            {
                this.innerContainer.Add(attr.ShortName, fields.ToList());
            }

            this.cachedValues = null;
        }
示例#2
0
        /// <summary>
        /// Reflects over the assembly to populate the <see cref="Fields"/>.
        /// </summary>
        /// <param name="restrictedToTypes">An <see cref="IEnumerable{Type}"/> containing types which should be included in the resulting <see cref="ArgumentDictionary"/>.</param>
        /// <returns>
        /// An <see cref="ArgumentDictionary"/>s representing all fields in the executing assembly decorated with an <see cref="ArgumentAttribute"/>.
        /// </returns>
        private static ArgumentDictionary Iterate(IEnumerable <Type> restrictedToTypes)
        {
            ArgumentDictionary returnValue   = new ArgumentDictionary();
            List <Type>        assemblyTypes = new List <Type>();

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    assemblyTypes.AddRange(assembly.GetTypes());
                }
                catch (ReflectionTypeLoadException)
                {
                    // Ignore this exception type.
                }
            }

            // TODO: Review whether it's appropriate to use restrictedToTypes like this. Maybe should only perform the assembly search if restrictedToTypes.Any() == false?
            List <Type> toInclude = restrictedToTypes.Any()
                ? restrictedToTypes.Where(x => assemblyTypes.Contains(x)).ToList()
                : assemblyTypes;

            foreach (Type type in toInclude)
            {
                foreach (FieldInfo field in type.GetFields(Context.bindingFlags))
                {
                    ArgumentAttribute attribute = field.GetCustomAttributes <ArgumentAttribute>().FirstOrDefault();
                    if (attribute != null)
                    {
                        returnValue.Add(attribute, new AttributeField(attribute, field));
                    }
                }
            }

            return(returnValue);
        }
示例#3
0
 /// <summary>
 /// Instantiates an <see cref="AttributeField"/> object using the supplied parameters.
 /// </summary>
 /// <param name="attribute">The <see cref="ArgumentAttribute"/> of the <see cref="AttributeField"/>.</param>
 /// <param name="field">The <see cref="FieldInfo"/> of the <see cref="AttributeField"/>.</param>
 public AttributeField(ArgumentAttribute attribute, FieldInfo field)
 {
     this.Attr  = attribute;
     this.Field = field;
 }