internal static CommandLineArgument Create(ParameterInfo parameter) { var ret = PropertyInitializer.CreateInstance <CommandLineArgument>(); ret.Position = parameter.Position; ret.ArgumentType = parameter.ParameterType; ret.Source = parameter; ret.DefaultValue = parameter.HasAttr <DefaultValueAttribute>() ? parameter.Attr <DefaultValueAttribute>().Value : null; ret.IgnoreCase = true; if (parameter.Member.DeclaringType.HasAttr <ArgIgnoreCase>() && parameter.Member.DeclaringType.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } if (parameter.HasAttr <ArgIgnoreCase>() && parameter.Attr <ArgIgnoreCase>().IgnoreCase == false) { ret.IgnoreCase = false; } ret.Aliases.Add(parameter.Name); ret.Metadata.AddRange(parameter.Attrs <IArgMetadata>().AssertAreAllInstanceOf <ICommandLineArgumentMetadata>()); return(ret); }
/// <summary> /// Gets the attribute of the given type or null if the parameter does not have this attribute defined. The standard reflection helper GetCustomAttributes will /// give you a new instance of the attribute every time you call it. This helper caches it's results so if you ask for the same attibute twice you will actually /// get back the same attribute. Note that the cache key is based off of the type T that you provide. So asking for Attr() where T : BaseType> and then asking for Attr() where T : ConcreteType /// will result in two different objects being returned. If you ask for Attr() where T : BaseType and then Attr() where T :BaseType the caching will work and you'll get the same object back /// the second time. /// </summary> /// <typeparam name="T">The type of attribute to search for</typeparam> /// <param name="info">The parameter to inspect</param> /// <returns>The desired attribute or null if it is not present</returns> public static T Attr <T>(this ParameterInfo info) { return(info.Attrs <T>().FirstOrDefault()); }
/// <summary> /// Returns true if the given parameter has an attribute of the given type (including inherited types). /// </summary> /// <typeparam name="T">The type of attribute to test for (will return true for attributes that inherit from this type)</typeparam> /// <param name="info">The parameter to test</param> /// <returns>true if a matching attribute was found, false otherwise</returns> public static bool HasAttr <T>(this ParameterInfo info) { return(info.Attrs <T>().Count > 0); }