How do i create a components at runtime in C#

  • Thread starter Thread starter Julia
  • Start date Start date
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.
 
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.
 
"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.
 
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.
 
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?
 
Back
Top