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

GenerateGetterExpr == GenerateGetterFunc same methods ?! #302

Closed
dundich opened this issue Sep 13, 2024 · 2 comments
Closed

GenerateGetterExpr == GenerateGetterFunc same methods ?! #302

dundich opened this issue Sep 13, 2024 · 2 comments

Comments

@dundich
Copy link
Contributor

dundich commented Sep 13, 2024

public static class ReflectionUtils
{
    public static Func<object, object> GenerateGetterExpr(PropertyInfo property)
    {
        var objInstanceExpr = Expression.Parameter(typeof(object), "instance");
        var typedInstanceExpr = Expression.TypeAs(objInstanceExpr, property.DeclaringType);

        var propertyExpr = Expression.Property(typedInstanceExpr, property);
        var propertyObjExpr = Expression.Convert(propertyExpr, typeof(object));

        return Expression.Lambda<Func<object, object>>(propertyObjExpr, objInstanceExpr).Compile();
    }

    public static Func<object, object> GenerateGetterFunc(PropertyInfo property)
    {
        var objInstanceExpr = Expression.Parameter(typeof(object), "instance");
        var typedInstanceExpr = Expression.TypeAs(objInstanceExpr, property.DeclaringType);

        var propertyExpr = Expression.Property(typedInstanceExpr, property);
        var propertyObjExpr = Expression.Convert(propertyExpr, typeof(object));

        return Expression.Lambda<Func<object, object>>(propertyObjExpr, objInstanceExpr).Compile();
    }
    ```
@zarusz
Copy link
Owner

zarusz commented Sep 13, 2024

Sounds like indeed this is a duplicate. Do you want to contribute a PR and remove one of the methods?

@zarusz
Copy link
Owner

zarusz commented Sep 13, 2024

Closed via #304

@zarusz zarusz closed this as completed Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants