From 13a011f31a35d8cd720fcdc5e93f4dd2a253561c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 15:29:33 +0200 Subject: [PATCH] Removed unnecessary GetMethodHandle method --- .../Translation/MethodDecompiler.cs | 53 +------------------ 1 file changed, 2 insertions(+), 51 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index b2352443..20c2a95a 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -108,8 +108,8 @@ namespace ComputeSharp.Shaders.Translation lock (Lock) { // Get the handle of the input method - GetMethodHandle(methodInfo, out string assemblyPath, out EntityHandle methodHandle); - + string assemblyPath = methodInfo.DeclaringType?.Assembly.Location ?? throw new InvalidOperationException(); + EntityHandle methodHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.MetadataToken) ?? throw new InvalidOperationException(); // Get or create a decompiler for the target assembly, and decompile the method if (!Decompilers.TryGetValue(assemblyPath, out CSharpDecompiler decompiler)) { @@ -129,55 +129,6 @@ namespace ComputeSharp.Shaders.Translation } } - /// - /// Loads the assembly path and method handle for the input instance - /// - /// The input to inspect - /// The path of the assembly containing the input method - /// The handle for the input method - private void GetMethodHandle(MethodInfo methodInfo, out string assemblyPath, out EntityHandle methodHandle) - { - // Try to get the method handle from the assembly of the declaring type - Type declaringType = methodInfo.DeclaringType ?? throw new InvalidOperationException("Invalid declaring type"); - string path = declaringType.Assembly.Location; - if (!string.IsNullOrEmpty(path)) - { - assemblyPath = path; - methodHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.MetadataToken) ?? throw new InvalidOperationException(); - } - else - { - // Try to get the PEFile and type definition for the method declaring type - if (!TypeDefinitions.TryGetValue(methodInfo.DeclaringType, out (PEFile PEFile, TypeDefinition TypeDefinition) tuple)) - { - foreach (PEFile peFile in PEFiles) - { - // If not available, browse through the loaded PEFiles to find the type definition - TypeDefinitionHandle typeDefinitionHandle = peFile.Metadata.TypeDefinitions.FirstOrDefault(t => t.GetFullTypeName(peFile.Metadata).ToString() == methodInfo.DeclaringType.FullName); - - if (!typeDefinitionHandle.IsNil) - { - tuple = (peFile, peFile.Metadata.GetTypeDefinition(typeDefinitionHandle)); - TypeDefinitions.Add(methodInfo.DeclaringType, tuple); - - break; - } - } - - if (tuple.PEFile is null) throw new InvalidOperationException($"Missing PEFile for declaring type {methodInfo.DeclaringType.FullName}"); - } - - PEFile peFileForMethod = tuple.PEFile; - TypeDefinition typeDefinition = tuple.TypeDefinition; - - // Retrieve the assembly path and method handle from the loaded PEFile and metadata - assemblyPath = peFileForMethod.FileName; - methodHandle = typeDefinition.GetMethods() - .Where(m => peFileForMethod.Metadata.StringComparer.Equals(peFileForMethod.Metadata.GetMethodDefinition(m).Name, methodInfo.Name)) - .First(m => peFileForMethod.Metadata.GetMethodDefinition(m).GetParameters().Count == methodInfo.GetParameters().Length); - } - } - /// /// Creates a new instance targeting a given assembly ///