How to instance type of configuration

  • Thread starter Thread starter Enosh Chang
  • Start date Start date
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
 
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
 
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
 
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?
 
Back
Top