MemberInitExpression fails to recognize needed explicit conversion from value type to object.
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start by reproducing the issue with the provided Expression.Bind, MemberInit, and Lambda.Compile example, then inspect the validation path for Expression.Bind and compare it with the Expression.Convert workaround. Done means the invalid value-type-to-object binding is rejected with ArgumentException, with regression coverage for the reported case.
Written by the indexing model from the issue text.
Description
Description
Hello,
while compiling an Expression Tree I encountered an error when compiling a it as a lambda:
System.InvalidProgramException: Common Language Runtime detected an invalid program.
After some painful debugging (the expression tree was a bit larger^^) I found out that Expression.Bind(...) argument validation only checks for assignability to the member that is being initialized.
However: If the member has type 'object' and the value I want to bind is any value-type, the compiler expects an explicit convert to object. I guess this is because of boxing??
So either I would expect the compiler to be able to bind a value-type variable to an object member or the validation for the arguments of Expression.Bind(...) should throw an exception that tells me that a binding form a value-type to an object requires an explicit cast, but not a compiler error with such little detail further down the line...
If call a method or constructor I will get an error when assigning a value-type to an object parameter, so this seems fine. As I stated, I would expect an error from Expression.Bind if I cannot perform such an assignment.
This whole thing happens because the validation only checks for memberType.IsAssignableFrom(expression.Type)
Reproduction Steps
using System.Linq.Expressions;
var parameter = Expression.Parameter(typeof(decimal));
var binding = Expression.Bind(
typeof(Wrapper).GetProperty(nameof(Wrapper.Value))!,
parameter
);
var init = Expression.MemberInit(
Expression.New(typeof(Wrapper)),
binding
);
var lambda = Expression.Lambda(init, parameter);
var func = lambda.Compile();
return;
public class Wrapper
{
public object? Value { get; set; }
}
Exception:
System.InvalidProgramException: Common Language Runtime detected an invalid program.
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
at Program.<Main>$(String[] args)
Expected behavior
The comment for Expression.Bind states:
ArgumentException — member does not represent a field or property. -or- The property represented by member does not have a set accessor. -or- expression.Type is not assignable to the type of the field or property that member represents
I expect an Argument Exception when I am unable to assign a value-type to an object-member.
Actual behavior
No Exception is thrown. The error occurs once the lambda is being compiled.
Regression?
No response
Known Workarounds
Using Expression.Convert(...).
Configuration
.NET SDK 9.0.100
.NET 9.0
C# Version: 13
Other information
No response
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·