Need help understanding "Activator.CreateInstance" (ASP.NET 2.0 / C#)

Posted on 2007-11-15
Last Modified: 2013-11-07
I'm trying to get this concept because I want to use it. Bascially the end result is having an interchangable data access layer (Switching from SqlClient to OracleClient for example). Below is the core code that I'm trying to understand:

      static public ArticlesProvider Instance
            _instance = (ArticlesProvider)Activator.CreateInstance(Type.GetType(DAL.SqlClient.SqlArticlesProvider));
            return _instance;

I found this snippet on the web but it's incomplete and couldn't find the rest of it. What I don't understand is what does Activator.CreateInstance do and is this really saying that it creating an instance of an entire namespace?
Question by:bemara57
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 30

Expert Comment

ID: 20295708
From msdn (
Activator.CreateInstance Method

Creates an instance of the specified type using the constructor that best matches the specified parameters.

Not sure about ASP.

E.g., in Remoting it may means thta no server instance is created before first clien call. You do not create an object by calling constructor but rather ask activator to get an instance. Srver object may be different type so the problem is that the *type* of object to create isn't know until runtime.  Activator helps to solve the problem.

Author Comment

ID: 20299557
Thanks but I can't get it to work in my website. Here's what I did but don't understand what the problem is:

namespace Company.DAL
    public abstract class ConfigProvider
        static private ConfigProvider _instance = null;
        static public ConfigProvider Instance
                if (_instance == null)
                    _instance = (ConfigProvider)Activator.CreateInstance(
                return _instance;

        public abstract string GetConfigValue(ConfigXML site, string id, string name);

Then in the "interchangable" data access layer I have this:
namespace Company.DAL.MdbClient
    public class MdbConfig : ConfigProvider
        public override string GetConfigValue(ConfigXML site, string id, string name)
                string connString = WebConfigurationManager.ConnectionStrings[site.SettingsTable.ConnectionStringName].ConnectionString;
                string sqlCmd = "SELECT Value FROM " + site.SettingsTable.TableName + " WHERE UCASE(id)='" + id.ToUpper() + "' AND UCASE(name)='" + name.ToUpper() + "'";

                using (OleDbConnection conn = new OleDbConnection(connString))
                    OleDbCommand cmd = new OleDbCommand(sqlCmd, conn);

                    object obj = cmd.ExecuteScalar();

                    if (obj.Equals(System.DBNull.Value))
                        return string.Empty;
                        return (string)obj;
                return string.Empty;

Then in my aspx page I'm calling the method like this:
ConfigProvider.Instance.GetConfigValue(domain, "company", "Address1");

But I get this error:
Compiler Error Message: CS0119: 'Company.DAL.MdbClient.MdbConfig' is a 'type', which is not valid in the given context
Source Error:
Line 21:                 if (_instance == null)
Line 22:                     _instance = (ConfigProvider)Activator.CreateInstance(
Line 23:                        Type.GetType(Company.DAL.MdbClient.MdbConfig));
Line 24:                 return _instance;
Line 25:             }
What am I doing wrong??

Accepted Solution

adam_ingeniti earned 500 total points
ID: 20318010
I think you're mostly correct - you just need to use


instead of


... GetType() is only a valid method on actual INSTANCES...  whereas typeof() works when you're referring to an actual TYPE.

Try it and see if your code works?

            (new DataBinder()).GetType();

Author Comment

ID: 20327604
That worked! Thanks for the help, it's exactly how I needed it to work.

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question