zarusz / SlimMessageBus

Lightweight message bus interface for .NET (pub/sub and request-response) with transport plugins for popular message brokers.
Apache License 2.0
475 stars 78 forks source link

GenerateGetterExpr == GenerateGetterFunc same methods ?! #302

Closed dundich closed 1 month ago

dundich commented 1 month ago
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 commented 1 month ago

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

zarusz commented 1 month ago

Closed via #304