From 51a26c416bb74e0d438e160182b578cac219c2dd Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 31 May 2015 10:03:54 +0200 Subject: [PATCH] fix output of generic methods and typeof --- .../CSharp/ExpressionBuilder.cs | 5 +- .../Tests/ICSharpCode.Decompiler.Tests.csproj | 1 + .../Tests/TestCases/Generics.cs | 51 +++++++++++++++++++ ICSharpCode.Decompiler/Tests/TestRunner.cs | 6 +++ .../TypeSystem/DecompilerTypeSystem.cs | 7 ++- 5 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 ICSharpCode.Decompiler/Tests/TestCases/Generics.cs diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index d7163cb87..a39643f20 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -206,9 +206,9 @@ namespace ICSharpCode.Decompiler.CSharp protected internal override TranslatedExpression VisitLdTypeToken(LdTypeToken inst) { - return new TypeOfExpression(ConvertType(inst.Type)) + return new TypeOfExpression(ConvertType(inst.Type)).Member("TypeHandle") .WithILInstruction(inst) - .WithRR(new TypeOfResolveResult(compilation.FindType(KnownTypeCode.Type), inst.Type)); + .WithRR(new TypeOfResolveResult(compilation.FindType(new TopLevelTypeName("System", "RuntimeTypeHandle")), inst.Type)); } protected internal override TranslatedExpression VisitLogicNot(LogicNot inst) @@ -548,6 +548,7 @@ namespace ICSharpCode.Decompiler.CSharp } } else { var mre = new MemberReferenceExpression(target.Expression, inst.Method.Name); + mre.TypeArguments.AddRange(inst.Method.TypeArguments.Select(a => ConvertType(a))); expr = new InvocationExpression(mre, argumentExpressions); } return expr.WithILInstruction(inst).WithRR(rr); diff --git a/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj index ec79b5fcc..39feec815 100644 --- a/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj @@ -105,6 +105,7 @@ + diff --git a/ICSharpCode.Decompiler/Tests/TestCases/Generics.cs b/ICSharpCode.Decompiler/Tests/TestCases/Generics.cs new file mode 100644 index 000000000..4c31d2aab --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/TestCases/Generics.cs @@ -0,0 +1,51 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team +// +// 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. + +using System; + +namespace Generics +{ + /// + /// Description of Generics. + /// + public class Generics + { + public static void Main() + { + Console.WriteLine(TestGenericReturn()); + Console.WriteLine(TestGenericReturn()); + TestGenericParam(); + TestGenericParam(); + } + + public static T TestGenericReturn() + { + return default(T); + } + + public static void TestGenericParam() + { + Console.WriteLine(typeof(T)); + } + + public static void TestGenericParam() + { + Console.WriteLine(typeof(T1) + " " + typeof(T2)); + } + } +} diff --git a/ICSharpCode.Decompiler/Tests/TestRunner.cs b/ICSharpCode.Decompiler/Tests/TestRunner.cs index 85b3d5954..7267791b2 100644 --- a/ICSharpCode.Decompiler/Tests/TestRunner.cs +++ b/ICSharpCode.Decompiler/Tests/TestRunner.cs @@ -58,6 +58,12 @@ namespace ICSharpCode.Decompiler.Tests TestCompileDecompileCompileOutputAll("Switch.cs"); } + [Test] + public void Generics() + { + TestCompileDecompileCompileOutputAll("Generics.cs"); + } + void TestCompileDecompileCompileOutputAll(string testFileName) { TestCompileDecompileCompileOutput(testFileName, CompilerOptions.None); diff --git a/ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs b/ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs index 814ed1185..ea25d0cc2 100644 --- a/ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs +++ b/ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs @@ -233,8 +233,11 @@ namespace ICSharpCode.Decompiler var parameterTypes = methodReference.Parameters.SelectArray(p => Resolve(p.ParameterType)); var returnType = Resolve(methodReference.ReturnType); foreach (var method in methods) { - if (CompareSignatures(method.Parameters, parameterTypes) && CompareTypes(method.ReturnType, returnType)) - return method; + if (method.TypeParameters.Count != methodReference.GenericParameters.Count) + continue; + if (!CompareSignatures(method.Parameters, parameterTypes) || !CompareTypes(method.ReturnType, returnType)) + continue; + return method; } return CreateFakeMethod(methodReference); }