![]()
C# ta System.Reflection kütüphanesinde bulunan Reflection kısaca bir objenin içerisini okuyarak bize istediğimiz bilgileri döner. Yani Runtime esnasında Assembly(Derlenen kod kısmı) içindeki tüm türler hakkında bilgi edinmemizi sağlar. Bunlar Objenin tipi , Propertyleri, Objenin içerisinde tanımlanmış methodlar ve bunların parametreleri ve tipleridir ama bir objenin içini okumak için onun tippini öğreniriz. Bunuda typeof(obj) veya obj.GetType() merhodları ile yaparız.
Reflection ,
=> generic yapılarda ,
=> bir objeyi başka objeye map etme işleminde ,
=> generic tipli obje yapılarında,
=> farklı veri formatlarının birbiriyle eşleştirme işlemlerinde ,
=> jsondan gelen bir datanın xml deki bir data ile eşleştirilmesinde ,
=> db’den gelen bir datanın bir obje ile eşleştirilmesinde ,
=> işlemlerinde , bir tipin tüm methodlarının listelenmesinde ve parametrelerinin okunmasında ,
=> Activator.CreateInstance komutu, generic tiplerde,
=> ilgili objenin bir kopyasını oluşturarak ram’e yükleme işlemleri ,
=> bir tipin propertylerine otomatik olarak veri setlenmesinide ,
=> bir exe veya dll dosyasının method ve parametre analizi gibi senaryolarda kullanılır.
Yani üzerinde ciddi çalışılmış bir kütüphanedir.
Şimdi bir obje üzerinde uygulamalarına bakalım;
Türleri ve tipleri almak istediğimizde GetTypes() ve GetProperties() methodlarına bakalm
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class Bilgi { public string Adi { get; set; } public string Soyadi { get; set; } public int Yasi { get; set; } } Assembly assembly = Assembly.GetExecutingAssembly(); Type[] tur = assembly.GetTypes(); foreach (var item in tur) { Console.WriteLine(“Adı: “ + item.Name); foreach (var item2 in item.GetProperties()) { Console.WriteLine(“—-> Bilgi: “ + item2.Name); Console.WriteLine($“—-> Tipi: {item2.Name}/{item2.PropertyType}”); } } //Output: Adi: Program Adi: Bilgi ——> Bilgi: Adi ——> Tipi: Adi/System.String ——> Bilgi: Soyadi ——> Tipi: Soyadi/System.String ——> Bilgi: Yasi ——> Tipi: Yasi/System.Int32 Adi: Calisan |

Şimdide GetMethod() ve Invoke() methodlarına bakalım;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
namespace Uygulama { public class Personel { public static void Bilgi() { Console.WriteLine(“Personel Bilgileri”); } public static void Maas(double maas) { Console.WriteLine(“Personel Maaşı: “ + maas + ” TL”); } } } var tip = typeof(Uygulama.Personel); var fonk = tip.GetMethod(“Bilgi”); //Console.WriteLine(fonk);//fonksiyona ulaştım Personel personel = new Personel(); fonk.Invoke(personel, null);//invoke: çağırmak demek. Methodu çağırırız var fonk1 = tip.GetMethod(“Maas”); object[] gonder = { 5000 }; fonk1.Invoke(personel, gonder); //Output: Personel Bilgileri Personel Maasi: 5000 TL |
Reflection sayesinde class içindeki methodlarımızı açlıştırıp içeriklerine bakabilitriz. Yukarda Invoke() ilede methodu çalıştırıyoruz.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class Bilgi { public string Adi { get; set; } public string Soyadi { get; set; } public int Yasi { get; set; } public string CreateList(string adi , string soyadi) { return string.Format(“{0} – {1} “, adi, soyadi); } } Bilgi bilgi = new Bilgi(); var type = bilgi.GetType(); var methods = type.GetMethods().ToList(); foreach (var item in methods) { Console.WriteLine(item.Name); } //output: get_Adi set_Adi get_Soyadi set_Soyadi get_Yasi set_Yasi CreateList GetType ToString Equals GetHashCode |
Reflection ile çalışma zamanında nesne oluşturmak için CreateInstance() methodunu kullanıyoruz.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Type type = Type.GetType(“Bilgi”); var bilgi = Activator.CreateInstance(type); var method = type.GetMethod(“CreateList”); var result = method.Invoke(bilgi, new object[] { “Deneme”, “Reflection” }); Console.WriteLine(“Sonuç: “ + result); class Bilgi { public string Adi { get; set; } public string Soyadi { get; set; } public int Yasi { get; set; } public string CreateList(string adi , string soyadi) { return string.Format(“{0} – {1} “, adi, soyadi); } } //Output: Sonuç: Deneme – Reflection |
Sağlıcakla kalın….








Bir yanıt yazın