将Func <T,TProperty>转换为Expression <Func <T,Property >>

xvdiff

我有一个通用的存储库实现,该实现允许传递选择器以声明实体的主键属性:

public abstract class RepositoryBase<TEntity, TKey>
    where TEntity : class 
{

    private readonly Func<TEntity, TKey> _keySelector;

    protected Func<TEntity, TKey> KeySelector {
        get {
            return _keySelector;
        }
    }

    protected RepositoryBase(Func<TEntity, TKey> selector) {
        _keySelector = selector;
    }

}

...可以这样使用:

public class UserRepository : RepositoryBase<User, Guid>
{
    public UserRepository()
        : base((user) => user.Id)
    {

    }
}

现在,我已经实现了一个内存中的存储库,以执行一些单元测试,在这些单元测试中,我想为每个持久化的实体生成一个新的标识。如果实体没有密钥的公共访问器,我创建了一个扩展方法来使用反射来设置属性。

public static void SetProperty<T, TProperty>(this T instance, Expression<Func<T, TProperty>> selector,
        TProperty newValue)
        where T : class
    {
        if (instance == null)
            throw new ArgumentNullException("instance");
        if (selector == null)
            throw new ArgumentNullException("selector");

        var propertyInfo = selector.GetMember() as PropertyInfo;
        if (propertyInfo == null)
            throw new InvalidOperationException();

        propertyInfo.SetValue(instance, newValue);
    }

我的问题现在是:如何使用KeySelector作为表达式来设置主键值?有没有办法转换它?还是有更好的方法来实现我的目标?

这样吗 这甚至有意义吗?:

protected override void AddItem(TEntity entity)
    {
        if (entity == null)
            throw new ArgumentNullException("entity");

        var id = default(TKey);
        if (GetPrimaryKey(entity).Equals(default(TKey)))
        {
            id = _identifierGenerator.Generate();
            entity.SetProperty(x => GetPrimaryKey(x), id); // <----
        }

        _items[id] = entity;
    }

上面使用的一些方法:

方法“ GetPrimaryKey”

public TKey GetPrimaryKey(TEntity entity)
{
    if (entity == null)
        throw new ArgumentNullException("entity");

    return KeySelector(entity);
}

方法“ GetMember”

public static MemberInfo GetMember<T, TProperty>(this Expression<Func<T, TProperty>> expression)
    {
        var memberExp = RemoveUnary(expression.Body);

        return memberExp == null ? null : memberExp.Member;
    }

方法“ RemoveUnary”

    private static MemberExpression RemoveUnary(Expression toUnwrap)
    {
        var unwrap = toUnwrap as UnaryExpression;
        if (unwrap != null)
        {
            return unwrap.Operand as MemberExpression;
        }

        return toUnwrap as MemberExpression;
    }
乔恩·斯基特

基本上,您不能-不能以任何有用的方式。解决方案似乎很简单-改为更改属性的类型:

private readonly Expression<Func<TEntity, TKey>> _keySelector;

protected Expression<Func<TEntity, TKey>> KeySelector {
    get {
        return _keySelector;
    }
}

protected RepositoryBase(Expression<Func<TEntity, TKey>> selector) {
    _keySelector = selector;
}

您仍然可以使用lambda表达式来初始化属性,并且如果确实需要,可以将表达式树编译为委托。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将Expression <Func <T,TProperty >>转换为Expression <Func <object,object >>,反之亦然

来自分类Dev

将Expression <Func <T1 >>转换为Expression <Func <T1,T2 >>

来自分类Dev

将Expression <Func <T,V >>转换为Expression <Func <T,Nullable <V >>>

来自分类Dev

如何将Expression <Func <T,object >>转换为Expression <Func <T,bool >>?

来自分类Dev

将表达式转换为Expression <Func <T,bool >>

来自分类Dev

如何将 BinaryExpression 转换为 Expression<Func<T, bool>>?

来自分类Dev

是否可以将Expression <Func <T,bool >>转换为Expression <Func <MyType,bool >>?

来自分类Dev

将Expression <Func <TEntity,IEnumerable <TProperty >>> valueSelector,TValue []值转换为Expression <Func <TElement,bool >>

来自分类Dev

将Expression <Func <T,bool >>转换为Expression <Func <T1,bool >>,以便T是T1的成员

来自分类Dev

无法从 `Expression<Func<T1, T2>>` 转换为 `Expression<Func<object, object>>`

来自分类Dev

ConcurrentDictionary-将addValueFactory(Func <string,T,T>)转换为updateValueFactory(Func <string,T,T>)?

来自分类Dev

如何将IQueryable <T>转换为Expression <Func <T,bool >>?

来自分类Dev

将谓词<T>转换为Func <T,bool>

来自分类Dev

如何将委托`Func<T1, Func<T2, Task<TResult>>>` 转换为`Func<T1, Task<Func<T2, TResult>>`?

来自分类Dev

无法将OrderBy(Func <T,IComperable>)转换为SQL

来自分类Dev

如何将Func <T,object>转换为Func <Exception,object>

来自分类Dev

如何将Func <T,object>转换为Func <Exception,object>

来自分类Dev

如何在C#中将LambdaExpression转换为Expression <Func <T,bool >>

来自分类Dev

方法重载中的 Expression<Func<T,bool>> 与 Func<T,bool>

来自分类Dev

如何将简单的视图模型转换为Func <T,bool>谓词?

来自分类Dev

您可以在编译时将__func__转换为wchar_t []吗?

来自分类Dev

无法将lambda表达式转换为委托类型'System.Func <T,TKey>'

来自分类Dev

无法将lambda表达式转换为委托类型'System.Func <T,TKey>'

来自分类Dev

无法从“oncurrentBag<T>”转换为“Func<ConcurrentBag<T>>”

来自分类Dev

C#成员表达式Func <T,object>转换为Func <T,bool> MethodBinaryExpression

来自分类Dev

将Func转换为委托

来自分类Dev

方法的隐式转换为Func <T,Tresult>

来自分类Dev

将Expression <Func <TModel,TValue >>转换为Expression <Func <TModel,bool >>

来自分类Dev

将 Expression<Func<Tin, object>> 转换为 Expression<Func<Tin, Tout>>

Related 相关文章

  1. 1

    将Expression <Func <T,TProperty >>转换为Expression <Func <object,object >>,反之亦然

  2. 2

    将Expression <Func <T1 >>转换为Expression <Func <T1,T2 >>

  3. 3

    将Expression <Func <T,V >>转换为Expression <Func <T,Nullable <V >>>

  4. 4

    如何将Expression <Func <T,object >>转换为Expression <Func <T,bool >>?

  5. 5

    将表达式转换为Expression <Func <T,bool >>

  6. 6

    如何将 BinaryExpression 转换为 Expression<Func<T, bool>>?

  7. 7

    是否可以将Expression <Func <T,bool >>转换为Expression <Func <MyType,bool >>?

  8. 8

    将Expression <Func <TEntity,IEnumerable <TProperty >>> valueSelector,TValue []值转换为Expression <Func <TElement,bool >>

  9. 9

    将Expression <Func <T,bool >>转换为Expression <Func <T1,bool >>,以便T是T1的成员

  10. 10

    无法从 `Expression<Func<T1, T2>>` 转换为 `Expression<Func<object, object>>`

  11. 11

    ConcurrentDictionary-将addValueFactory(Func <string,T,T>)转换为updateValueFactory(Func <string,T,T>)?

  12. 12

    如何将IQueryable <T>转换为Expression <Func <T,bool >>?

  13. 13

    将谓词<T>转换为Func <T,bool>

  14. 14

    如何将委托`Func<T1, Func<T2, Task<TResult>>>` 转换为`Func<T1, Task<Func<T2, TResult>>`?

  15. 15

    无法将OrderBy(Func <T,IComperable>)转换为SQL

  16. 16

    如何将Func <T,object>转换为Func <Exception,object>

  17. 17

    如何将Func <T,object>转换为Func <Exception,object>

  18. 18

    如何在C#中将LambdaExpression转换为Expression <Func <T,bool >>

  19. 19

    方法重载中的 Expression<Func<T,bool>> 与 Func<T,bool>

  20. 20

    如何将简单的视图模型转换为Func <T,bool>谓词?

  21. 21

    您可以在编译时将__func__转换为wchar_t []吗?

  22. 22

    无法将lambda表达式转换为委托类型'System.Func <T,TKey>'

  23. 23

    无法将lambda表达式转换为委托类型'System.Func <T,TKey>'

  24. 24

    无法从“oncurrentBag<T>”转换为“Func<ConcurrentBag<T>>”

  25. 25

    C#成员表达式Func <T,object>转换为Func <T,bool> MethodBinaryExpression

  26. 26

    将Func转换为委托

  27. 27

    方法的隐式转换为Func <T,Tresult>

  28. 28

    将Expression <Func <TModel,TValue >>转换为Expression <Func <TModel,bool >>

  29. 29

    将 Expression<Func<Tin, object>> 转换为 Expression<Func<Tin, Tout>>

热门标签

归档