{"id":2330,"date":"2006-07-13T08:16:00","date_gmt":"2006-07-13T08:16:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/creating-class-instances-from-type-strings\/"},"modified":"2016-07-28T10:48:51","modified_gmt":"2016-07-28T10:48:51","slug":"creating-class-instances-from-type-strings","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/creating-class-instances-from-type-strings\/","title":{"rendered":"Creating Class Instances from Type Strings"},"content":{"rendered":"<p>If you&#8217;ve looked through the web.config or the machine.config then you&#8217;ve had to have seen various type strings strewn about the configuration.&#160; But have you ever wondered how they use those type strings to actually make a useful class instance (or object instance if you prefer) out of that type string?&#160; It&#8217;s actually really easy.<\/p>\n<p>You only have to do two thing.&#160; First, pass the type string to the <strong>Type.GetType<\/strong> method.&#160; If you pass in a valid type string, the method will return type information for that type. If you pass in an invalid type string, the method returns null (though you can send in a boolean parameter to tell it to throw an error right there if you want).&#160; <\/p>\n<p>Second, pass the resulting type information into the&#160;<strong>Activator.CreateInstance<\/strong> method.&#160; There are 13 overloads to the CreateInstance method, so you can also pass in constructor parameters (if needed) as well as a bunch of other information I&#8217;ve never really bothered to research.<\/p>\n<p>Following is an example that shows how to create different database connection objects using type strings:<\/p>\n<\/p>\n<p>\/\/Use must use an interface (or base class) to store a <br \/>\/\/reference to the object you want to create because the <br \/>\/\/exact object type varies based on which type string <br \/>\/\/you use to create the object. In this example we are <br \/>\/\/creating either a SqlDbConnection or an <br \/>\/\/OleDbConnection, so we use an IDbConnection to store a <br \/>\/\/reference to the object because both implement the <br \/>\/\/IDbConnection interface.<\/p>\n<p>Type cnxType; \/\/stores reference to the object type<br \/>IDbConnection cnx; \/\/stores reference to the object instance<\/p>\n<p>\/\/Example of creating a SqlConnection using a type string<br \/>cnxType = Type.GetType(@&#8221;System.Data.SqlClient.SqlConnection, System.Data, <br \/>&#160;&#160;&#160;&#160; Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&#8243;); <\/p>\n<p>cnx = (IDbConnection)Activator.CreateInstance(cnxType); <\/p>\n<p>\/\/Example of creating an OleDbConnection using a type string<br \/>cnxType = Type.GetType(@&#8221;System.Data.OleDb.OleDbConnection, System.Data, <br \/>&#160;&#160;&#160;&#160; Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&#8243;);<\/p>\n<p>cnx = (IDbConnection)Activator.CreateInstance(cnxType); <\/p>\n<p>\/\/ &#8230; Do Something With Your Connection &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve looked through the web.config or the machine.config then you&#8217;ve had to have seen various type strings strewn about the configuration.&#160; But have you ever wondered how they use those type strings to actually make a useful class instance (or object instance if you prefer) out of that type string?&#160; It&#8217;s actually really easy&#8230;.&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2330","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2330"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2330\/revisions"}],"predecessor-version":[{"id":41405,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2330\/revisions\/41405"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2330"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}