首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Func<sometype,bool> to Func<T,bool>

Func<sometype,bool> to Func<T,bool>
EN

Stack Overflow用户
提问于 2010-03-15 18:44:50
回答 2查看 2.1K关注 0票数 1

如果我有:

代码语言:javascript
复制
public static Func<SomeType, bool> GetQuery() {
 return a => a.Foo=="Bar";
}

和一个通用版本

代码语言:javascript
复制
public static Func<T, bool> GetQuery<T>() {
 return (Func<T,bool>)GetQuery();
}

有没有办法将我强类型的SomeType函数转换为T函数?到目前为止,我发现的唯一方法是尝试将其与模拟函数结合起来:

代码语言:javascript
复制
Func<T, bool> q=a => true;
return (Func<T, bool>)Delegate.Combine(GetQuery(), q);

我知道如何使用Expression.Lambda,但我需要使用简单的函数,而不是表达式树。

编辑-使用.net 3.5使用马修斯示例,并有明确的使用细节。

然而,我所追求的仍然是,当返回一个值时,如何从concreteType的Func到T的Func。

我只是想通过编译器错误-并且很高兴有可能使T成为一个不同的类型,并抛出一个运行时错误。

代码语言:javascript
复制
public interface ISecureEntity {
 Func<T,bool> SecureFunction<T>(UserAccount user);
}


public class Product : ISecureEntity {
 public Func<T,bool> SecureFunction<T>(UserAccount user) {
  return (Func<T,bool>)SecureFunction(user); //this is an invalid cast
 }
 public static Func<Product,bool> SecureFunction(UserAccount user) {
  return f => f.OwnerId==user.AccountId;
 }
 public string Name { get;set; }
 public string OwnerId { get;set; }
}


public class ProductDetail : ISecureEntity {
 public Func<T,bool> SecureFunction<T>(UserAccount user) {
  return (Func<T,bool>)SecureFunction(user); //this is an invalid cast
 }
 public static Func<ProductDetail,bool> SecureFunction(UserAccount user) {
  return pd => Product.SecureFunction(user)(pd.ParentProduct);
 }
 public int DetailId { get;set; }
 public string DetailText { get;set; }
 public Product ParentProduct { get;set; }
}

然后在存储库中使用:

代码语言:javascript
复制
public IList<T> GetData<T>() {
 IList<T> data=null;
 Func<T,bool> query=GetSecurityQuery<T>();
 using(var context=new Context()) {
  var d=context.GetGenericEntitySet<T>().Where(query);
  data=d.ToList();
 }
 return data;
}
private Func<T,bool> GetSecurityQuery<T>() where T : new() {
  var instanceOfT = new T();
        if (typeof(Entities.ISecuredEntity).IsAssignableFrom(typeof(T))) {
            return ((Entities.ISecuredEntity)instanceOfT).SecurityQuery<T>(GetCurrentUser());
        }
        return a => true; //returning a dummy query
    }
}
EN

回答 2

Stack Overflow用户

发布于 2010-03-15 18:54:15

我不太清楚你在问什么,但这里有个暗处.

代码语言:javascript
复制
public interface IFoo
{
    string Foo { get; set; }
}
public static Func<T, bool> GetQuery<T>()
    where T : IFoo
{
    return i => i.Foo == "Bar";
}
// example...
public class SomeType : IFoo
{
    public string Foo { get; set; }
}
public static Func<SomeType, bool> GetQuery()
{
    return GetQuery<SomeType>();
}
票数 2
EN

Stack Overflow用户

发布于 2010-03-18 13:15:43

对于任何遇到这种情况并想知道我最终得到的解决方案的人来说,有两个部分。感谢上面的帮助和对我的理智/我想做的事情的质疑。

因为我试图做的应该是使用表达式,而不是委托函数。我只选择了委托路径,以便将上下文传递到子查询/表达式中。

要使用具有从现有表达式(某种子表达式)传入变量的能力的表达式,我使用了LinqKit.Invoke。

我的期末课程看上去像:

代码语言:javascript
复制
public interface ISecureEntity {
 Func<T,bool> SecureFunction<T>(UserAccount user);
}


public class Product : ISecureEntity {
 public Expression<Func<T,bool>> SecureFunction<T>(UserAccount user) {
  return SecureFunction(user) as Expression<Func<T,bool>>; 
 }
 public static Expression<Func<Product,bool>> SecureFunction(UserAccount user) {
  return f => f.OwnerId==user.AccountId;
 }
 public string Name { get;set; }
 public string OwnerId { get;set; }
}


public class ProductDetail : ISecureEntity {
 public Expression<Func<T,bool>> SecureFunction<T>(UserAccount user) {
  return SecureFunction(user) as Expression<Func<T,bool>>; 
 }
 public static Func<ProductDetail,bool> SecureFunction(UserAccount user) {
  return pd => Product.SecureFunction(user).Invoke(pd.ParentProduct);
 }
 public int DetailId { get;set; }
 public string DetailText { get;set; }
 public Product ParentProduct { get;set; }
}

用法:

代码语言:javascript
复制
public IList<T> GetData<T>() {
 IList<T> data=null;
 Expression<Func<T,bool>> query=GetSecurityQuery<T>();
 using(var context=new Context()) {
  var d=context.GetGenericEntitySet<T>().Where(query);
  data=d.ToList();
 }
 return data;
}
private Expression<Func<T,bool>> GetSecurityQuery<T>() where T : new() {
  var instanceOfT = new T();
        if (typeof(Entities.ISecuredEntity).IsAssignableFrom(typeof(T))) {
            return ((Entities.ISecuredEntity)instanceOfT).SecurityQuery<T>(GetCurrentUser());
        }
        return a => true; //returning a dummy query
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2449596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档