{"id":38,"date":"2009-08-07T09:39:11","date_gmt":"2009-08-07T15:09:11","guid":{"rendered":"http:\/\/www.arjarapu.com\/wordpress\/?p=38"},"modified":"2010-02-27T20:44:37","modified_gmt":"2010-02-28T02:14:37","slug":"how-to-set-property-of-an-object-created-using-reflection","status":"publish","type":"post","link":"http:\/\/www.arjarapu.com\/wordpress\/2009\/08\/how-to-set-property-of-an-object-created-using-reflection\/","title":{"rendered":"How to set property of an object created using Reflection?"},"content":{"rendered":"<p>The VB.NET to C#.NET converter <a href=\"http:\/\/www.developerfusion.com\/tools\/convert\/vb-to-csharp\/\">tool <\/a> from Developer Fusion is awesome! It convert code from VB to C# or vice versa and primarily used by enthusiasts to learn the other language syntax. The tool does a great deal of work in trans-coding. However, not every thing is translated appropriately and you may have to clean up a little bit. Here is an example of issues I faced while using Reflection. <!--more--><\/p>\n<p>Couple of days back I wrote code in VB.NET as shown below to load an assembly, create an object and set some properties<\/p>\n<pre class=\"brush:vb\">\r\n    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load\r\n        If Not Page.IsPostBack Then\r\n            'here is dynamic loading of the libraries\r\n            Dim asm As Assembly = Assembly.LoadFile(&quot;C:\\Inetpub\\wwwroot\\MySite\\Bin\\RssReader.dll&quot;)\r\n            Dim ctrlType As Type = asm.GetType(&quot;MySite.CustomControls.RssReader&quot;)\r\n\r\n            Dim control As Object = Activator.CreateInstance(ctrlType)\r\n            control.FeedUrl = &quot;http:\/\/www.google.com\/news?pz=1&amp;ned=us&amp;hl=en&amp;topic=m&amp;output=rss&quot;\r\n            Me.Controls.Add(control)\r\n        End If\r\n    End Sub\r\n<\/pre>\n<p>When one of my C#.NET based colleague wanted to make use of my code, he used the above VB.NET to C#.NET code and got the below code.<\/p>\n<pre class=\"brush:csharp\">\r\nprotected void  \/\/ ERROR: Handles clauses are not supported in C#\r\nPage_Load(object sender, System.EventArgs e)\r\n{\r\n\u00a0 \u00a0 if (!Page.IsPostBack) {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \/\/here is dynamic loading of the libraries\r\n\u00a0 \u00a0 \u00a0 \u00a0 Assembly asm = Assembly.LoadFile(&quot;C:\\\\Inetpub\\\\wwwroot\\\\MySite\\\\Bin\\\\RssReader.dll&quot;);\r\n\u00a0 \u00a0 \u00a0 \u00a0 Type ctrlType = asm.GetType(&quot;MySite.CustomControls.RssReader&quot;);\r\n\u00a0 \u00a0 \u00a0 \u00a0 \r\n\u00a0 \u00a0 \u00a0 \u00a0 object control = Activator.CreateInstance(ctrlType);\r\n\u00a0 \u00a0 \u00a0 \u00a0 control.FeedUrl = &quot;http:\/\/www.google.com\/news?pz=1&amp;ned=us&amp;hl=en&amp;topic=m&amp;output=rss&quot;;\r\n\u00a0 \u00a0 \u00a0 \u00a0 this.Controls.Add(control);\r\n\u00a0 \u00a0 }\r\n}\r\n<\/pre>\n<p>The tool did a great job in conversion process, however, we cracked our head to fix the error <\/p>\n<pre class='brush:vb'>\r\nError\t1\t'System.Web.UI.Control' does not contain a definition for 'FeedUrl' and no extension method 'FeedUrl' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)\tC:\\Inetpub\\wwwroot\\MySite\\Default2.aspx.cs\t22\t25\thttp:\/\/localhost\/MySite\/\r\n<\/pre>\n<p>leading to line of code<\/p>\n<pre class=\"brush:csharp\">\r\n control.FeedUrl = &quot;http:\/\/www.google.com\/news?pz=1&amp;ned=us&amp;hl=en&amp;topic=m&amp;output=rss&quot;;\r\n<\/pre>\n<p>The same piece of code did work in VB.NET but in C#.NET didn&#8217;t work until we modified the code as show below<\/p>\n<pre class=\"brush:csharp\">\r\nctrlType.GetProperty(&quot;FeedUrl&quot;).SetValue(control, @&quot;http:\/\/www.google.com\/news?pz=1&amp;ned=us&amp;hl=en&amp;topic=m&amp;output=rss&quot;, null);\r\n<\/pre>\n<p>My intention in this post is not to criticize the converter tool, but to cover &#8216;How to set property of an object created using Reflection?&#8217; in both C#.NET and VB.NET<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The VB.NET to C#.NET converter tool from Developer Fusion is awesome! It convert code from VB to C# or vice versa and primarily used by enthusiasts to learn the other language syntax. The tool does a great deal of work in trans-coding. However, not every thing is translated appropriately and you may have to clean up a little bit. Here is an example of issues I faced while using Reflection.  <!--more--> <a href=\"http:\/\/www.arjarapu.com\/wordpress\/2009\/08\/how-to-set-property-of-an-object-created-using-reflection\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,6,12,4],"tags":[23,24,16,35,17],"_links":{"self":[{"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/posts\/38"}],"collection":[{"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":13,"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":51,"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/posts\/38\/revisions\/51"}],"wp:attachment":[{"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}