﻿
{"id":4850,"date":"2016-08-06T02:17:13","date_gmt":"2016-08-06T02:17:13","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=4850"},"modified":"2016-08-06T02:17:13","modified_gmt":"2016-08-06T02:17:13","slug":"cta-system-reflection-kutuphanesi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/cta-system-reflection-kutuphanesi\/","title":{"rendered":"C#&#8217;ta System.Reflection K\u00fct\u00fcphanesi"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>C# dilinde; bir s\u0131n\u0131f\u0131n elemanlar\u0131na nesne, referans yahut s\u0131n\u0131f ismi \u00fczerinden eri\u015fmekten ziyade alternatif bir \u015fekilde programatik olarak eri\u015febilmek i\u00e7in ilgili tipin \u00fczerinde belli ad\u0131mlarla \u00e7al\u0131\u015fma ger\u00e7ekle\u015ftirmemiz gerekmektedir. \u0130\u015fte bu ad\u0131mlar\u0131n kayna\u011f\u0131n\u0131 bizlere <strong><em>System.Reflection<\/em><\/strong> k\u00fct\u00fcphanesi sa\u011flamaktad\u0131r.<\/p>\n<p>Asl\u0131nda bilinen bir tip \u00fczerinde reflect i\u015fleminin her ne kadar \u00f6nemi olmasada, bilinmeyen, \u00f6zelle\u015ftirilmi\u015f(generic) \u00e7al\u0131\u015fmalarda elde edilen nesnelerin memberlar\u0131na eri\u015filmesi gereken bir durumda kullanaca\u011f\u0131m\u0131z birinci dereceden \u00f6nem arz eden bir yap\u0131dan bahsediyor olaca\u011f\u0131z.<\/p>\n<p>\u015eimdi konuyu pratikle destekleyerek ilerlemeye ba\u015flayabiliriz. A\u015fa\u011f\u0131daki s\u0131n\u0131f \u00f6rnek bir Reflection i\u015flemi i\u00e7in haz\u0131rlanm\u0131\u015ft\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Ogrenci\r\n    {\r\n        public string OgrenciNo { get; set; }\r\n        public string Adi { get; set; }\r\n        public string SoyAdi { get; set; }\r\n        public double MaasHesapla(int Saat, double Ucret) =&gt; Saat * Ucret;\r\n    }\r\n<\/pre>\n<p>Asl\u0131nda bu s\u0131n\u0131f\u0131n tipi, i\u00e7eri\u011fi ve t\u00fcm elemanlar\u0131 net bir \u015fekilde belli olmas\u0131na kar\u015f\u0131n reflect i\u015fleminin uygulanmas\u0131nda bir girizgah olmas\u0131 a\u00e7\u0131s\u0131ndan a\u015fa\u011f\u0131daki gibi System.Reflection k\u00fct\u00fcphanesinin nimetlerinden yararlanarak mevcut elemanlar\u0131na eri\u015febiliriz.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4858\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-300x187.png\" alt=\"C#'ta System.Reflection K\u00fct\u00fcphanesi\" width=\"300\" height=\"187\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-300x187.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi.png 350w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nG\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi ilgili tip \u00fczerinden(<em>ki bu tip bir referans yahut nesne \u00fczerinden GetType metodu ilede g\u00f6r\u00fcld\u00fc\u011f\u00fc gibi elde edilebilir<\/em>) &#8220;Get&#8221; ile ba\u015flayan metodlar\u0131n \u00e7o\u011funu kullanarak reflect i\u015flemlerini ger\u00e7ekle\u015ftirebiliriz. Mesela bu metodlardan &#8220;GetProperties&#8221; metodu ilgili tip i\u00e7erisindeki property yap\u0131lar\u0131na getirirken, &#8220;GetMethods&#8221; metodu ise tip i\u00e7erisinde mevcut olan metodlar\u0131 elde ettirmektedir. Ve bu i\u015flemleri yaparken yap\u0131s\u0131na g\u00f6re s\u0131n\u0131flarda \u00e7al\u0131\u015fmaktad\u0131r.(<em>MethodInfo, PropertyInfo<\/em>)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            Ogrenci o = new Ogrenci();\r\n            o.GetType().GetProperties().ToList().ForEach(p =&gt;\r\n            {\r\n                Console.WriteLine(p.Name);\r\n            });\r\n\r\n            Console.Read();\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki \u00f6rnek kod blo\u011funu incelerseniz e\u011fer, &#8220;Ogrenci&#8221; s\u0131n\u0131f\u0131 i\u00e7erisindeki t\u00fcm propertyleri elde ederek bunlar\u0131n isimlerini ekrana yazd\u0131rm\u0131\u015f bulunmaktay\u0131z.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4862\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-1.png\" alt=\"C#'ta System.Reflection K\u00fct\u00fcphanesi\" width=\"120\" height=\"93\" \/><\/a><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            Ogrenci o = new Ogrenci();\r\n            o.GetType().GetMethods().ToList().ForEach(m =&gt;\r\n            {\r\n                if (m.Name == &quot;MaasHesapla&quot;)\r\n                {\r\n                    var Sonuc = m.Invoke(o, new object&#x5B;] { 3, 5 });\r\n                    Console.WriteLine(Sonuc);\r\n                }\r\n            });\r\n            \r\n            Console.Read();\r\n        }\r\n    }\r\n<\/pre>\n<p>Bu \u00f6rne\u011fimizde ise &#8220;Ogrenci&#8221; s\u0131n\u0131f\u0131 i\u00e7erisindeki t\u00fcm metodlar\u0131 elde edip &#8220;MaasHesapla&#8221; isimli metodu Invoke metodu ile \u00e7al\u0131\u015ft\u0131rmakta ve sonucu elde etmekteyiz. Burada dikkat etmeniz gereken nokta, elemanlar\u0131n ad\u0131, niteli\u011fi gibi \u00f6zellikler d\u0131\u015f\u0131nda i\u015flevselli\u011fi bir nesne gerektirece\u011finden dolay\u0131 Invoke metodunun ilk parametresine &#8220;o&#8221; referans\u0131ndaki nesneyi vermekteyiz. Eee haliyle &#8220;MaasHesapla&#8221; metodu &#8220;o&#8221; referans\u0131na ba\u011fl\u0131 nesne \u00fczerinden \u00e7al\u0131\u015ft\u0131r\u0131lm\u0131\u015f olacakt\u0131r.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4866\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-2.png\" alt=\"C#'ta System.Reflection K\u00fct\u00fcphanesi\" width=\"87\" height=\"48\" \/><\/a><\/p>\n<p>Haliyle propertylerde de ayn\u0131 mant\u0131k s\u00f6z konusu olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            Ogrenci o = new Ogrenci();\r\n\r\n            o.GetType().GetProperties().ToList().ForEach(p =&gt;\r\n            {\r\n                if (p.Name == &quot;OgrenciNo&quot;)\r\n                    p.SetValue(o, &quot;11040355&quot;);\r\n                else if (p.Name == &quot;Adi&quot;)\r\n                    p.SetValue(o, &quot;Gen\u00e7ay&quot;);\r\n                else\r\n                    p.SetValue(o, &quot;Y\u0131ld\u0131z&quot;);\r\n            });\r\n\r\n            Console.WriteLine($&quot;{o.Adi} {o.SoyAdi} {o.OgrenciNo}&quot;);\r\n            Console.Read();\r\n        }\r\n    }\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi propertylerede veri eklemek i\u00e7in bir nesneye ihtiya\u00e7 olaca\u011f\u0131ndan dolay\u0131 tekrar &#8220;o&#8221; referans\u0131ndaki nesne SetValue metoduna ba\u011flanm\u0131\u015f ve o nesne \u00fczerinden property \u00e7al\u0131\u015ft\u0131r\u0131lm\u0131\u015ft\u0131r.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4868\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-3.png\" alt=\"C#'ta System.Reflection K\u00fct\u00fcphanesi\" width=\"193\" height=\"44\" \/><\/a><br \/>\nAyn\u0131 i\u015flem propertydeki veri \u00e7a\u011fr\u0131l\u0131rkende yap\u0131lmaktad\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            Ogrenci o = new Ogrenci() { Adi = &quot;Gen\u00e7ay&quot;, SoyAdi = &quot;Y\u0131ld\u0131z&quot;, OgrenciNo = &quot;11040355&quot; };\r\n\r\n            o.GetType().GetProperties().ToList().ForEach(p =&gt;\r\n            {\r\n                if (p.Name == &quot;OgrenciNo&quot;)\r\n                    Console.WriteLine($&quot;\u00d6\u011frenci No : &quot; + p.GetValue(o));\r\n                else if (p.Name == &quot;Adi&quot;)\r\n                    Console.WriteLine($&quot;Ad\u0131 : &quot; + p.GetValue(o));\r\n                else\r\n                    Console.WriteLine($&quot;Soyad\u0131 : &quot; + p.GetValue(o));\r\n            });\r\n\r\n            Console.Read();\r\n        }\r\n    }\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi GetValue metodu i\u015flevsel \u00f6zellikte oldu\u011fu i\u00e7in &#8220;o&#8221; referans\u0131na ba\u011fl\u0131 nesne \u00fczerinden de\u011fer \u00e7a\u011fr\u0131m i\u015flemi ger\u00e7ekle\u015ftirecektir.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4870\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/08\/Cta-System.Reflection-K\u00fct\u00fcphanesi-4.png\" alt=\"C#'ta System.Reflection K\u00fct\u00fcphanesi\" width=\"180\" height=\"68\" \/><\/a><\/p>\n<p>Evet, buraya kadar Reflection i\u015flemini bilinen bir class \u00fczerinde uygulam\u0131\u015f olduk.<\/p>\n<p><strong><em>Peki \u00fczerinde \u00e7al\u0131\u015ft\u0131\u011f\u0131m\u0131z s\u0131n\u0131f generic(\u00f6zelle\u015ftirilmi\u015f) ise ne yapaca\u011f\u0131z?<\/em><\/strong><br \/>\nTabi ki de ayn\u0131 i\u015flemleri uygulayaca\u011f\u0131z. Sadece olay biraz kompleksle\u015f(mi\u015f) gibi g\u00f6z\u00fckecektir.<\/p>\n<p><strong><em>Hele hele elimizdeki s\u0131n\u0131f Generic Extension metod bar\u0131nd\u0131yorsa?<\/em><\/strong><br \/>\n\u0130\u015fte bu soruya \u00f6zel olarak tasarlad\u0131\u011f\u0131m a\u015fa\u011f\u0131daki \u00f6rnek kod blo\u011funu inceleyiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    static class Islemler\r\n    {\r\n        static public T Islem&lt;T&gt;(this T nesne, T yeniNesne) where T : class, new()\r\n        {\r\n            nesne.GetType().GetProperties().ToList().ForEach(p =&gt;\r\n            {\r\n                yeniNesne.GetType().GetProperties().ToList().ForEach(p2 =&gt;\r\n                {\r\n                    if (p.Name == p2.Name)\r\n                    {\r\n                        p.SetValue(nesne, p2.GetValue(yeniNesne));\r\n                    }\r\n                });\r\n            });\r\n\r\n            return nesne;\r\n        }\r\n\r\n        static public T Islem&lt;T&gt;(this T nesne) where T : class, new()\r\n        {\r\n            T yeniNesne = new T();\r\n            nesne.GetType().GetProperties().ToList().ForEach(p =&gt;\r\n            {\r\n                p.SetValue(yeniNesne, &quot;sdasd&quot;);\r\n            });\r\n\r\n            return yeniNesne;\r\n        }\r\n    }\r\n<\/pre>\n<p>Burada g\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere Islem ad\u0131nda iki adet overload generic extension metod tasarlanm\u0131\u015ft\u0131r. Bu metodlar i\u00e7erisinde \u00fczerinde \u00e7al\u0131\u015f\u0131lan nesnenin propertyleri elde edilmektedir. Tek parametreli metod elde etti\u011fi propertye manuel de\u011fer vermektedir. Burada dikkat etmemiz gereken nokta, i\u00e7erisinde T tipinde olu\u015fturulan bir nesne, SetValue metodunda fiziksel kaynak olarak kullan\u0131lmaktad\u0131r. Di\u011fer metodumuz ise \u00fczerinde \u00e7al\u0131\u015ft\u0131\u011f\u0131 nesne ile ikinci parametrede ald\u0131\u011f\u0131 nesnenin benzer propertylerini kontrol etmekte ve ilgili nesneye, parametreyle al\u0131nan nesnenin de\u011ferleri set edilmektedir.<\/p>\n<p>Son olarak System.Reflection k\u00fct\u00fcphanesinin \u00f6nemini \u00f6zetleyen bir s\u00f6zle i\u00e7eri\u011fimizi sonland\u0131rmak istiyorum;<\/p>\n<blockquote><p>&#8220;Bir \u015feyi \u00e7\u00f6zemiyorsan onun \u00e7\u00f6z\u00fcm\u00fc kesinlikle Reflection&#8217;d\u0131r.&#8221;<\/p><\/blockquote>\n<p>Okudu\u011funuz i\u00e7in te\u015fekk\u00fcr ederim&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, C# dilinde; bir s\u0131n\u0131f\u0131n elemanlar\u0131na nesne, referans yahut s\u0131n\u0131f ismi \u00fczerinden eri\u015fmekten ziyade alternatif bir \u015fekilde programatik olarak eri\u015febilmek i\u00e7in ilgili tipin \u00fczerinde belli ad\u0131mlarla \u00e7al\u0131\u015fma ger\u00e7ekle\u015ftirmemiz gerekmektedir. \u0130\u015fte bu ad\u0131mlar\u0131n kayna\u011f\u0131n\u0131 bizlere&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3223,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[1403,1407,1404,1405,1408,1406,1402],"class_list":["post-4850","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-sharp-c","tag-getproperties","tag-getvalue","tag-methodinfo","tag-propertyinfo","tag-reflection","tag-setvalue","tag-system-reflection"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/4850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=4850"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/4850\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3223"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=4850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=4850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=4850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}