Skip to content

Commit

Permalink
Fix #2922: Invalid object initializer ILAst, when expression tree con…
Browse files Browse the repository at this point in the history
…tains get-accessor in Expression.Bind. Because either one of the accessors is used by Expression.Bind to determine the property, we can safely use the set-accessor instead.
  • Loading branch information
siegfriedpammer committed Apr 2, 2023
1 parent de8f133 commit 7e1e9d6
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ICSharpCode.Decompiler/IL/Transforms/TransformExpressionTrees.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.Semantics;
Expand Down Expand Up @@ -544,6 +545,15 @@ ILInstruction Convert()
return (null, SpecialType.UnknownType);
if (MatchGetMethodFromHandle(invocation.Arguments[0], out var member))
{
var method = (IMethod)member;
// It is possible to use Expression.Bind with a get-accessor,
// however, it would be an invalid expression tree if the property is readonly.
// As this is an assignment, the ILAst expects a set-accessor. To avoid any problems
// constructing property assignments, we explicitly use the set-accessor instead.
if (method.AccessorOwner is IProperty { CanSet: true } property && method != property.Setter)
{
member = property.Setter;
}
}
else if (MatchGetFieldFromHandle(invocation.Arguments[0], out member))
{
Expand Down

0 comments on commit 7e1e9d6

Please sign in to comment.