예제 #1
0
 public EnumToLookup()
 {
     // set default behaviour, can be overridden by setting properties on object before calling Apply()
     NameFieldLength = 255;
     TableNamePrefix = "Enum_";
     _enumParser = new EnumParser { SplitWords = true };
 }
예제 #2
0
 /// <summary>
 ///
 /// </summary>
 public EnumToLookup()
 {
     // set default behaviour, can be overridden by setting properties on object before calling Apply()
     NameFieldLength = 255;
     TableNamePrefix = "Enum_";
     _enumParser     = new EnumParser {
         SplitWords = true
     };
 }
		public void ReadsDecoratedName()
		{
			// arrange
			var parser = new EnumParser { SplitWords = true };

			// act
			var result = parser.GetLookupValues(typeof(DecoratedEnum));

			// assert
			Assert.AreEqual("Wide boy", result.Single().Name);
		}
		public void ReadsByteEnum()
		{
			// arrange
			var parser = new EnumParser { SplitWords = false };

			// act
			var result = parser.GetLookupValues(typeof(ByteEnum));

			// assert
			Assert.AreEqual("FooBar", result.Single().Name);
		}
예제 #5
0
        /// <summary>
        /// Loops through the values in an enum type and gets the ids and names
        /// for use in the generated lookup table.
        /// </summary>
        /// <param name="lookup">Enum to process</param>
        /// <param name="getDescriptions">Descriptions of enumeration members should be got out of <see cref="DescriptionAttribute"/> got, because it will be used for DB description column.</param>
        /// <exception cref="System.ArgumentException">Lookup type must be an enum;lookup</exception>
        public ICollection <LookupValue> GetLookupValues(Type lookup, bool getDescriptions = false)
        {
            if (!lookup.IsEnum)
            {
                throw new ArgumentException("Lookup type must be an enum", "lookup");
            }

            var namesWithDescription = !getDescriptions;

            var values = new List <LookupValue>();

            foreach (Enum value in Enum.GetValues(lookup))
            {
                if (IsRuntimeOnly(value))
                {
                    continue;
                }

                // avoid cast error for byte enums by converting to int before using a cast
                // https://github.com/timabell/ef-enum-to-lookup/issues/20
                var numericValue = Convert.ChangeType(value, typeof(int));

                var lookupValue = new LookupValue
                {
                    Id   = (int)numericValue,
                    Name = EnumName(value, !getDescriptions),
                };

                if (getDescriptions)
                {
                    lookupValue.Description = EnumParser.EnumDescriptionValue(value);
                }

                values.Add(lookupValue);
            }
            return(values);
        }