How do i create a components at runtime in C#

J

Julia

Hi,

My system uses Mail Client component
the Mail Client can either wrap exchange or pop3\SMTP server functions

in order to do that I declared an abstract named class MailClient
and create two components in two different DLL which derive from MailClient

ExchangeImpl.dll
Pop3Impl.dll

In the server I define variable

MailClient mailClient;

Now I want to create a specific implementation according to configuration
or user choice

How do I create a specific mail client at run time?

Thanks.
 
J

Jon Skeet [C# MVP]

Julia said:
My system uses Mail Client component
the Mail Client can either wrap exchange or pop3\SMTP server functions

in order to do that I declared an abstract named class MailClient

Where does this class live? It should be in another, separate DLL which
both ExchangeImpl and Pop3Impl reference.
and create two components in two different DLL which derive from MailClient

ExchangeImpl.dll
Pop3Impl.dll

In the server I define variable

MailClient mailClient;

Now I want to create a specific implementation according to configuration
or user choice

How do I create a specific mail client at run time?

Load the appropriate assembly using Assembly.Load, get a reference to
the type itself using Assembly.GetType, and then create an instance of
the type using Activator.CreateInstance.
 
J

Julia

"Where does this class live? It should be in another, separate DLL which
both ExchangeImpl and Pop3Impl reference"

It does of course.

"Load the appropriate assembly using Assembly.Load, get a reference to
the type itself using Assembly.GetType, and then create an instance of
the type using Activator.CreateInstance."


Thanks.
 
J

Julia

BTW can the name of the type which passed to GetType be the Base class name?



Julia said:
"Where does this class live? It should be in another, separate DLL which
both ExchangeImpl and Pop3Impl reference"

It does of course.

"Load the appropriate assembly using Assembly.Load, get a reference to
the type itself using Assembly.GetType, and then create an instance of
the type using Activator.CreateInstance."


Thanks.
 
J

Jon Skeet [C# MVP]

Julia said:
BTW can the name of the type which passed to GetType be the Base
class name?

Well, that would return the base type rather than the actual type, so
when you then called Activator.CreateInstance it would create an
instance of that base type (if it could). How would it know which
derived type to construct?
 

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