Create an instance of an object from its type name??

Merrion

Junior Contributor
Joined
Sep 29, 2001
Messages
265
Location
Dublin, Ireland
OK - I have a plug in framework where my application can have 3rd party plug-ins added via the .config file (provided they confrom to the IPlugIn interface...)

What I have so far reads the Type Name and checks if it supports the interface OK - but how do I instantiate an object of that type if it does?

I have.....
App.config
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
      <!-- Extra custom configuration sections -->
      <section name="printerEventListeners" 
type="PrinterMonitorService.CustomConfigurationSectionReaders.PrinterEventListenersSectionReader" />
     
   </configSections>
    
    <printerEventListeners>
       <!-- Key = the unique name by which the listener is known,
            ClassType = the full type name of the class implementing IPrinterEventListenerBase ,
            CommandLine = startup parameters for that class -->
       <printerEventListener key="printerEventListener1" 
           classType="PrinterMonitorService.PrinterMonitorLogfileListener"
           commandLine="c:\printerEventListener1.log" />
    </printerEventListeners>

Then in the application....
Visual Basic:
            For Each PrintJobListener In PrintJobListeners
                If PrintJobListener.ImplementingClassType.IsSubclassOf(GetType(PrintJobMonitorListenerBase)) Then
                    '\\ Add an instance of the PrintJobMonitorListenerBase derived class...
                    If ApplicationTracing.TraceVerbose Then
                        Trace.WriteLine("PrintJobMonitorListenerBase derived class: " & PrintJobListener.ImplementingClassType.ToString, Me.GetType.ToString)
                        '<-- What to put here ????
                    End If
                End If
            Next

Thanks in advance,
Duncan
 
The answer is:
Visual Basic:
Dim o As Object = Activator.CreateInstance(PrintJobListener.ImplementingClassType)

Suprisingly straight forward...
 
Back
Top