using System;
using System.Collections.Generic;
using System.Linq;
namespace xCommons.Helpers {
public partial class ObjectHelper {
///
/// Convert Enum Keys to Array
///
///
///
public static Array GetKeys () {
//
ValidateEnumbType ();
var type = typeof (T);
//
var result = Enum.GetNames (type);
//
return result;
}
///
/// Convert EnumValues to Array
///
///
///
public static Array GetValues () {
//
ValidateEnumbType ();
var type = typeof (T);
//
var result = Enum.GetValues (type);
//
return result;
}
///
/// Convert an Enum to IEnumerable of it's Childs
///
///
///
public static IEnumerable ToEnumerableValues () {
//
ValidateEnumbType ();
var type = typeof (T);
//
var result = GetValues ()
.Cast ();
//
return result;
}
///
/// Retrieve Keys of an Enumerable
///
///
///
public static IEnumerable ToEnumerableKeys () {
//
ValidateEnumbType ();
var type = typeof (T);
//
var result = GetKeys ()
.Cast ();
//
return result;
}
public static IDictionary ToDictionary () {
//
ValidateEnumbType ();
//
var result = new Dictionary ();
foreach (var name in Enum.GetNames (typeof (T))) {
result.Add (name, (int) Enum.Parse (typeof (T), name));
}
//
return result;
}
private static void ValidateEnumbType () {
//
var type = typeof (T);
if (!type.IsEnum) {
throw new InvalidCastException ();
}
}
}
}