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
On the Trail of the Expanding Databases
 It is sometimes difficult for other IT people to understand the constraints that DBAs have to work... Read more...

SQL Server 2008: The New Data Types
 Brad continues his helicopter-level view of the most interesting new features of SQL Server 2008 with a... Read more...

Reporting on Mobile Device Activity Using Exchange 2007 ActiveSync Logs
 In this new column giving practical advice on all things Sys Admin related, Ben Lye takes on the often... Read more...

The Bejeweled Puzzle in SQL
 Alex Kozak provides another SQL puzzle to hone your SQL Skills with.  Read more...

Using Powershell to Generate Table-Creation Scripts
 For all of us who learn best by trying out examples, Bob Sheldon produces a PowerShell script file for... Read more...