C#’da object bir sınıf olan ve her tanımladığımız nesneden sonra GetTpe() ile ulaşabildiğimiz bir sınıftır Type sınıfı şimdi type sınıfını kullanarak sınıflara ait özelliklere ve üyelere nasıl erişebildiğimiz ile ilgili bilgiye makalenin devamında gözatalım.
C# Type sınıfı ile sınıflara ait tüm üyelere ve özelliklere erişim
Şimdi oluşturduğumuz sınıflar veya varolan sınıflara erişip üyekere gözatalım veya sınıflara ait özelliklere nasıl eriştiğimiz görelim öncelikle bu makalemizdeki kodları denemek için bir console uygulaması açın ve aşağıdaki kodlar yeni Personel adından bir sınıf oluşturarak içerisine ekleyin.
Personel.cs
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 | namespace ConsoleApplication1 { class Personel { public int Id { get; set; } public string Ad { get; set; } public string Soyad { get; set; } public Personel() { this.Id = -1; this.Ad = string.Empty; } public Personel(string ad, string soyad) { this.Ad = ad; this.Soyad = soyad; } public string TamAdGetir() { return string.Format("{0} {1}", Ad, Soyad); } } } |
Şimdi ise Personel adlı sınıfımıza ait üye ve özelliklere nasıl eriştiğimizi görelim. Program.cs adlı sınımızın içeriğini aşağıdaki şekilde değiştirin.
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 41 42 43 44 45 | namespace ConsoleApplication1 { using System; class Program { static void Main(string[] args) { //Type type = Type.GetType("ConsoleApplication1.Personel"); 1.Yötem //Type type = typeof (Personel); 2.Yöntem var pers = new Personel(); //3.Yöntem Type type = pers.GetType(); Console.WriteLine("Sınıf Özellikleri ------"); Console.WriteLine("Tam Adı: {0}", type.FullName); Console.WriteLine("Adı: {0}", type.Name); Console.WriteLine("İsim Uzayı: {0}", type.Namespace); Console.WriteLine(); Console.WriteLine("Personel Özellikleri ------"); foreach (var s in type.GetProperties()) { Console.WriteLine("Tür: {0} - Ad: {1}", s.PropertyType, s.Name); } Console.WriteLine(); Console.WriteLine("Personel Metodları ------"); foreach (var s in type.GetMethods()) { Console.WriteLine("Metod Adı: {0}", s.Name); Console.WriteLine("Geri Dönüş Türü: {0}", s.ReturnType); Console.WriteLine("Geri Dönüş Parametresi: {0}", s.ReturnParameter); } Console.WriteLine(); Console.WriteLine("Personel Yapıcı Metodları ------"); foreach (var s in type.GetConstructors()) { Console.WriteLine("Yapıcı Metod Adı: {0}", s.Name); } Console.ReadLine(); } } } |
Yukarıda yorum satırına aldığım 2 satır ile beraber pers.GetType()
metodu ilede Type sınıfını elde edebiliyoruz.
Sonuç
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 41 42 43 44 45 46 47 48 | Sınıf Özellikleri ------ Tam Adı: ConsoleApplication1.Personel Adı: Personel İsim Uzayı: ConsoleApplication1 Personel Özellikleri ------ Tür: System.Int32 - Ad: Id Tür: System.String - Ad: Ad Tür: System.String - Ad: Soyad Personel Metodları ------ Metod Adı: get_Id Geri Dönüş Türü: System.Int32 Geri Dönüş Parametresi: Int32 Metod Adı: set_Id Geri Dönüş Türü: System.Void Geri Dönüş Parametresi: Void Metod Adı: get_Ad Geri Dönüş Türü: System.String Geri Dönüş Parametresi: System.String Metod Adı: set_Ad Geri Dönüş Türü: System.Void Geri Dönüş Parametresi: Void Metod Adı: get_Soyad Geri Dönüş Türü: System.String Geri Dönüş Parametresi: System.String Metod Adı: set_Soyad Geri Dönüş Türü: System.Void Geri Dönüş Parametresi: Void Metod Adı: TamAdGetir Geri Dönüş Türü: System.String Geri Dönüş Parametresi: System.String Metod Adı: ToString Geri Dönüş Türü: System.String Geri Dönüş Parametresi: System.String Metod Adı: Equals Geri Dönüş Türü: System.Boolean Geri Dönüş Parametresi: Boolean Metod Adı: GetHashCode Geri Dönüş Türü: System.Int32 Geri Dönüş Parametresi: Int32 Metod Adı: GetType Geri Dönüş Türü: System.Type Geri Dönüş Parametresi: System.Type Personel Yapıcı Metodları ------ Yapıcı Metod Adı: .ctor Yapıcı Metod Adı: .ctor |