Home All Groups Group Topic Archive Search About

How to instance type of configuration

Author
13 Oct 2006 4:25 PM
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

Author
13 Oct 2006 4:33 PM
Carl Daniel [VC++ MVP]
"Enosh Chang" <changen***@yahoo.com.tw> wrote in message
news:%23Q1bvQu7GHA.3644@TK2MSFTNGP03.phx.gbl...
> 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
Author
13 Oct 2006 4:41 PM
Massimo
Show quote
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
ha scritto nel messaggio news:uHXRJVu7GHA.4500@TK2MSFTNGP02.phx.gbl...

>> 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);

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
Author
13 Oct 2006 11:26 PM
Enosh Chang
"Massimo" <bar***@mclink.it>
???????:%23LFqsZu7GHA.4***@TK2MSFTNGP05.phx.gbl...
Show quote
> "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
> ha scritto nel messaggio news:uHXRJVu7GHA.4500@TK2MSFTNGP02.phx.gbl...
>
>>> 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);
>
> 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?

AddThis Social Bookmark Button