// Copyright (c) 2022 Tom Englert // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #nullable enable using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata; using ICSharpCode.Decompiler.IL; using ICSharpCode.Decompiler.Metadata; namespace ICSharpCode.Decompiler.Disassembler { public class SortByNameProcessor : IEntityProcessor { public IReadOnlyCollection Process(MetadataFile module, IReadOnlyCollection items) { return items.OrderBy(item => GetSortKey(item, module)).ToArray(); } public IReadOnlyCollection Process(MetadataFile module, IReadOnlyCollection items) { return items.OrderBy(item => GetSortKey(item, module)).ToArray(); } public IReadOnlyCollection Process(MetadataFile module, IReadOnlyCollection items) { return items.OrderBy(item => GetSortKey(item, module)).ToArray(); } public IReadOnlyCollection Process(MetadataFile module, IReadOnlyCollection items) { return items.OrderBy(item => GetSortKey(item, module)).ToArray(); } public IReadOnlyCollection Process(MetadataFile module, IReadOnlyCollection items) { return items.OrderBy(item => GetSortKey(item, module)).ToArray(); } public IReadOnlyCollection Process(MetadataFile module, IReadOnlyCollection items) { return items.OrderBy(item => GetSortKey(item, module)).ToArray(); } public IReadOnlyCollection Process(MetadataFile module, IReadOnlyCollection items) { return items.OrderBy(item => GetSortKey(item, module)).ToArray(); } private static string GetSortKey(TypeDefinitionHandle handle, MetadataFile module) => handle.GetFullTypeName(module.Metadata).ToILNameString(); private static string GetSortKey(MethodDefinitionHandle handle, MetadataFile module) { PlainTextOutput output = new PlainTextOutput(); MethodDefinition definition = module.Metadata.GetMethodDefinition(handle); // Start with the methods name, skip return type output.Write(module.Metadata.GetString(definition.Name)); DisassemblerSignatureTypeProvider signatureProvider = new DisassemblerSignatureTypeProvider(module, output); MethodSignature> signature = definition.DecodeSignature(signatureProvider, new MetadataGenericContext(handle, module)); if (signature.GenericParameterCount > 0) { output.Write($"`{signature.GenericParameterCount}"); } InstructionOutputExtensions.WriteParameterList(output, signature); return output.ToString(); } private static string GetSortKey(InterfaceImplementationHandle handle, MetadataFile module) => module.Metadata.GetInterfaceImplementation(handle) .Interface .GetFullTypeName(module.Metadata) .ToILNameString(); private static string GetSortKey(FieldDefinitionHandle handle, MetadataFile module) => module.Metadata.GetString(module.Metadata.GetFieldDefinition(handle).Name); private static string GetSortKey(PropertyDefinitionHandle handle, MetadataFile module) => module.Metadata.GetString(module.Metadata.GetPropertyDefinition(handle).Name); private static string GetSortKey(EventDefinitionHandle handle, MetadataFile module) => module.Metadata.GetString(module.Metadata.GetEventDefinition(handle).Name); private static string GetSortKey(CustomAttributeHandle handle, MetadataFile module) => module.Metadata.GetCustomAttribute(handle) .Constructor .GetDeclaringType(module.Metadata) .GetFullTypeName(module.Metadata) .ToILNameString(); } }