4.5 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T11:30:16.984345+00:00 | zai-org/GLM-5-FP8 | 1 | accee265bcf5788d |
Documentation: EnumDescriptionTypeConverterShared
1. Purpose
EnumDescriptionTypeConverterShared is a type converter that enables displaying human-readable descriptions for enumeration values instead of their raw string names. It extends System.ComponentModel.EnumConverter and is designed for UI data binding scenarios (particularly WPF). When an enum value has a System.ComponentModel.DescriptionAttribute, this converter extracts that description and optionally localizes it via the StringResources resource manager before returning it for display.
2. Public Interface
EnumDescriptionTypeConverterShared(Type type)
Signature: Constructor
Description: Initializes a new instance of the converter for the specified enum type. Delegates to the base EnumConverter constructor.
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
Signature: object ConvertTo(ITypeDescriptorContext, CultureInfo, object, Type)
Description: Converts an enum value to a display string. Behavior:
- If
destinationTypeis notstring, delegates tobase.ConvertTo() - If
valueisnull, returnsstring.Empty - If the enum field cannot be resolved via reflection, returns
string.Empty - If no
DescriptionAttributeis present or the description is empty, returnsvalue.ToString() - If a
DescriptionAttributeexists, attempts to look up the description text as a resource key viaStringResources.ResourceManager.GetString() - If the resource lookup returns null/whitespace, returns the raw description text; otherwise returns the localized string
public static string GetEnumDescription(Enum value)
Signature: static string GetEnumDescription(Enum)
Description: A utility method that retrieves the description for an enum value. Behavior:
- Uses reflection to get the enum field's
DescriptionAttribute - If attributes exist, attempts localization via
StringResources.ResourceManager.GetString() - Returns the localized string if found; otherwise returns the raw description
- If no
DescriptionAttributeis present, returnsvalue.ToString()
3. Invariants
- The converter only modifies output when
destinationType == typeof(string); all other type conversions are passed through to the base class. - The
DescriptionAttribute.Descriptionproperty is treated as a resource key first; if no matching resource is found, the raw description text is used as a fallback. ConvertToalways returns a non-null string (either the description, the enum name, orstring.Empty).GetEnumDescriptionalways returns a non-null string (either the description, the enum name, or the raw description text).
4. Dependencies
This module depends on:
System- Core BCL types (Type,string,object)System.ComponentModel-EnumConverter,DescriptionAttribute,ITypeDescriptorContextSystem.Globalization-CultureInfoSystem.Linq-Enumerable.Any()extension methodDTS.Common.SharedResource.Strings.StringResources- Resource manager for localization lookups
Consumers:
- Not determinable from source alone. Based on the class design and referenced article URL, it is intended for WPF data binding scenarios where enum values are displayed in UI controls.
5. Gotchas
-
Null handling inconsistency:
ConvertTohas explicit null checks forvalueand returnsstring.Empty, butGetEnumDescriptionhas no null check for itsvalueparameter. PassingnulltoGetEnumDescriptionwill throw aNullReferenceExceptionwhen callingvalue.GetType(). -
Missing field info handling inconsistency:
ConvertTochecks iffi(field info) is null and returnsstring.Empty, butGetEnumDescriptiondoes not perform this check. If reflection fails to find the field,GetEnumDescriptionwill throw aNullReferenceException. -
Resource key collision: The
DescriptionAttribute.Descriptionvalue is used directly as a resource key. If the description text itself is not intended as a resource key but happens to match one, unintended localization may occur. -
Culture parameter ignored: The
ConvertTomethod accepts acultureparameter but does not use it; the resource manager's default culture is used instead.