Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Create Instance of an Interface

Posted on 2005-02-25
Medium Priority
Last Modified: 2006-11-17
I thought this would be easy :) And it probably is, but I've searched and can't find anything...
Isn't there an equivalent for "getClassForName" from Java?

I have an interface, but don't know what specific class I want to load until runtime. Sure you've heard this before. I want to grab the string of the name from the Web.config file and dynamically create a class of that type...

For example,

public static IServiceProvider GetServiceProvider()
                  if (serviceProvider == null)
                        String ServiceProviderObjectName = GlobalConfigurations.ServiceProviderType;
                        serviceProvider = (IServiceProvider)Activator.CreateInstance(null,ServiceProviderObjectName );                        

                  return serviceProvider ;

What am I missing? And if it's all that stuff about assemblies, etc. Could someone give me the 2 minute drill on what that stuff is...:)

Question by:cdfllc
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

Accepted Solution

gena17 earned 1000 total points
ID: 13406284

You can use:

yourObject = (IServiceProvider)Activator.CreateInstance(Type.GetType(nameOfYourClassIncludeNamespacesString));


Author Comment

ID: 13406539
I could have sworn that I tried that!. Oh well. It works!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 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