Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expression 'GetValue' method is not work in parameterized anonymous object NewExpression case #1166

Open
leafkevin opened this issue Jan 8, 2024 · 1 comment
Assignees
Labels
bug Something isn't working todo Things to be done in the future

Comments

@leafkevin
Copy link

leafkevin commented Jan 8, 2024

Bug Description

Anonymous object, will report an error at the time of construction, anonymous objects are parameterized.
The anonymous object is

new { TotalAmount = 25, Products = new List<int> { 1, 2 } }

where code excuted here

public static object GetValue(this NewExpression expression)
{
    if (expression.Arguments?.Count != 0)
    {
        return Activator.CreateInstance(expression.Constructor.DeclaringType,
            expression.Arguments?.Select(arg => arg.GetValue()));
    }
    else
    {
        return Activator.CreateInstance(expression.Constructor.DeclaringType);
    }
}

exception has happened.

Exception Message:

System.MissingMethodException:“Constructor on type '<>f__AnonymousType29`2[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Collections.Generic.List`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' not found.

I changed to this, it's work.

public static object GetValue(this NewExpression expression)
{
    if (expression.Arguments.Count > 0)
        return Activator.CreateInstance(expression.Type, expression.Arguments.Select(arg => arg.Evaluate()).ToArray());
    else return Activator.CreateInstance(expression.Type);
}
@leafkevin leafkevin added the bug Something isn't working label Jan 8, 2024
@mikependon
Copy link
Owner

Thanks for tbis cool PR mate. We will verify our Integration Tests for this first and approve once everything is passing.

@mikependon mikependon added the todo Things to be done in the future label Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working todo Things to be done in the future
Projects
None yet
Development

No branches or pull requests

2 participants