Damon Armstrong

Caffeine Induced Tirades about .NET and Life
And don't forget to check out my latest Simple-Talk articles
Add to Technorati Favorites      Add to Google     

Creating Class Instances from Type Strings

Published Thursday, July 13, 2006 8:16 AM

If you've looked through the web.config or the machine.config then you've had to have seen various type strings strewn about the configuration.  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?  It's actually really easy.

You only have to do two thing.  First, pass the type string to the Type.GetType method.  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). 

Second, pass the resulting type information into the Activator.CreateInstance method.  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've never really bothered to research.

Following is an example that shows how to create different database connection objects using type strings:

//Use must use an interface (or base class) to store a
//
reference to the object you want to create because the
//exact object type varies based on which
type string
//you use to create the object. In this example we are
//creating
either a SqlDbConnection or an
//OleDbConnection, so we use an IDbConnection
to store a
//
reference to the object because both implement the
//IDbConnection
interface.

Type
cnxType; //stores reference to the object type
IDbConnection cnx;
//stores reference to the object instance

//Example of creating a SqlConnection using a type string
cnxType = Type.GetType(
@"System.Data.SqlClient.SqlConnection, System.Data,
     Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
);

cnx = (
IDbConnection)Activator
.CreateInstance(cnxType);

//Example of creating an OleDbConnection using a type string
cnxType = Type.GetType(
@"System.Data.OleDb.OleDbConnection, System.Data,
     Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
);

cnx = (
IDbConnection)Activator
.CreateInstance(cnxType);

// ... Do Something With Your Connection ...

by Damon
Filed Under:

Comments

No Comments
You need to sign in to comment on this blog

















<July 2006>
SuMoTuWeThFrSa
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345
A SysAdmin's Guide to Change Management
 In the first in a series of monthly articles, ‘Confessions of a Sys Admin’, Matt describes the issues... Read more...

Exchange: Recovery Storage Groups
 It can happen at any time: You get a request, as Admin, from your company, to provide the contents of... Read more...

Build Your Own Virtualized Test Lab
 Desmon explains the fundamentals of building a test lab for Windows servers and Enterprise applications... Read more...

Rendering Hierarchical Data with the Treeview
 It sometimes happens that Web Server controls that visualize data don't quite fit with the way that... Read more...

SQL Server 2008: Performance Data Collector
 With Performance Data Collector in SQL Server 2008, you can now store performance data from a number of... Read more...