Skip to content

Commit 21f3722

Browse files
authored
Add support for constrained opcode (#299)
+semver:fix
1 parent b08d8f6 commit 21f3722

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using NUnit.Framework;
4+
5+
namespace DelegateDecompiler.Tests;
6+
7+
[TestFixture]
8+
public class Issue298 : DecompilerTestsBase
9+
{
10+
[Test]
11+
public void TestNotConstrained()
12+
{
13+
static int NotConstrained<T>(T value) where T : ITestInterface => ((ITestInterface)value).Value;
14+
15+
Expression<Func<TestClass, int>> expected = value => value.Value;
16+
Test(NotConstrained, expected);
17+
}
18+
19+
[Test]
20+
public void TestConstrained()
21+
{
22+
static int Constrained<T>(T value) where T : ITestInterface => value.Value;
23+
24+
Expression<Func<TestClass, int>> expected = value => value.Value;
25+
Test(Constrained, expected);
26+
}
27+
28+
interface ITestInterface
29+
{
30+
public int Value { get; }
31+
}
32+
33+
class TestClass : ITestInterface
34+
{
35+
public int Value { get; set; }
36+
}
37+
}

src/DelegateDecompiler/Processors/ConvertTypeProcessor.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ internal class ConvertTypeProcessor : IProcessor
1010
{
1111
public static void Register(Dictionary<OpCode, IProcessor> processors)
1212
{
13-
processors.Register(new ConvertTypeProcessor(), OpCodes.Castclass, OpCodes.Unbox, OpCodes.Unbox_Any);
13+
processors.Register(new ConvertTypeProcessor(),
14+
OpCodes.Castclass,
15+
OpCodes.Unbox,
16+
OpCodes.Unbox_Any,
17+
OpCodes.Constrained);
1418
}
15-
19+
1620
public void Process(ProcessorState state, Instruction instruction)
1721
{
1822
var expression = state.Stack.Pop();
19-
state.Stack.Push(Expression.Convert(expression, (Type)instruction.Operand));
23+
var targetType = (Type)instruction.Operand;
24+
state.Stack.Push(expression.Type != targetType
25+
? Expression.Convert(expression, targetType)
26+
: expression);
2027
}
2128
}
2229
}

0 commit comments

Comments
 (0)