-
Notifications
You must be signed in to change notification settings - Fork 1
net472_PropertyGridHelpers.Support_Support_GetFileExtension
dparvin edited this page Jul 5, 2025
·
12 revisions
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. |
A string containing the file extension for the resource. If no valid extension is found, returns an empty string.
| exception | condition |
|---|---|
| InvalidOperationException | Thrown if the referenced property is not found or is not public. |
This method determines the file extension based on the following order of precedence:
- Checks if the property has a
FileExtensionAttributeand retrieves the value of the property it references. - If the referenced property is a string, its value is returned.
- 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.
- 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.
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"- class Support
- namespace PropertyGridHelpers.Support
- assembly PropertyGridHelpers