에 :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();
}
의 Func을에 SomeType의 내 강력한 형식의 Func을 캐스팅하는 방법이 있나요 티? 내가 지금까지 발견하는 유일한 방법은 시도하고 모의 기능을 결합하는 것입니다 :
Func<T, bool> q=a => true;
return (Func<T, bool>)Delegate.Combine(GetQuery(), q);
내가 그 Expression.Lambda와 함께 할 방법을 알고,하지만 난 일반 기능하지 식 트리로 작업해야
편집 - 매튜스 예제를 사용하여 .NET 3.5 을 사용 및 사용의 명시적인 세부 사항.
비록 내가 후에도 그래도 값을 반환 할 때 Func Of concreteType에서 Func Of 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
}
}
불행하게도, 당신이 달성하려고하는지 이해가 안 돼요. –
어떤 버전의 .Net? 여기는 상당히 중요합니다. – Dykam
이 위의 내용을 업데이트하여보다 명확하게되었습니다. .NET 3.5 유형의 SomeType이 될 것입니다 런타임 T에서 Func을 에 Func을 캐스팅하려고하지만 선언하는 인터페이스를 갖고 싶어 사용 : Func을 GetQuery 를(); –
steve