public void Add(Enum e) { int val = (int)Convert.ChangeType(e, e.GetTypeCode()); string t = e.GetType().Name; if (!enums.ContainsKey(t)) enums.Add(t, new SortedList<int, int>()); SortedList<int, int> values = enums[t]; values.Add(val, ++uniqValue); }
public int Lookup(Enum e) { int val = (int)Convert.ChangeType(e, e.GetTypeCode()); string t = e.GetType().Name; if (!enums.ContainsKey(t)) return -1; if (!enums[t].ContainsKey(val)) return -1; return enums[t][val]; }
private static void EvaluateEnum(Enum e) { Console.WriteLine(String.Format("Information about {0}", e.GetType().Name)); Console.WriteLine(String.Format("Underlying type: {0}", e.GetTypeCode())); var enumData = Enum.GetValues(e.GetType()); foreach (var item in enumData) { Console.WriteLine(String.Format("Name: {0} Value: {0:d} ", item)); } Console.WriteLine(); }
static StackObject *GetTypeCode_0(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject *ptr_of_this_method; StackObject *__ret = ILIntepreter.Minus(__esp, 1); ptr_of_this_method = ILIntepreter.Minus(__esp, 1); System.Enum instance_of_this_method = (System.Enum) typeof(System.Enum).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); var result_of_this_method = instance_of_this_method.GetTypeCode(); return(ILIntepreter.PushObject(__ret, __mStack, result_of_this_method)); }
static int GetTypeCode(IntPtr L) { try { ToLua.CheckArgsCount(L, 1); System.Enum obj = (System.Enum)ToLua.CheckObject <System.Enum>(L, 1); System.TypeCode o = obj.GetTypeCode(); LuaDLL.lua_pushinteger(L, (int)o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int GetTypeCode(IntPtr L) { try { ToLua.CheckArgsCount(L, 1); System.Enum obj = (System.Enum)ToLua.CheckObject(L, 1, typeof(System.Enum)); System.TypeCode o = obj.GetTypeCode(); ToLua.Push(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
/// <summary> /// Converte Enum em inteiro /// Caso não seja valida a conversao retorna -1 /// </summary> /// <param name="value">System.Enum</param> /// <returns>int</returns> public static int ToInt(this System.Enum value) { int aux; if (value == null) { aux = -1; } else { var valor = Convert.ChangeType(value, value.GetTypeCode()); if (valor == null || !int.TryParse(valor.ToString(), out aux)) { aux = -1; } } return(aux); }
/// <summary> /// Converte Enum em Byte /// Caso não seja valida a conversao retorna 0 /// </summary> /// <param name="value">System.Enum</param> /// <returns>int</returns> public static byte ToByte(this System.Enum value) { byte aux; if (value == null) { aux = 0; } else { var valor = Convert.ChangeType(value, value.GetTypeCode()); if (valor == null || !byte.TryParse(valor.ToString(), out aux)) { aux = 0; } } return(aux); }
/// <summary> /// Aborts the current request processing /// </summary> protected void AbortRequest(System.Enum responseCode) { if (responseCode == null) { throw new ArgumentNullException("HttpApplication", InternalFunctions.GetResourceString("ArgumentNullException", "ResponseCode")); } Type _enumType = responseCode.GetType(); if (responseCode.GetTypeCode() == TypeCode.Int32) { AbortRequest((int)System.Enum.Parse(_enumType, responseCode.ToString(), true)); } else { throw new WebDavException(InternalFunctions.GetResourceString("InvalidEnumIntType")); } }
static int GetTypeCode(IntPtr L) { ToLua.CheckArgsCount(L, 1); System.Enum obj = (System.Enum)ToLua.CheckObject(L, 1, typeof(System.Enum)); System.TypeCode o; try { o = obj.GetTypeCode(); } catch (Exception e) { return(LuaDLL.luaL_error(L, e.Message)); } ToLua.Push(L, o); return(1); }
/// <summary> /// returns an instance of the specified asset type /// </summary> /// <param name="e"></param> /// <param name="owningClientId"></param> /// <returns></returns> public Gobject GetNewInstance(Enum e, int owningClientId) { int id = (int)Convert.ChangeType(e, e.GetTypeCode()); return GetNewInstance(id, owningClientId); }
/// <summary> /// Determines if the enumeration value contains a combination of valid flags. /// </summary> /// <param name="value">A bit combination of constant values.</param> /// <param name="enumType">An enumeration type.</param> /// <returns>true if all flags are defined in the enumeration; otherwise, false.</returns> private static bool AreFlagsDefined(this Enum value, Type enumType) { //get the enumeration type code describing underlying type: TypeCode code = value.GetTypeCode(); switch (code) { case TypeCode.Boolean: case TypeCode.Char: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: //validate unsigned value { ulong flags = Convert.ToUInt64(value); //convert to 64-bit unsigned integer ulong flag; //iterate enumeration constants: foreach (object obj in Enum.GetValues(enumType)) { flag = Convert.ToUInt64(obj); //get flag constants if (flag == flags) //last flag { return(true); } //remove bits: flags &= ~flag; } } break; case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: //validate signed value { long flags = Convert.ToInt64(value); //convert to 64-bit signed integer if (flags < 0) //validate the flags are greater-than or equal to zero { throw new ArgumentOutOfRangeException(nameof(value), $"The specified System.Enum value of '{flags}' and must be greater-than or equal to zero."); } long flag; var enumValues = Enum.GetValues(enumType); //iterate enumeration constants: foreach (object obj in enumValues) { flag = Convert.ToInt64(obj); //get flag constants if (flag < 0) //validate the flag is greater-than or equal to zero { throw new InvalidOperationException($"Cannot validate System.Enum '{enumType}', TypeCode: '{code}' with value {obj} that is less-than zero."); } } //iterate enumeration constants: foreach (object obj in enumValues) { flag = Convert.ToInt64(obj); //get flag constants if (flag == flags) //last flag { return(true); } //remove bits: flags &= ~flag; } } break; default: throw new ArgumentException($"Cannot validate System.Enum '{enumType}' with TypeCode: '{code}'.", "value"); } return(false); }
public static bool ValidateEnumType(Enum enumToValidate) { if (enumToValidate.GetTypeCode() != TypeCode.Int32) throw new Exception("Invalid Enum Type"); return true; }
/// <summary> /// Internal function to validate the enumerator is of type Int32 /// </summary> /// <param name="enumToValidate"></param> /// <exception cref="WebDavException">Throw exception if the enumToValidate value is not a valid Int32</exception> internal static bool ValidateEnumType(Enum enumToValidate) { if (enumToValidate.GetTypeCode() != TypeCode.Int32) throw new WebDavException(InternalFunctions.GetResourceString("InvalidEnumIntType")); return true; }
/// <summary> /// Adds an asset /// </summary> /// <param name="name"></param> /// <param name="CreateCallback"></param> /// <param name="scale"></param> public void AddAsset(Enum e, GetGobjectDelegate CreateCallback, Vector3 scale) { int id = (int)Convert.ChangeType(e, e.GetTypeCode()); AddAsset(new Asset(id, CreateCallback, scale)); }
public static void GetTypeCode(Enum e, TypeCode expected) { Assert.Equal(expected, e.GetTypeCode()); }
public static int GetNumberValue(this System.Enum value) { return(Convert.ToInt32(Convert.ChangeType(value, value.GetTypeCode()))); }
public static string GetStringValue(this System.Enum value) { return(Convert.ToString(Convert.ChangeType(value, value.GetTypeCode()))); }