net472_PropertyGridHelpers.Support_Support_GetFileExtension - dparvin/PropertyGridHelpers GitHub Wiki

Support.GetFileExtension method

Retrieves the file extension associated with a property, if specified.

public static string GetFileExtension(ITypeDescriptorContext context)
parameter description
context The type descriptor context, which provides metadata about the property and its container.

Return Value

A string containing the file extension for the resource. If no valid extension is found, returns an empty string.

Exceptions

exception condition
InvalidOperationException Thrown if the referenced property is not found or is not public.

Remarks

This method determines the file extension based on the following order of precedence:

  1. Checks if the property has a FileExtensionAttribute and retrieves the value of the property it references.
  2. If the referenced property is a string, its value is returned.
  3. If the referenced property is an enumeration:
  • Returns the enum's string representation, unless it is None, in which case an empty string is returned.
  • If the enum field has an EnumTextAttribute, returns its custom text value.
  • If the enum field has a LocalizedEnumTextAttribute, returns its localized text value.
  1. If no matching attributes are found, the method returns an empty string.

Normally a user would not call this method directly, but it is used by the UIEditors to load values into the PropertyGrid.

Examples

Example usage:

[FileExtension(nameof(FileType))] public string FileName { get; set; } = "example"; 
public string FileType { get; set; } = "xml";  

var PropertyDescriptor = TypeDescriptor.GetProperties(this)[nameof(FileName)]; 
var context = new CustomTypeDescriptorContext(PropertyDescriptor, this);  
string extension = GetFileExtension(context);
Console.WriteLine(extension); // Outputs: "xml"

See Also

⚠️ **GitHub.com Fallback** ⚠️