Files
DP44/enriched-partialglm/Common/DTS.Common.SharedResource.md
2026-04-17 14:55:32 -04:00

4.5 KiB

source_files, generated_at, model, schema_version, sha256
source_files generated_at model schema_version sha256
Common/DTS.Common.SharedResource/EnumDescriptionTypeConverterShared.cs
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 destinationType is not string, delegates to base.ConvertTo()
  • If value is null, returns string.Empty
  • If the enum field cannot be resolved via reflection, returns string.Empty
  • If no DescriptionAttribute is present or the description is empty, returns value.ToString()
  • If a DescriptionAttribute exists, attempts to look up the description text as a resource key via StringResources.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 DescriptionAttribute is present, returns value.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.Description property is treated as a resource key first; if no matching resource is found, the raw description text is used as a fallback.
  • ConvertTo always returns a non-null string (either the description, the enum name, or string.Empty).
  • GetEnumDescription always 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, ITypeDescriptorContext
  • System.Globalization - CultureInfo
  • System.Linq - Enumerable.Any() extension method
  • DTS.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

  1. Null handling inconsistency: ConvertTo has explicit null checks for value and returns string.Empty, but GetEnumDescription has no null check for its value parameter. Passing null to GetEnumDescription will throw a NullReferenceException when calling value.GetType().

  2. Missing field info handling inconsistency: ConvertTo checks if fi (field info) is null and returns string.Empty, but GetEnumDescription does not perform this check. If reflection fails to find the field, GetEnumDescription will throw a NullReferenceException.

  3. Resource key collision: The DescriptionAttribute.Description value 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.

  4. Culture parameter ignored: The ConvertTo method accepts a culture parameter but does not use it; the resource manager's default culture is used instead.