Özet - Expression ağaçları
Expression ve Compile'ı birleştirerek dinamik bir filtre oluşturucu yazarsın.
Özet - Expression ağaçları
Bu grupta Expression<T>, lambda çevrimi, gövde inceleme, Compile ve IQueryable'ı öğrendin. Bir lambdayı ifade ağacına çevirip hem inceleyebilir hem de Compile ile delegeye dönüştürüp çalıştırabilirsin.
Şimdi bunları birleştirerek dinamik bir filtre oluşturucu yazacaksın: önce elle bir n > 10 ifade ağacı kuracaksın, sonra bunu derleyip bir dizi üzerinde çağıracaksın.
Uygulama Dinamik filtre oluştur ve çağır
Bir n > 10 koşulunu ifade ağacı olarak kur. Adımlar: ParameterExpression pe = Expression.Parameter(typeof(int), "n");, Expression body = Expression.GreaterThan(pe, Expression.Constant(10));, Expression<Func<int, bool>> filter = Expression.Lambda<Func<int, bool>>(body, pe);. Sonra Func<int, bool> fn = filter.Compile(); ile delege üret ve Console.WriteLine(fn(15)); ile sonucu yazdır.
Örnek çözümü göster
ParameterExpression pe = Expression.Parameter(typeof(int), "n"); Expression body = Expression.GreaterThan(pe, Expression.Constant(10)); Expression<Func<int, bool>> filter = Expression.Lambda<Func<int, bool>>(body, pe); Func<int, bool> fn = filter.Compile(); Console.WriteLine(fn(15));
Örnek çözüm:
using System;
using System.Linq.Expressions;
class Program
{
static void Main()
{
ParameterExpression pe = Expression.Parameter(typeof(int), "n");
Expression body = Expression.GreaterThan(pe, Expression.Constant(10));
Expression<Func<int, bool>> filter = Expression.Lambda<Func<int, bool>>(body, pe);
Func<int, bool> fn = filter.Compile();
Console.WriteLine(fn(15));
}
}
Uygulama Dinamik filtreyi diziye uygula
Bir n > 10 filtresini ifade ağacı olarak kur, filter.Compile() ile delege üret ve numbers.Where(fn) ile { 5, 12, 8, 20, 3 } dizisine uygula. Sonucu foreach ile gez ve her öğeyi Console.WriteLine(n) ile yazdır.
Örnek çözümü göster
ParameterExpression pe = Expression.Parameter(typeof(int), "n"); Expression body = Expression.GreaterThan(pe, Expression.Constant(10)); Expression<Func<int, bool>> filter = Expression.Lambda<Func<int, bool>>(body, pe); Func<int, bool> fn = filter.Compile(); var filtered = numbers.Where(fn); foreach (var n in filtered) { Console.WriteLine(n); }
Örnek çözüm:
using System;
using System.Linq;
using System.Linq.Expressions;
class Program
{
static void Main()
{
int[] numbers = { 5, 12, 8, 20, 3 };
ParameterExpression pe = Expression.Parameter(typeof(int), "n");
Expression body = Expression.GreaterThan(pe, Expression.Constant(10));
Expression<Func<int, bool>> filter = Expression.Lambda<Func<int, bool>>(body, pe);
Func<int, bool> fn = filter.Compile();
var filtered = numbers.Where(fn);
foreach (var n in filtered)
{
Console.WriteLine(n);
}
}
}
Kendini test et
-
Compile ne döndürür?
- Bir ifade ağacı
- Çalıştırılabilen bir delege
- Bir Type nesnesi
- Bir string
Cevabı göster
Çalıştırılabilen bir delege — Doğru. Compile, çalıştırılabilen bir delege döndürür.
-
Özelliği içeriğine eşle
- Body
- İç ifade
- NodeType
- İşlem türü
- Parameters
- Parametre listesi
- Compile
- Delege üretir