📄 enumex.cs
字号:
//==========================================================================================
//
// OpenNETCF.EnumEx
// Copyright (c) 2003-2006, OpenNETCF.org
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the OpenNETCF.org Shared Source License.
//
// 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 OpenNETCF.org Shared Source License
// for more details.
//
// You should have received a copy of the OpenNETCF.org Shared Source License
// along with this library; if not, email licensing@opennetcf.org to request a copy.
//
// If you wish to contact the OpenNETCF Advisory Board to discuss licensing, please
// email licensing@opennetcf.org.
//
// For general enquiries, email enquiries@opennetcf.org or visit our website at:
// http://www.opennetcf.org
//
//==========================================================================================
using System;
using System.Globalization;
using System.Collections;
using System.Reflection;
// Peter Foot
// Sergey Bogdanov
namespace OpenNETCF
{
/// <summary>
/// Provides helper functions for Enumerations.
/// </summary>
/// <remarks>Extends the <see cref="T:System.Enum">System.Enum Class</see>.</remarks>
/// <seealso cref="T:System.Enum">System.Enum Class</seealso>
public sealed class EnumEx
{
private EnumEx(){}
#region Get Name
/// <summary>
/// Retrieves the name of the constant in the specified enumeration that has the specified value.
/// </summary>
/// <param name="enumType">An enumeration type.</param>
/// <param name="value">The value of a particular enumerated constant in terms of its underlying type.</param>
/// <returns> A string containing the name of the enumerated constant in enumType whose value is value, or null if no such constant is found.</returns>
/// <exception cref="System.ArgumentException"> enumType is not an System.Enum. -or- value is neither of type enumType nor does it have the same underlying type as enumType.</exception>
/// <example>The following code sample illustrates the use of GetName (Based on the example provided with desktop .NET Framework):
/// <code>[Visual Basic]
/// Imports System
///
/// Public Class GetNameTest
///
/// Enum Colors
/// Red
/// Green
/// Blue
/// Yellow
/// End Enum 'Colors
///
/// Enum Styles
/// Plaid
/// Striped
/// Tartan
/// Corduroy
/// End Enum 'Styles
///
/// Public Shared Sub Main()
/// MessageBox.Show("The 4th value of the Colors Enum is " + [OpenNETCF.Enum].GetName(GetType(Colors), 3))
/// MessageBox.Show("The 4th value of the Styles Enum is " + [OpenNETCF.Enum].GetName(GetType(Styles), 3))
/// End Sub 'Main
///
/// End Class 'GetNameTest</code>
/// <code>[C#]
/// using System;
///
/// public class GetNameTest
/// {
/// enum Colors { Red, Green, Blue, Yellow };
/// enum Styles { Plaid, Striped, Tartan, Corduroy };
///
/// public static void Main()
/// {
/// MessageBox.Show("The 4th value of the Colors Enum is " + OpenNETCF.Enum.GetName(typeof(Colors), 3));
/// MessageBox.Show("The 4th value of the Styles Enum is " + OpenNETCF.Enum.GetName(typeof(Styles), 3));
/// }
/// }</code>
/// </example>
/// <seealso cref="M:System.Enum.GetName(System.Type,System.Object)">System.Enum.GetName Method</seealso>
public static string GetName(Type enumType, object value)
{
//check that the type supplied inherits from System.Enum
if(enumType.BaseType==Type.GetType("System.Enum"))
{
//get details of all the public static fields (enum items)
FieldInfo[] fi = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
//cycle through the enum values
foreach(FieldInfo thisField in fi)
{
object numericValue = 0;
try
{
//convert the enum value to the numeric type supplied
numericValue = Convert.ChangeType(thisField.GetValue(null), value.GetType(), null);
}
catch
{
throw new ArgumentException();
}
//if value matches return the name
if(numericValue.Equals(value))
{
return thisField.Name;
}
}
//if there is no match return null
return null;
}
else
{
//the type supplied does not derive from enum
throw new ArgumentException("enumType parameter is not an System.Enum");
}
}
#endregion
#region Get Names
/// <summary>
/// Retrieves an array of the names of the constants in a specified enumeration.
/// </summary>
/// <param name="enumType">An enumeration type.</param>
/// <returns>A string array of the names of the constants in enumType. The elements of the array are sorted by the values of the enumerated constants.</returns>
/// <exception cref="System.ArgumentException">enumType parameter is not an System.Enum</exception>
/// <example>The follow example shows how to enumerate the members of the System.DayOfWeek enumeration by adding them to a ComboBox:-
/// <code>[Visual Basic]
/// Dim thisDOW As New DayOfWeek
/// For Each thisDOW In OpenNETCF.Enum.GetValues(Type.GetType("System.DayOfWeek"))
/// ComboBox1.Items.Add(thisDOW)
/// Next</code>
/// <code>[C#]
/// foreach(DayOfWeek thisdow in OpenNETCF.Enum.GetValues(typeof(DayOfWeek)))
/// {
/// comboBox1.Items.Add(thisdow);
/// }</code></example>
/// <seealso cref="M:System.Enum.GetNames(System.Type)">System.Enum.GetNames Method</seealso>
public static string[] GetNames(Type enumType)
{
if(enumType.BaseType==Type.GetType("System.Enum"))
{
//get the public static fields (members of the enum)
System.Reflection.FieldInfo[] fi = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
//create a new enum array
string[] names = new string[fi.Length];
//populate with the values
for(int iEnum = 0; iEnum < fi.Length; iEnum++)
{
names[iEnum] = fi[iEnum].Name;
}
//return the array
return names;
}
else
{
//the type supplied does not derive from enum
throw new ArgumentException("enumType parameter is not an System.Enum");
}
}
#endregion
#region Get Underlying Type
/// <summary>
/// Returns the underlying type of the specified enumeration.
/// <para><b>New in v1.1</b></para>
/// </summary>
/// <param name="enumType">An enumeration type.</param>
/// <returns>The underlying <see cref="System.Type"/> of <paramref>enumType</paramref>.</returns>
/// <seealso cref="M:System.Enum.GetUnderlyingType(System.Type)">System.Enum.GetUnderlyingType Method</seealso>
public static Type GetUnderlyingType(Type enumType)
{
return System.Enum.GetUnderlyingType(enumType);
}
#endregion
#region Get Values
/// <summary>
/// Retrieves an array of the values of the constants in a specified enumeration.
/// </summary>
/// <param name="enumType">An enumeration type.</param>
/// <returns>An System.Array of the values of the constants in enumType. The elements of the array are sorted by the values of the enumeration constants.</returns>
/// <exception cref="System.ArgumentException">enumType parameter is not an System.Enum</exception>
/// <seealso cref="M:System.Enum.GetValues(System.Type)">System.Enum.GetValues Method</seealso>
public static System.Enum[] GetValues(Type enumType)
{
if(enumType.BaseType==Type.GetType("System.Enum"))
{
//get the public static fields (members of the enum)
System.Reflection.FieldInfo[] fi = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
//create a new enum array
System.Enum[] values = new System.Enum[fi.Length];
//populate with the values
for(int iEnum = 0; iEnum < fi.Length; iEnum++)
{
values[iEnum] = (System.Enum)fi[iEnum].GetValue(null);
}
//return the array
return values;
}
else
{
//the type supplied does not derive from enum
throw new ArgumentException("enumType parameter is not an System.Enum");
}
}
#endregion
#region Is Defined
/// <summary>
/// Returns an indication whether a constant with a specified value exists in a specified enumeration.
/// <para><b>New in v1.1</b></para>
/// </summary>
/// <param name="enumType">An enumeration type.</param>
/// <param name="value">The value or name of a constant in enumType.</param>
/// <returns><b>true</b> if a constant in <paramref>enumType</paramref> has a value equal to value; otherwise, <b>false</b>.</returns>
/// <seealso cref="M:System.Enum.IsDefined(System.Type,System.Object)">System.Enum.IsDefined Method</seealso>
public static bool IsDefined(Type enumType, object value)
{
return System.Enum.IsDefined(enumType, value);
}
#endregion
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -