Ağustos 26, 2016
ASP.NET(C#) ile Property isimlerini ve Türlerini listeletme
Merhaba arkadaşlar bu yazımda asp.net ile bir class içinde oluşturduğumuz propertyleri ve bu propertylerin tiplerini listeletme kodlarını paylaşacağım.
public class ogrenci
{
public string adi { get; set; }
public string okulu { get; set; }
public int yasi { get; set; }
}
//YUKARIDAKİ KISIMDA CLASSIMIZI OLUŞTURDUK
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ogrenci ogr = new ogrenci() { adi="Mustafa Zenbil",okulu="Haliç Üniversitesi"};
Response.Write(ogr.GetType()+"
");
//üstteki satır class adını vericek yani çıktısı ogrenci
foreach (var prop in ogr.GetType().GetProperties())
Response.Write(prop.Name +"=" +prop.PropertyType.Name+"="+prop.GetValue(ogr,null) +"
");
//foreach ile class içindeki propertyleri döndürüyoruz ve önce propertynin adı ve daha sonra değişken tipini ve en sonda o prpertydeki değişken değerini yazdırıyoruz
}
}