static Menu() { LanguageTag = EnumCache <Language> .Description(default(Language)); if (!Platform.HasRenderAPI) { return; } if (Platform.HasCoreAPI) { // Game.OnStart += Subscribe; } //else { Subscribe(null); } void Subscribe(EventArgs args) { Render.OnEndScene += OnEndScene; if (Platform.HasUserInputAPI) { UserInput.OnWndProc += OnWndProc; } } }
private static void CheckNullableValue <T>(T value) where T : struct { Check.That(EnumCache.ToUInt64Slow((Enum)(object)value)).IsEqualTo(EnumCache.ToUInt64(value)); Check.That(EnumCache.ToUInt64Nullable((T?)value)).IsEqualTo(EnumCache.ToUInt64(value)); Check.That(GetString(value)).IsEqualTo(value.ToString()); }
public void 方法关键字转换属性关键字() { MethodKeyword methodKeyword = MethodKeyword.Abstract; PropertyKeyword propertyKeyword = EnumCache.ToPropertyKeyword(methodKeyword); Assert.Equal(PropertyKeyword.Abstract, propertyKeyword); }
public void StringValuesAreShared() { var stringValues = EnumCache.GetStringValues <TestEnum, int>(); var nullableStringValues = EnumCache.GetStringValues <TestEnum?, int>(); Assert.Same(stringValues, nullableStringValues); }
public void Issue598_EnumsWithRepeatedValuesAsText() { var info = EnumCache.GetInfo <TestEnumWithRepeatsAsText> (); Assert.IsTrue(info.IsEnum); Assert.IsTrue(info.StoreAsText); }
public void ShouldReturnFalseForClass() { var info = EnumCache.GetInfo <TestClassThusNotEnum>(); Assert.IsFalse(info.IsEnum); Assert.IsFalse(info.StoreAsText); }
private static EnumCache GetCache(Type type) { EnumCache cache; if (EnumsCache.TryGetValue(type, out cache)) { return(cache); } cache = new EnumCache(); foreach (FieldInfo fieldInfo in type.GetFields()) { DescriptionAttribute attribute = fieldInfo.GetCustomAttribute <DescriptionAttribute>(); if (null == attribute) { continue; } if (cache.StringToEnum.ContainsKey(attribute.Description)) { throw new Exception($"Enum \"{type.Name}\" has more than 1 element with equal description \"{attribute.Description}\""); } object enumValue = fieldInfo.GetValue(null); cache.StringToEnum.Add(attribute.Description, enumValue); cache.EnumToString.Add(enumValue, attribute.Description); } EnumsCache.TryAdd(type, cache); return(cache); }
public void ShouldReturnTrueForByteEnumStoreAsInt() { var info = EnumCache.GetInfo <TestByteEnumStoreAsInt>(); Assert.IsTrue(info.IsEnum); Assert.IsFalse(info.StoreAsText); }
/// <summary> /// 解析给定的数字字符串,并返回对应的值。 /// </summary> /// <param name="str">要解析的数字字符串。</param> /// <param name="cache">枚举的缓存。</param> /// <param name="comparison">字符串比较。</param> /// <param name="value">字符串对应的值。</param> /// <returns>数字字符串的解析是否成功。</returns> private static bool TryParseString(string str, EnumCache cache, StringComparison comparison, ref ulong value) { Contract.Requires(str != null && cache != null); // 比较常数值的名称。 for (int i = 0; i < cache.Names.Length; i++) { if (string.Equals(str, cache.Names[i], comparison)) { value = cache.Values[i]; return(true); } } if (!cache.HasDescription) { return(false); } // 比较常数值的描述信息。 for (int i = 0; i < cache.Descriptions.Length; i++) { if (string.Equals(str, cache.Descriptions[i], comparison)) { value = cache.Values[i]; return(true); } } return(false); }
public void EnumValuesAreShared() { var enumValues = EnumCache.GetEnumValues <TestEnum, int>(); var nullableEnumValues = EnumCache.GetEnumValues <TestEnum?, int>(); Assert.Same(enumValues, nullableEnumValues); }
public void ShouldReturnTrueForEnumStoreAsInt() { var info = EnumCache.GetInfo <TestEnumStoreAsInt>(); Assert.IsTrue(info.IsEnum); Assert.IsFalse(info.StoreAsText); Assert.IsNull(info.EnumValues); }
public void Issue598_EnumsWithRepeatedValues() { var info = EnumCache.GetInfo <TestEnumWithRepeats> (); Assert.IsTrue(info.IsEnum); Assert.IsFalse(info.StoreAsText); Assert.IsNull(info.EnumValues); }
public static bool HasEnumVal <T>(this string description) where T : struct { Debug.Assert(typeof(T).IsEnum); EnumCache cache = GetCache(typeof(T)); return(null != cache.StringToEnum.TryGetValue(description)); }
/// <summary> /// 将一个或多个枚举常数的名称、描述或数字值的字符串表示转换成等效的枚举值。 /// 一个参数指定该操作是否区分大小写。 /// </summary> /// <param name="enumType">枚举类型。</param> /// <param name="value">包含要转换的值或名称的字符串。</param> /// <param name="ignoreCase">若要忽略大小写则为 <c>true</c>;否则为 <c>false</c>。</param> /// <returns><see cref="ulong"/> 表示的枚举值。</returns> /// <exception cref="ArgumentException"><paramref name="value"/> 是空字符串 ("") 或只包含空白。</exception> /// <exception cref="ArgumentException"><paramref name="value"/> 是一个名称,但不是为该枚举定义的命名常量之一。 /// </exception> /// <exception cref="OverflowException"><paramref name="value"/> 超出 <paramref name="enumType"/> /// 基础类型的范围。</exception> private static ulong ParseToULong(Type enumType, string value, bool ignoreCase) { Contract.Requires(enumType != null && enumType.IsEnum); Contract.Requires(value != null); value = value.Trim(); if (value.Length == 0) { throw CommonExceptions.MustContainValidInfo("value"); } // 尝试对数字进行解析,这样可避免之后的字符串比较。 ulong tmpValue; if (TryParseString(value, out tmpValue)) { return(tmpValue); } // 尝试对描述信息进行解析。 EnumCache cache = GetEnumCache(enumType); StringComparison comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; ulong ulValue = 0UL; int start = 0; do { // 去除前导空白。 while (char.IsWhiteSpace(value, start)) { start++; } int idx = value.IndexOf(',', start); if (idx < 0) { idx = value.Length; } if (idx == start) { start = idx + 1; continue; } int nIdx = idx - 1; // 去除后面的空白。 while (char.IsWhiteSpace(value, nIdx)) { nIdx--; } Contract.Assume(nIdx >= start); string str = value.Substring(start, nIdx - start + 1); start = idx + 1; // 尝试识别为名称、描述或数字。 if (!TryParseString(str, cache, comparison, ref tmpValue) && !TryParseString(str, out tmpValue)) { throw CommonExceptions.EnumValueNotFound(enumType, str); } ulValue |= tmpValue; } while (start < value.Length); return(ulValue); }
private UiArchiveExtension GetArchiveExtension(ArchiveEntry indices) { string ext = PathEx.GetMultiDotComparableExtension(indices.Name); const string extensionPrefix = ".win32."; ext = ext.Substring(extensionPrefix.Length); return(EnumCache <UiArchiveExtension> .Parse(ext)); }
public string ToTypeName(ISqlFormater formater) { var ef = EnumCache.Get <DataFormat>(); var format = formater.ToDataFormat(DataType); var ei = ef.Get(format.ToStringX()); var type = ei.Description; return(type); }
public void 属性关键字() { PropertyKeyword propertyKeyword = PropertyKeyword.Abstract; var output = EnumCache.GetPropertyKeyword(propertyKeyword); Assert.Equal("abstract", output); output = EnumCache.GetValue(propertyKeyword); Assert.Equal("abstract", output); }
public void 方法关键字() { MethodKeyword methodKeyword = MethodKeyword.Abstract; var output = EnumCache.GetMethodKeyword(methodKeyword); Assert.Equal("abstract", output); output = EnumCache.GetValue(methodKeyword); Assert.Equal("abstract", output); }
public void 命名空间访问修饰符() { NamespaceAccess access = NamespaceAccess.Internal; var output = EnumCache.GetNamespaceAccess(access); Assert.Equal("internal", output); output = EnumCache.GetValue(access); Assert.Equal("internal", output); }
public void 成员类型() { MemberType type = MemberType.BaseType; var output = EnumCache.GetMemberType(type); Assert.Equal("BaseType", output); output = EnumCache.GetValue(type); Assert.Equal("BaseType", output); }
public void 字段关键字() { FieldKeyword fieldKeyword = FieldKeyword.StaticReadonly; var output = EnumCache.GetFieldKeyword(fieldKeyword); Assert.Equal("static readonly", output); output = EnumCache.GetValue(fieldKeyword); Assert.Equal("static readonly", output); }
public void 成员访问修饰符() { MemberAccess access = MemberAccess.Protected; var output = EnumCache.GetMemberAccess(access); Assert.Equal("protected", output); output = EnumCache.GetValue(access); Assert.Equal("protected", output); }
public void 泛型关键字() { GenericKeyword genericKeyword = GenericKeyword.Unmanaged; var output = EnumCache.GetGenericKeyword(genericKeyword); Assert.Equal("unmanaged", output); output = EnumCache.GetValue(genericKeyword); Assert.Equal("unmanaged", output); }
public void 事件关键字() { EventKeyword eventKeyword = EventKeyword.Virtual; var output = EnumCache.GetEventKeyword(eventKeyword); Assert.Equal("virtual", output); output = EnumCache.GetValue(eventKeyword); Assert.Equal("virtual", output); }
public void 结构体关键字() { StructKeyword structKeyword = StructKeyword.ReadonlyRef; var output = EnumCache.GetStructKword(structKeyword); Assert.Equal("readonly ref", output); output = EnumCache.GetValue(structKeyword); Assert.Equal("readonly ref", output); }
public void 类关键字() { ClassKeyword classKeyword = ClassKeyword.Abstract; var output = EnumCache.GetClassKeyword(classKeyword); Assert.Equal("abstract", output); output = EnumCache.GetValue(classKeyword); Assert.Equal("abstract", output); }
public string GetRelativePath() { if (!string.IsNullOrEmpty(PS3KnownPath)) { return(PS3KnownPath); } string folder = EnumCache <FFXFileSignatures> .IsDefined(Signature) ? Signature.ToString().ToUpper() : "Unknown"; return(Path.Combine(folder, string.IsNullOrEmpty(PS2KnownName) ? GetFileName() : PS2KnownName + '.' + folder.ToLower())); }
public static IsoTableEntryInfo TryParse(string filePath) { string name = Path.GetFileNameWithoutExtension(filePath); if (name[0] != 'F' || name[6] != '_') { return(null); } int index = int.Parse(name.Substring(1, 5)); int defectiveIndex = int.Parse(name.Substring(7, 5)); bool implicitCompressed = false; IsoTableEntryFlags flags = IsoTableEntryFlags.None; for (int i = 12; i < name.Length; i++) { switch (name[i]) { case 'I': implicitCompressed = true; break; case 'C': flags |= IsoTableEntryFlags.Compressed; break; case 'D': flags |= IsoTableEntryFlags.Dummy; break; } } FFXFileSignatures signature = 0; string ext = Path.GetExtension(filePath); if (!string.IsNullOrEmpty(ext)) { FFXFileSignatures?tag = EnumCache <FFXFileSignatures> .TryParse(ext.Substring(1)); if (tag == null) { return(null); } signature = tag.Value; } return(new IsoTableEntryInfo(index, defectiveIndex, -1, -1, flags) { IsCompressed = implicitCompressed, Signature = signature, UncompressedSize = new FileInfo(filePath).Length, }); }
internal static T ConsumeEnum <T>(this Peekable <Token> tokens) where T : struct { var token = tokens.Read(); var value = tokens.ConsumeString(); if (!EnumCache <T> .TryGet(token.Value, out T val)) { token.Throw(ErrorCode.InvalidEnum, "Unable to parse " + typeof(T).Name); } return(val); }
/// <summary> /// 获取属性修饰符 /// </summary> /// <param name="info"></param> /// <returns></returns> public static PropertyKeyword GetPropertyKeyword(PropertyInfo info) { MethodInfo method = info.GetGetMethod(); if (method == null) { method = info.GetSetMethod() ?? throw new NullReferenceException("无法获取属性的信息"); } return(EnumCache.ToPropertyKeyword(GetMethodKeyword(method))); }