{"id":32,"date":"2009-12-11T15:29:59","date_gmt":"2009-12-11T14:29:59","guid":{"rendered":"http:\/\/blog.blaten.nl\/?p=7"},"modified":"2009-12-11T15:29:59","modified_gmt":"2009-12-11T14:29:59","slug":"copy-properties-between-classes-using-lcg","status":"publish","type":"post","link":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/","title":{"rendered":"Copy properties between classes using LCG"},"content":{"rendered":"<p>How often do we developers have to create functions just to copy properties from one object to another object, just because it&#8217;s a different type? If the source and destination type have properties with the same name and are of the same type, you can use Lightweight Code Generation to create a delegate to do just that.<\/p>\n<p>As with all programming logic there is a pro and a con to using LCG to do this. Pro, it&#8217;s fast because you use compiled code after you&#8217;ve built the delegate. Con, it takes a bit of time to create this delegate. I&#8217;d use this approach when I will surely need to call this copying function a lot.<\/p>\n<p>Anyway, without further ado.. This is the function I&#8217;ve cooked up. (and I&#8217;m sorry but the comments are in dutch&#8230; \ud83d\ude42 )<\/p>\n<pre class=\"syntax csharp\">public static CopyPublicPropertiesDelegate GenerateCopyDelegate()\n{\nType typeIn = typeof(T);\nType typeOut = typeof(U);\n\nDynamicMethod dm = new DynamicMethod(typeIn.Name, null, new Type[] { typeof(T), typeof(U) }, typeof(GenericCopyObject).Module);\n\nILGenerator il = dm.GetILGenerator();\n\nPropertyInfo[] propsIn = typeIn.GetProperties(BindingFlags.Public | BindingFlags.Instance);\nPropertyInfo[] propsOut = typeOut.GetProperties(BindingFlags.Public | BindingFlags.Instance);\n\nList ps = new List();\n\nforeach (PropertyInfo prop in propsIn) \/\/ voor alle properties op het In object\n{\nPropertyInfo propOut = propsOut.FirstOrDefault\n(t => t.Name.Equals(prop.Name) && (t.PropertyType.Equals(prop.PropertyType));\nif (propOut != default(PropertyInfo)) \/\/ er is een property op het out object met dezelfde naam en type\nif (prop.CanRead && propOut.CanWrite) \/\/ als we op de bron kunnen lezen en op het doel kunnen schrijven kunnen we kopieren\n{\n\/\/ nu een stukje Assembler...\nil.Emit(OpCodes.Ldarg_1);   \/\/ Plaats het doelobject op de stack\nil.Emit(OpCodes.Ldarg_0);   \/\/ Plaats het bronobject op de stack\nil.EmitCall(OpCodes.Callvirt, prop.GetGetMethod(), null); \/\/ Haal het eerste element (bronobject) van de stack af en lees de waarde uit van de property\nil.EmitCall(OpCodes.Callvirt, propOut.GetSetMethod(), null);\/\/ Haal het eerste element (doelobject) van de stack af en schrijf de waarde naar de property\n}\n}\nil.Emit(OpCodes.Ret); \/\/ we zijn klaar dus de methode mag dan eindigen\nreturn (CopyPublicPropertiesDelegate)dm.CreateDelegate(typeof(CopyPublicPropertiesDelegate));\n}<\/pre>\n<p>The code needs a small warning though, it won&#8217;t work with properties with the same name but a different type and generic types are also quite tricky.<\/p>\n<p>I&#8217;m still working on a fix for it, and I&#8217;m open for suggestions \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Describes how you can create a delegate, using lightweight code generation, to copy equal properties between objects of different classes.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7,1],"tags":[4,10,11],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Copy properties between classes using LCG &raquo; Christiaan Nieuwlaat .nl<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copy properties between classes using LCG &raquo; Christiaan Nieuwlaat .nl\" \/>\n<meta property=\"og:description\" content=\"Describes how you can create a delegate, using lightweight code generation, to copy equal properties between objects of different classes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/\" \/>\n<meta property=\"og:site_name\" content=\"Christiaan Nieuwlaat .nl\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/christiaan.nieuwlaat\" \/>\n<meta property=\"article:published_time\" content=\"2009-12-11T14:29:59+00:00\" \/>\n<meta name=\"author\" content=\"chris\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@krizzje\" \/>\n<meta name=\"twitter:site\" content=\"@krizzje\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"chris\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/\",\"url\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/\",\"name\":\"Copy properties between classes using LCG &raquo; Christiaan Nieuwlaat .nl\",\"isPartOf\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#website\"},\"datePublished\":\"2009-12-11T14:29:59+00:00\",\"dateModified\":\"2009-12-11T14:29:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.christiaannieuwlaat.nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Copy properties between classes using LCG\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#website\",\"url\":\"https:\/\/www.christiaannieuwlaat.nl\/\",\"name\":\"Christiaan Nieuwlaat .nl\",\"description\":\"IT Whizzkid behind the wheels of steel\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.christiaannieuwlaat.nl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef\",\"name\":\"chris\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g\",\"caption\":\"chris\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Copy properties between classes using LCG &raquo; Christiaan Nieuwlaat .nl","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/","og_locale":"en_US","og_type":"article","og_title":"Copy properties between classes using LCG &raquo; Christiaan Nieuwlaat .nl","og_description":"Describes how you can create a delegate, using lightweight code generation, to copy equal properties between objects of different classes.","og_url":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/","og_site_name":"Christiaan Nieuwlaat .nl","article_publisher":"https:\/\/www.facebook.com\/christiaan.nieuwlaat","article_published_time":"2009-12-11T14:29:59+00:00","author":"chris","twitter_card":"summary_large_image","twitter_creator":"@krizzje","twitter_site":"@krizzje","twitter_misc":{"Written by":"chris","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/","url":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/","name":"Copy properties between classes using LCG &raquo; Christiaan Nieuwlaat .nl","isPartOf":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/#website"},"datePublished":"2009-12-11T14:29:59+00:00","dateModified":"2009-12-11T14:29:59+00:00","author":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef"},"breadcrumb":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2009\/12\/11\/copy-properties-between-classes-using-lcg\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.christiaannieuwlaat.nl\/"},{"@type":"ListItem","position":2,"name":"Copy properties between classes using LCG"}]},{"@type":"WebSite","@id":"https:\/\/www.christiaannieuwlaat.nl\/#website","url":"https:\/\/www.christiaannieuwlaat.nl\/","name":"Christiaan Nieuwlaat .nl","description":"IT Whizzkid behind the wheels of steel","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.christiaannieuwlaat.nl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef","name":"chris","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g","caption":"chris"}}]}},"_links":{"self":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts\/32"}],"collection":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":0,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}