For the complete documentation index, see llms.txt. This page is also available as Markdown.

Enum Utilities

EnumUtility and EnumEntry enumeration metadata tools.

EnumUtility reads the name, alias, description, and value of an enumeration item and returns it as a EnumEntry structure.

The following code is from the framework test, Gender is the test enumeration in Zongsoft.Tests, not Discussions.Models.Gender. The former's alias and description must be read together with the test definition.

EnumEntry

EnumEntry contains metadata for the enumeration items:

Field
Description

Enumeration type.

Name

Enumeration item name.

Value

Enumeration item value; you can select an enumeration value or a basic value.

Aliases

Enumeration item alias.

Description

Enumeration item description.

Read Enumeration Items

Source: framework/Zongsoft.Core/test/Common/EnumUtilityTest.cs (excerpt; see source for context).

EnumUtilityTest.cs
public void TestGetEnumEntry()
{
	var entry = EnumUtility.GetEnumEntry(Gender.Female);

	Assert.Equal("Female", entry.Name);
	Assert.Equal(Gender.Female, entry.Value); //注意:entry.Value 为枚举类型
	Assert.True(entry.HasAlias("F"));
	Assert.Equal("女士", entry.Description);
	Assert.Equal("女士", EnumUtility.GetEnumDescription(Gender.Female));

	entry = EnumUtility.GetEnumEntry(Gender.Male, true);

	Assert.Equal("Male", entry.Name);
	Assert.Equal((byte)1, entry.Value); //注意:entry.Value 为枚举项的基元类型
	Assert.True(entry.HasAlias("M"));
	Assert.Equal("男士", entry.Description);
	Assert.Equal("男士", EnumUtility.GetEnumDescription(Gender.Male));
}

GetEnumEntries can read the entire enumeration type, and can also append null value items to nullable enumerations.

Source: framework/Zongsoft.Core/test/Common/EnumUtilityTest.cs (excerpt; see source for context).

Last updated