| | 1 | | #if NET35_OR_GREATER |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | |
|
| | 7 | | namespace PropertyGridHelpers.Support |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Provides logic to extract base names from resource names, removing a specified assembly prefix and the |
| | 11 | | /// standard <c>.resources</c> extension. |
| | 12 | | /// </summary> |
| | 13 | | /// <remarks> |
| | 14 | | /// This implementation uses<see cref = "string.Substring(int, int)" /> for compatibility with frameworks |
| | 15 | | /// prior to.NET 8, and is functionally equivalent to the range-based extractor defined for .NET 8 and higher. |
| | 16 | | /// </remarks> |
| | 17 | | /// <seealso cref="IResourceBaseNameExtractor"/> |
| | 18 | | internal class SubstringBasedBaseNameExtractor : IResourceBaseNameExtractor |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// Extracts base names from the specified resource names, removing the standard <c>.resources</c> extension |
| | 22 | | /// and stripping the provided assembly prefix if present. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="assemblyPrefix"> |
| | 25 | | /// The assembly prefix to remove from the beginning of resource names (e.g., the default namespace of the |
| | 26 | | /// assembly). If the base name starts with this prefix, it will be removed along with any subsequent dot |
| | 27 | | /// separator. |
| | 28 | | /// </param> |
| | 29 | | /// <param name="resourceNames"> |
| | 30 | | /// The full resource names to process, typically retrieved via <see cref="System.Reflection.Assembly.GetManifes |
| | 31 | | /// </param> |
| | 32 | | /// <returns> |
| | 33 | | /// An ordered list of distinct base names with the <c>.resources</c> extension and the assembly prefix removed. |
| | 34 | | /// </returns> |
| | 35 | | public IList<string> ExtractBaseNames(string assemblyPrefix, string[] resourceNames) |
| 8 | 36 | | { |
| | 37 | | const string resourceExtension = ".resources"; |
| 16 | 38 | | var resourceExtensionLength = resourceExtension.Length; |
| 16 | 39 | | var baseNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 40 | |
|
| 16 | 41 | | foreach (var name in resourceNames) |
| 8 | 42 | | { |
| 16 | 43 | | if (name.EndsWith(resourceExtension, StringComparison.OrdinalIgnoreCase)) |
| 8 | 44 | | { |
| 16 | 45 | | var baseName = name.Substring(0, name.Length - resourceExtensionLength); |
| | 46 | |
|
| 16 | 47 | | if (baseName.StartsWith(assemblyPrefix, StringComparison.OrdinalIgnoreCase)) |
| 16 | 48 | | baseName = baseName.Substring(assemblyPrefix.Length).TrimStart('.'); |
| | 49 | |
|
| 16 | 50 | | _ = baseNames.Add(baseName); |
| 8 | 51 | | } |
| | 52 | |
|
| 8 | 53 | | } |
| | 54 | |
|
| 16 | 55 | | return baseNames.OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToList(); |
| 8 | 56 | | } |
| | 57 | | } |
| | 58 | | } |
| | 59 | |
|
| | 60 | | #endif |