/*----------Functions----------*/ //PUBLIC /// <summary> /// Initialise this object with the information needed for the event /// </summary> /// <param name="to">The state that the state machine has been changed to</param> /// <param name="toScreen">The screen object that will be displayed for the new state. This can be null if there was no screen assigned</param> /// <param name="from">The state the state machine was in previous to the change. This can be null for the first instance</param> /// <param name="fromScreen">The screen object that was used to display the screen previously. This can be null for the first instance or if no screen was assigned</param> public ScreenStateTransitionEventArgs(TEnum to, ScreenBase toScreen, TEnum?from, ScreenBase fromScreen) { ToState = to; ToScreen = toScreen; FromState = from; FromScreen = fromScreen; }
public static IEnumerable <TEnum> GetValues <TEnum>() where TEnum : struct, IConvertible { try { if (!typeof(TEnum).IsEnum) { throw new ArgumentException("Le type T doit être une énumération."); } IEnumerable <TEnum> items = new List <TEnum>(); foreach (Enum item in Enum.GetValues(typeof(TEnum))) { TEnum parsedEnum = (TEnum)(object)item; items = items.Concat <TEnum>(new[] { parsedEnum }); } return(items); } catch (Exception e) { throw; } }
/// <summary> /// Creates a new EnumConvertor from the given type /// </summary> /// <param name="enumType">Type of converter. Must be an enum type.</param> public EnumConverter(Type enumType) { if (enumType == null) { throw new ArgumentNullException(nameof(enumType), "Enum conversion type cannot be null"); } Array v = Enum.GetValues(enumType); List <string> tempNames = new List <string>(v.Length); List <TEnum> tempValues = new List <TEnum>(v.Length); for (int i = 0; i < v.Length; i++) { TEnum value = (TEnum)v.GetValue(i); string name = Enum.GetName(enumType, value); if (!string.IsNullOrEmpty(name) && !this.names.ContainsKey(value) && !this.values.ContainsKey(name)) { tempNames.Add(name); tempValues.Add(value); this.names.Add(value, name); this.values.Add(name, value); } } this.orderedNames = tempNames.ToArray(); this.orderedValues = tempValues.ToArray(); }
public Dictionary <string, TEnum> GetReverseMap <TEnum>() { object resultObj; if (!_reverseMapCache.TryGetValue(typeof(TEnum), out resultObj)) { Dictionary <string, string> dict; string type = typeof(TEnum).Name; if (!_strings.TryGetValue(type, out dict)) { return(null); } Dictionary <string, TEnum> result = new Dictionary <string, TEnum>(); foreach (KeyValuePair <string, string> pair in dict) { TEnum val = (TEnum)Enum.Parse(typeof(TEnum), pair.Key); result.Add(pair.Value, val); } _reverseMapCache[typeof(TEnum)] = resultObj = result; } return((Dictionary <string, TEnum>)resultObj); }
public static TEnum GenerateEnum <TEnum>() { Type enumType; if (IsNullable(typeof(TEnum))) { enumType = Nullable.GetUnderlyingType(typeof(TEnum)); } else { enumType = typeof(TEnum); } if (!enumType.IsEnum) { throw new Exception("Generic parameter must be an enum."); } List <TEnum> enumMembers = enumType.GetFields() .Where(f => f.IsLiteral) .Select(f => (TEnum)Enum.Parse(enumType, f.GetValue(null).ToString(), false)) .ToList(); TEnum randomEnum = enumMembers.OrderBy(x => Guid.NewGuid()).Take(1).FirstOrDefault(); return(randomEnum); }
/// <summary>Returns the highest value encountered in an enumeration</summary> /// <typeparam name="TEnum"> /// Enumeration of which the highest value will be returned /// </typeparam> /// <returns>The highest value in the enumeration</returns> public static TEnum GetHighestValue <TEnum>() where TEnum : IComparable { TEnum[] values = GetValues <TEnum>(); // If the enumeration is empty, return nothing if (values.Length == 0) { return(default(TEnum)); } // 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. TEnum highestValue = values[0]; for (int index = 1; index < values.Length; ++index) { if (values[index].CompareTo(highestValue) > 0) { highestValue = values[index]; } } return(highestValue); }
public PNGMapLayerReader(Bitmap bitmap, TMapLayerHeader header) { _bitmap = bitmap; _enum = TranslateToEnum(header); _colorDictionary = header.Colors.ToDictionary(col => col.Color, col => col.Type); _layerType = header.Type; }
public static IDictionary <TEnum, TArrtibute> GetEnumDict <TEnum, TArrtibute>(params TEnum[] rankEnumKeys) where TArrtibute : System.Attribute { if (rankEnumKeys == null) { throw new ArgumentNullException("rankEnumKeys"); } List <TEnum> list = rankEnumKeys.ToList <TEnum>(); Type typeFromHandle = typeof(TEnum); Dictionary <TEnum, TArrtibute> dictionary = new Dictionary <TEnum, TArrtibute>(); FieldInfo[] fields = typeFromHandle.GetFields(); for (int i = 0; i < fields.Length; i++) { FieldInfo fieldInfo = fields[i]; if (fieldInfo.FieldType.IsEnum) { TEnum tEnum = (TEnum)((object)fieldInfo.GetValue(typeFromHandle)); if (list.Contains(tEnum)) { object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(TArrtibute), false); if (customAttributes.Length != 0) { TArrtibute value = customAttributes[0] as TArrtibute; dictionary.Add(tEnum, value); } } } } return(dictionary); }
public DEnum(TEnum type, string value) { Type = type; StrValue = value; Value = type.DefineEnum.GetValueByNameOrAlias(value); }
internal static IEnumerable <ProtoEnumValue <TEnum> > GetEnumValues <TEnum>() { List <ProtoEnumValue <TEnum> > list = new List <ProtoEnumValue <TEnum> >(); foreach (FieldInfo enumField in typeof(TEnum).GetFields(BindingFlags.Static | BindingFlags.Public)) { if (!enumField.IsLiteral) { continue; } TEnum key = (TEnum)enumField.GetValue(null); ProtoEnumAttribute ea = AttributeUtils.GetAttribute <ProtoEnumAttribute>(enumField); int value; string name = (ea == null || string.IsNullOrEmpty(ea.Name)) ? enumField.Name : ea.Name; if (ea == null || !ea.HasValue()) { value = (int)Convert.ChangeType(key, typeof(int), CultureInfo.InvariantCulture); } else { value = (int)ea.Value; } list.Add(new ProtoEnumValue <TEnum>(key, value, name)); } list.Sort(delegate(ProtoEnumValue <TEnum> x, ProtoEnumValue <TEnum> y) { return(x.WireValue.CompareTo(y.WireValue)); }); return(list); }
public static TEnum GetRandomEnum <TEnum>() { Array enums = System.Enum.GetValues(typeof(TEnum)); TEnum value = (TEnum)enums.GetValue(UnityEngine.Random.Range(0, enums.Length)); return(value); }
public static List <TEnum> CreateEnumValueList <TEnum>() { Type enumType = typeof(TEnum); List <TEnum> result = new List <TEnum>(); #if NETSTANDARD || NETFX_CORE foreach (FieldInfo fieldInfo in enumType.GetTypeInfo().DeclaredFields) #else foreach (FieldInfo fieldInfo in enumType.GetFields()) #endif { if (enumType.Equals(fieldInfo.FieldType)) { TEnum item = (TEnum)Enum.Parse(enumType, fieldInfo.Name, true); result.Add(item); } } return(result); // IEnumerable<FieldInfo> fieldInfos // = enumType.GetFields().Where(x => enumType.Equals(x.FieldType)); // return fieldInfos.Select( // fieldInfo => (TEnum)Enum.Parse(enumType, fieldInfo.Name, true)).ToList(); }
/// <summary>Retrieves a list of all values contained in an enumeration</summary> /// <typeparam name="TEnum"> /// Type of the enumeration whose values will be returned /// </typeparam> /// <returns>All values contained in the specified enumeration</returns> internal static TEnum[] GetValuesXbox360 <TEnum>() { Type enumType = typeof(TEnum); 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 ); // Create an array to hold the enumeration values and copy them over from // the fields we just retrieved TEnum[] values = new TEnum[fieldInfos.Length]; for (int index = 0; index < fieldInfos.Length; ++index) { values[index] = (TEnum)fieldInfos[index].GetValue(null); } return(values); }
public void Init(TEnum type, TTarget target, Objectiver <TEnum, TUnit, TTarget> objectiver) { Objectiver = objectiver; Target = target; Type = type; EnumIndex = Enum <TEnum> .Int(type); }
public void TestInputOutput() { int width = 100; int height = 1000; int[] values = new int[width * height]; int[] values2 = new int[width * height]; values[70] = 1; values[1000] = 2; values[0] = 3; values2[4983] = 1; values2[444] = 2; TEnum lenum1 = new TEnum(new string[] { "a", "b", "c", "d" }); TEnum lenum2 = new TEnum(new string[] { "x", "y", "z" }); var map = new TMap() { Name = "test", BiomeLayer = new TMapLayer(width, height, values, lenum1, DefinitionType.Biome), ResourceLayers = new TMapLayer[] { new TMapLayer(width, height, values2, lenum2, DefinitionType.Resource) } }; using (var stream = new MemoryStream()) { MapIO.Export(map, stream, true); stream.Seek(0, SeekOrigin.Begin); TMap result = MapIO.Import(stream); Assert.Equal(map.Name, result.Name); } }
public TComponent Create(TEnum type, Vector3 position, Quaternion rotation, Transform parentTransform = null) { var transf = _container.InstantiatePrefab(_prefubs[type], position, rotation, parentTransform) .GetComponent <TComponent>(); return(transf); }
// THANKSTO: Maciej Hehl, https://stackoverflow.com/a/2709523 static int GetNumberOfSetBits(TEnum value) { var i = ToUInt64(value); i -= (i >> 1) & 0x5555555555555555UL; i = (i & 0x3333333333333333UL) + ((i >> 2) & 0x3333333333333333UL); return((int)(unchecked (((i + (i >> 4)) & 0xF0F0F0F0F0F0F0FUL) * 0x101010101010101UL) >> 56)); }
private static void WriteLayerEnum(BinaryWriter writer, TEnum layerEnum) { writer.Write(layerEnum.Length); for (int i = 0; i < layerEnum.Length; i++) { writer.Write(layerEnum.GetValue(i)); } }
public bool TryParseFlags(string value, bool ignoreCase, string delimiter, out TEnum result, EnumFormat[] formats) { TInt resultAsInt; var success = _cache.TryParseFlags(value, ignoreCase, delimiter, out resultAsInt, formats); result = ToEnum(resultAsInt); return(success); }
public bool TryToObject(ulong value, out TEnum result, EnumValidation validation) { TInt resultAsInt; var success = _cache.TryToObject(value, out resultAsInt, validation); result = ToEnum(resultAsInt); return(success); }
public override bool Equals(TEnum x, TEnum y) { if (equals == null) { Interlocked.CompareExchange(ref equals, GenerateEquals(), null); } return(equals.Invoke(x, y)); }
private EnumStringInfo Lookup(TEnum value) { if (!IsDefined(value, out var index)) { throw new ArgumentOutOfRangeException(nameof(value)); } return(_enumInfo ![index]);
public override int Compare(TEnum x, TEnum y) { if (compare == null) { Interlocked.CompareExchange(ref compare, GenerateCompare(), null); } return(compare.Invoke(x, y)); }
public override int GetHashCode(TEnum obj) { if (getHashCode == null) { Interlocked.CompareExchange(ref getHashCode, GenerateGetHashCode(), null); } return(getHashCode.Invoke(obj)); }
public TMapLayer(int width, int height, int[] values, TEnum layerEnum, DefinitionType type) { Width = width; Height = height; Values = values; LayerEnum = layerEnum; Type = type; }
protected void RaiseModelChanged(TEnum eventType, object eventValue) { var handler = ModelChanged; if (handler != null) { handler(eventType, eventValue); } }
public void ReturnsIEnumerableWithSingleValidValue() { SmartFlagTestEnum.TryFromValue(1, out var TEnum); var list = TEnum.ToList(); Assert.Single(TEnum); Assert.Equal("One", list[0].Name); }
/// <summary> /// Gets a valid enumeration item for provided <paramref name="key" /> if a valid item exists. /// </summary> /// <param name="key">The key to return an enumeration item for.</param> /// <param name="item">A valid instance of <typeparamref name="TEnum" />; otherwise <c>null</c>.</param> /// <returns><c>true</c> if a valid item with provided <paramref name="key" /> exists; <c>false</c> otherwise.</returns> public static bool TryGet([AllowNull] TKey key, [Nullable(2), NotNullWhen(true)] out TEnum item) { if ((object)key != null) { return(Enum <TEnum, TKey> .ItemsLookup.TryGetValue(key, out item)); } item = default(TEnum); return(false); }
public DType Accept(TEnum type, object converter, ExcelStream x, DefAssembly ass) { var d = x.Read(); if (CheckNull(type.IsNullable, d)) { return(null); } return(new DEnum(type, d.ToString().Trim())); }
private bool IsDefined(TEnum value, out int index) { if (_enumInfo == null) { throw new ArgumentOutOfRangeException(nameof(TEnum)); } index = Convert.ToInt32(value); return(index >= 0 && index < _enumInfo.Length); }
public TClass(TEnum type, string value) { this.Type = type; this.Value = value; }