Browse Source

Fix #2922: Invalid object initializer ILAst, when expression tree contains 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.

pull/2944/head
Siegfried Pammer 2 years ago
parent
commit
7e1e9d65fd
  1. 10
      ICSharpCode.Decompiler/IL/Transforms/TransformExpressionTrees.cs

10
ICSharpCode.Decompiler/IL/Transforms/TransformExpressionTrees.cs

@ -20,6 +20,7 @@ using System; @@ -20,6 +20,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.Semantics;
@ -544,6 +545,15 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -544,6 +545,15 @@ namespace ICSharpCode.Decompiler.IL.Transforms
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))
{

Loading…
Cancel
Save