Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/DelegateDecompiler.Tests/Issue298.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq.Expressions;
using NUnit.Framework;

namespace DelegateDecompiler.Tests;

[TestFixture]
public class Issue298 : DecompilerTestsBase
{
[Test]
public void TestNotConstrained()
{
static int NotConstrained<T>(T value) where T : ITestInterface => ((ITestInterface)value).Value;

Expression<Func<TestClass, int>> expected = value => value.Value;
Test(NotConstrained, expected);
}

[Test]
public void TestConstrained()
{
static int Constrained<T>(T value) where T : ITestInterface => value.Value;

Expression<Func<TestClass, int>> expected = value => value.Value;
Test(Constrained, expected);
}

interface ITestInterface
{
public int Value { get; }
}

class TestClass : ITestInterface
{
public int Value { get; set; }
}
}
13 changes: 10 additions & 3 deletions src/DelegateDecompiler/Processors/ConvertTypeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ internal class ConvertTypeProcessor : IProcessor
{
public static void Register(Dictionary<OpCode, IProcessor> processors)
{
processors.Register(new ConvertTypeProcessor(), OpCodes.Castclass, OpCodes.Unbox, OpCodes.Unbox_Any);
processors.Register(new ConvertTypeProcessor(),
OpCodes.Castclass,
OpCodes.Unbox,
OpCodes.Unbox_Any,
OpCodes.Constrained);
}

public void Process(ProcessorState state, Instruction instruction)
{
var expression = state.Stack.Pop();
state.Stack.Push(Expression.Convert(expression, (Type)instruction.Operand));
var targetType = (Type)instruction.Operand;
state.Stack.Push(expression.Type != targetType
? Expression.Convert(expression, targetType)
: expression);
}
}
}