2010-03-15 75 views
1

,如果我有:Func键<sometype。这时候,布尔>到Func键<T,bool>

public static Func<SomeType, bool> GetQuery() { 
return a => a.Foo=="Bar"; 
} 

和仿制

public static Func<T, bool> GetQuery<T>() { 
return (Func<T,bool>)GetQuery(); 
} 

有没有办法投SOMETYPE我的强类型Func键的函数求T' 到目前为止,我发现的唯一的方法是尝试与一个模拟功能结合起来:

Func<T, bool> q=a => true; 
return (Func<T, bool>)Delegate.Combine(GetQuery(), q); 

我知道该怎么做,与Expression.Lambda,但我需要用普通的功能,而不是表达式树工作

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

我仍然是什么后,我怎么能返回一个值从Func的concreteType到Func的T.

我只是想通过编译器错误 - 很高兴有T的潜力是一个不同的类型和抛出运行时错误。

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; } 
} 

然后消耗资源库:

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 
    } 
} 
+6

不幸的是,我不明白你要完成的任务。 – 2010-03-15 18:48:47

+0

哪个版本的.Net?这在这里相当重要。 – Dykam 2010-03-15 19:08:16

+0

已经更新,以便更清晰。 使用.NET 3.5 试图铸造Func键到函数求 在运行时T将类型SOMETYPE的,但我想有声明了一个接口: Func键 GetQuery (); – steve 2010-03-16 13:59:50

回答

2

我不能完全确定你的要求,但这里的一个在黑暗中拍摄...

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>(); 
} 
+0

已更新的问题以使用您的示例与我试图实现的内容 – steve 2010-03-16 14:04:50

1

对于任何人谁遇到这个,并想知道我最终解决方案有2个部分。 感谢上面的帮助和质疑我的理智/我正在尝试做什么。

对于我正在尝试做的应该使用表达式,而不是委托功能。我只沿着代表路线走,让我把上下文传递给子查询/表达式。

要使用Expressions传递来自现有表达式(排序子表达式)的变量,我使用了LinqKit.Invoke。

我的最终类的样子:

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; } 
} 

用法:

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 
    } 
} 
相关问题