2009-12-01 19:17:31 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2013-04-25 13:11:59 +00:00
|
|
|
Copyright (C) 2002-2013 Nuclex Development Labs
|
2009-12-01 19:17:31 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
License along with this library
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
namespace Nuclex.Support {
|
|
|
|
|
|
|
|
/// <summary>Helper methods for enumerations</summary>
|
|
|
|
public static class EnumHelper {
|
|
|
|
|
|
|
|
/// <summary>Returns the highest value encountered in an enumeration</summary>
|
2013-04-25 13:11:59 +00:00
|
|
|
/// <typeparam name="TEnumeration">
|
2009-12-01 19:17:31 +00:00
|
|
|
/// Enumeration of which the highest value will be returned
|
|
|
|
/// </typeparam>
|
|
|
|
/// <returns>The highest value in the enumeration</returns>
|
2013-04-25 13:11:59 +00:00
|
|
|
public static TEnumeration GetHighestValue<TEnumeration>()
|
|
|
|
where TEnumeration : IComparable {
|
|
|
|
TEnumeration[] values = GetValues<TEnumeration>();
|
2009-12-01 19:17:31 +00:00
|
|
|
|
|
|
|
// If the enumeration is empty, return nothing
|
|
|
|
if(values.Length == 0) {
|
2013-04-25 13:11:59 +00:00
|
|
|
return default(TEnumeration);
|
2009-12-01 19:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the highest value in the enumeration. We initialize the highest value
|
|
|
|
// to the first enumeration value so we don't have to use some arbitrary starting
|
|
|
|
// value which might actually appear in the enumeration.
|
2013-04-25 13:11:59 +00:00
|
|
|
TEnumeration highestValue = values[0];
|
2009-12-01 19:17:31 +00:00
|
|
|
for(int index = 1; index < values.Length; ++index) {
|
|
|
|
if(values[index].CompareTo(highestValue) > 0) {
|
|
|
|
highestValue = values[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return highestValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Returns the lowest value encountered in an enumeration</summary>
|
2013-04-25 13:11:59 +00:00
|
|
|
/// <typeparam name="TEnumeration">
|
2009-12-01 19:17:31 +00:00
|
|
|
/// Enumeration of which the lowest value will be returned
|
|
|
|
/// </typeparam>
|
|
|
|
/// <returns>The lowest value in the enumeration</returns>
|
2013-04-25 13:11:59 +00:00
|
|
|
public static TEnumeration GetLowestValue<TEnumeration>()
|
|
|
|
where TEnumeration : IComparable {
|
|
|
|
TEnumeration[] values = GetValues<TEnumeration>();
|
2009-12-01 19:17:31 +00:00
|
|
|
|
|
|
|
// If the enumeration is empty, return nothing
|
|
|
|
if(values.Length == 0) {
|
2013-04-25 13:11:59 +00:00
|
|
|
return default(TEnumeration);
|
2009-12-01 19:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the lowest value in the enumeration. We initialize the lowest value
|
|
|
|
// to the first enumeration value so we don't have to use some arbitrary starting
|
|
|
|
// value which might actually appear in the enumeration.
|
2013-04-25 13:11:59 +00:00
|
|
|
TEnumeration lowestValue = values[0];
|
2009-12-01 19:17:31 +00:00
|
|
|
for(int index = 1; index < values.Length; ++index) {
|
2010-01-20 20:21:23 +00:00
|
|
|
if(values[index].CompareTo(lowestValue) < 0) {
|
|
|
|
lowestValue = values[index];
|
2009-12-01 19:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-20 20:21:23 +00:00
|
|
|
return lowestValue;
|
2009-12-01 19:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Retrieves a list of all values contained in an enumeration</summary>
|
2012-03-03 12:45:49 +00:00
|
|
|
/// <typeparam name="TEnum">
|
2009-12-01 19:17:31 +00:00
|
|
|
/// Type of the enumeration whose values will be returned
|
|
|
|
/// </typeparam>
|
|
|
|
/// <returns>All values contained in the specified enumeration</returns>
|
2010-01-20 20:21:23 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// This method produces collectable garbage so it's best to only call it once
|
|
|
|
/// and cache the result.
|
|
|
|
/// </remarks>
|
2012-03-03 12:45:49 +00:00
|
|
|
public static TEnum[] GetValues<TEnum>() {
|
2010-09-17 15:29:22 +00:00
|
|
|
#if XBOX360 || WINDOWS_PHONE
|
2012-03-03 12:45:49 +00:00
|
|
|
return GetValuesXbox360<TEnum>();
|
2009-12-01 19:17:31 +00:00
|
|
|
#else
|
2012-03-03 12:45:49 +00:00
|
|
|
return (TEnum[])Enum.GetValues(typeof(TEnum));
|
2009-12-01 19:17:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Retrieves a list of all values contained in an enumeration</summary>
|
2012-03-03 12:45:49 +00:00
|
|
|
/// <typeparam name="TEnum">
|
2009-12-01 19:17:31 +00:00
|
|
|
/// Type of the enumeration whose values will be returned
|
|
|
|
/// </typeparam>
|
|
|
|
/// <returns>All values contained in the specified enumeration</returns>
|
2012-03-03 12:45:49 +00:00
|
|
|
internal static TEnum[] GetValuesXbox360<TEnum>() {
|
|
|
|
Type enumType = typeof(TEnum);
|
2009-12-01 19:17:31 +00:00
|
|
|
if(!enumType.IsEnum) {
|
|
|
|
throw new ArgumentException(
|
|
|
|
"The provided type needs to be an enumeration", "EnumType"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use reflection to get all fields in the enumeration
|
|
|
|
FieldInfo[] fieldInfos = enumType.GetFields(
|
|
|
|
BindingFlags.Public | BindingFlags.Static
|
|
|
|
);
|
|
|
|
|
2010-03-03 20:47:45 +00:00
|
|
|
// Create an array to hold the enumeration values and copy them over from
|
2009-12-01 19:17:31 +00:00
|
|
|
// the fields we just retrieved
|
2012-03-03 12:45:49 +00:00
|
|
|
TEnum[] values = new TEnum[fieldInfos.Length];
|
2009-12-01 19:17:31 +00:00
|
|
|
for(int index = 0; index < fieldInfos.Length; ++index) {
|
2012-03-03 12:45:49 +00:00
|
|
|
values[index] = (TEnum)fieldInfos[index].GetValue(null);
|
2009-12-01 19:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support
|