How to instance type of configuration

E

Enosh Chang

Hi all:

I saw there is one line on configuration file as following,

<add name="MyProfileProvider"
connectionStringName="MyDatabase"
applicationName="/"
type="System.Web.Profile.SqlProfileProvider, System.Web" />

I want to know how to instance type in my code. Thanks!

Best Regards
Enosh Chang
 
C

Carl Daniel [VC++ MVP]

Enosh Chang said:
Hi all:

I saw there is one line on configuration file as following,

<add name="MyProfileProvider"
connectionStringName="MyDatabase"
applicationName="/"
type="System.Web.Profile.SqlProfileProvider, System.Web" />

I want to know how to instance type in my code. Thanks!

I think you're asking how to create an instance of a type knowing only the
name of the type. In that case:

Type ty = Type.GetType(
"System.Web.Profile.SqlProfileProvider, System.Web"
);
System.Web.SqlProfileProvider prov
= (System.Web.SqlProfileProvider)Activator.CreateInstance(ty);

If that wasn't what you were looking for, post back with a more detailed
question.

-cd
 
M

Massimo

I think you're asking how to create an instance of a type knowing only the
name of the type. In that case:

Type ty = Type.GetType(
"System.Web.Profile.SqlProfileProvider, System.Web"
);
System.Web.SqlProfileProvider prov
= (System.Web.SqlProfileProvider)Activator.CreateInstance(ty);

You probably should use an interface or an abstract class here: if you're
reading the type name from a configuration file, chanches are you just don't
know the actual type of the object, so you can't give a type to "prov".


Massimo
 
E

Enosh Chang

Massimo said:
You probably should use an interface or an abstract class here: if you're
reading the type name from a configuration file, chanches are you just
don't know the actual type of the object, so you can't give a type to
"prov".


Massimo

That's so great. But I encounter another problem that it displays "Could not
load file or assembly 'XXXXX' or one of its dependencies" for some types.
How to solve this problem, dynamic loading assemblies?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top