PageRenderTime 37ms CodeModel.GetById 32ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 1ms

/Mercurial.Net/Attributes/EnumArgumentAttribute.cs

#
C# | 137 lines | 73 code | 13 blank | 51 comment | 11 complexity | 23cf957e570a55f6757f95705a789825 MD5 | raw file
  1using System;
  2using System.Globalization;
  3
  4namespace Mercurial.Attributes
  5{
  6    /// <summary>
  7    /// This attribute can be applied to enum-properties in option classes,
  8    /// to specify the arguments to pass to the executable in case of
  9    /// each possible enum value for the property. Note that usage of any enum values that
 10    /// don't have a corresponding <see cref="EnumArgumentAttribute"/> will be ignored.
 11    /// </summary>
 12    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
 13    public sealed class EnumArgumentAttribute : ArgumentAttribute
 14    {
 15        /// <summary>
 16        /// This is the backing field for the <see cref="Argument1"/> and <see cref="Argument2"/> properties.
 17        /// </summary>
 18        private readonly string[] _Arguments = new string[0];
 19
 20        /// <summary>
 21        /// This is the backing field for the <see cref="Value"/> property.
 22        /// </summary>
 23        private readonly object _Value;
 24
 25        /// <summary>
 26        /// Initializes a new instance of the <see cref="EnumArgumentAttribute"/> class
 27        /// with one argument for the enum value.
 28        /// </summary>
 29        /// <param name="value">The enum value.</param>
 30        /// <param name="argument1">The argument.</param>
 31        public EnumArgumentAttribute(object value, string argument1)
 32        {
 33            _Value = value;
 34            _Arguments = new[]
 35            {
 36                argument1
 37            };
 38        }
 39
 40        /// <summary>
 41        /// Initializes a new instance of the <see cref="EnumArgumentAttribute"/> class
 42        /// with two arguments for the enum value.
 43        /// </summary>
 44        /// <param name="value">The enum value.</param>
 45        /// <param name="argument1">The first argument.</param>
 46        /// <param name="argument2">The second argument.</param>
 47        public EnumArgumentAttribute(object value, string argument1, string argument2)
 48        {
 49            _Value = value;
 50            _Arguments = new[]
 51            {
 52                argument1, argument2
 53            };
 54        }
 55
 56        /// <summary>
 57        /// Gets the enum value to map the arguments to.
 58        /// </summary>
 59        public object Value
 60        {
 61            get
 62            {
 63                return _Value;
 64            }
 65        }
 66
 67        /// <summary>
 68        /// Gets the first argument that the <see cref="Value"/> is mapped to,
 69        /// or <see cref="string.Empty"/> if there is no argument.
 70        /// </summary>
 71        public string Argument1
 72        {
 73            get
 74            {
 75                if (_Arguments != null && _Arguments.Length >= 1)
 76                    return _Arguments[0];
 77
 78                return string.Empty;
 79            }
 80        }
 81
 82        /// <summary>
 83        /// Gets the second argument that the <see cref="Value"/> is mapped to,
 84        /// or <see cref="string.Empty"/> if there is no second argument.
 85        /// </summary>
 86        public string Argument2
 87        {
 88            get
 89            {
 90                if (_Arguments != null && _Arguments.Length >= 2)
 91                    return _Arguments[1];
 92
 93                return string.Empty;
 94            }
 95        }
 96
 97        /// <summary>
 98        /// Gets or sets a value indicating whether the enum value is a bitmask value (see <see cref="FlagsAttribute"/>)
 99        /// or not.
100        /// </summary>
101        public bool IsBitmask
102        {
103            get;
104            set;
105        }
106
107        /// <summary>
108        /// Gets a collection of options or arguments to pass to the Mercurial
109        /// executable, based on the property value from the command class.
110        /// </summary>
111        /// <param name="propertyValue">
112        /// The property value from the tagged property of the command class.
113        /// </param>
114        /// <returns>
115        /// A collection of options or arguments, or an empty array or <c>null</c>
116        /// for no options for the specified property value.
117        /// </returns>
118        public override string[] GetOptions(object propertyValue)
119        {
120            int propertyValueAsNumber = Convert.ToInt32(propertyValue, CultureInfo.InvariantCulture);
121            int valueAsNumber = Convert.ToInt32(_Value, CultureInfo.InvariantCulture);
122
123            if (IsBitmask)
124            {
125                if ((propertyValueAsNumber & valueAsNumber) == valueAsNumber)
126                    return _Arguments;
127            }
128            else
129            {
130                if (propertyValueAsNumber == valueAsNumber)
131                    return _Arguments;
132            }
133
134            return null;
135        }
136    }
137}