public void GetAvailableValuesTest() { const uint type = 123; int[] values = new int[] { 1, 2, 10 }; EnumValueCollection collection = new EnumValueCollection(); collection.Add(new EnumValue(1, "1", 0)); collection.Add(new EnumValue(2, "2", 0)); collection.Add(new EnumValue(3, "3", 0)); collection.Add(new EnumValue(10, "10", 0)); Mockery mockery = new Mockery(); ICamera camera = (ICamera)mockery.NewMock(typeof(ICamera)); Expect.Once.On(camera).Method("GetAvailableValues").With(type).Will(Return.Value(values)); EnumValueCollection result = EnumValue.GetListFrom(camera, type, collection); Assert.AreEqual(values.Length, result.Count); Assert.AreEqual(values[0], result[1].Value); Assert.AreEqual(values[1], result[2].Value); Assert.AreEqual(values[2], result[10].Value); mockery.VerifyAllExpectationsHaveBeenMet(); }
public static EnumValueCollection GetEnumCollection(Type EnumType) { FieldInfo[] fieldinfo = EnumType.GetFields(); EnumValueCollection enumValueCollection = new EnumValueCollection(); string description = null; foreach (FieldInfo field in fieldinfo) { if (field.IsSpecialName) { continue; } DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); description = (attributes.Length > 0) ? attributes[0].Description : field.GetValue(null).ToString(); enumValueCollection.Add(new EnumValue(Convert.ToInt32(field.GetValue(null)), field.GetValue(null).ToString(), description)); } return(enumValueCollection); }
/// <summary> /// Returns back a EnumCollection object for the given Enumeration /// This EnumCollection object can then be used as a DataSource for the UI /// controls /// </summary> /// <param name="EnumType"></param> /// <returns>EnumValueCollection Object</returns> public static EnumValueCollection GetEnumCollection(Type EnumType) { FieldInfo[] fieldsInfo = EnumType.GetFields(); EnumValueCollection enumValueCollection = new EnumValueCollection(); String description = null; // Now loop on all the fields foreach (FieldInfo field in fieldsInfo) { // Continue only for normal fields if (field.IsSpecialName) { continue; } // Now get all the Description attributes of the Enumeration DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); // Generate a friendly name for the Enum value description = (attributes.Length > 0) ? attributes[0].Description : field.GetValue(null).ToString(); enumValueCollection.Add(new EnumValue(Convert.ToInt32(field.GetValue(null)), field.GetValue(null).ToString(), description)); } return(enumValueCollection); }