Initial Commit ...
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace xCommons.Helpers {
|
||||
public partial class ObjectHelper {
|
||||
/// <summary>
|
||||
/// Convert Enum Keys to Array
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static Array GetKeys<T> () {
|
||||
//
|
||||
ValidateEnumbType<T> ();
|
||||
var type = typeof (T);
|
||||
|
||||
//
|
||||
var result = Enum.GetNames (type);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert EnumValues to Array
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static Array GetValues<T> () {
|
||||
//
|
||||
ValidateEnumbType<T> ();
|
||||
var type = typeof (T);
|
||||
|
||||
//
|
||||
var result = Enum.GetValues (type);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert an Enum to IEnumerable of it's Childs
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> ToEnumerableValues<T> () {
|
||||
//
|
||||
ValidateEnumbType<T> ();
|
||||
var type = typeof (T);
|
||||
|
||||
//
|
||||
var result = GetValues<T> ()
|
||||
.Cast<T> ();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Keys of an Enumerable
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<string> ToEnumerableKeys<T> () {
|
||||
//
|
||||
ValidateEnumbType<T> ();
|
||||
var type = typeof (T);
|
||||
|
||||
//
|
||||
var result = GetKeys<T> ()
|
||||
.Cast<string> ();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public static IDictionary<string, int> ToDictionary<T> () {
|
||||
//
|
||||
ValidateEnumbType<T> ();
|
||||
|
||||
//
|
||||
var result = new Dictionary<string, int> ();
|
||||
foreach (var name in Enum.GetNames (typeof (T))) {
|
||||
result.Add (name, (int) Enum.Parse (typeof (T), name));
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void ValidateEnumbType<T> () {
|
||||
//
|
||||
var type = typeof (T);
|
||||
if (!type.IsEnum) {
|
||||
throw new InvalidCastException ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user