|
-
Sep 30th, 2003, 05:40 PM
#1
Thread Starter
Frenzied Member
Instantiate a class by its type name??
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....
VB Code:
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
-
Sep 30th, 2003, 06:23 PM
#2
I wonder how many charact
Well, im going to need some code like that shortly, so Im sure if you dont have an answer by then, I will be helping..
-
Sep 30th, 2003, 06:27 PM
#3
You can use Activator.CreateInstance if you have the full namespace or 'type' of the object you can also use reflection via the Assembly.LoadFrom method. You can also load a type via a string using Type.GetType("MyNamespace.ObjectTypeName") Sorry for such a brief post but I'm headed out the door.
-
Sep 30th, 2003, 07:23 PM
#4
Addicted Member
yep... something like this:
VB Code:
Imports System.Reflection
'Then use
Dim a As [Assembly] = [Assembly].Load("YourDllProject")
T = a.GetType("YourDllProject.MainForm", True)
'Create an instance of the form
Dim o As Object = Activator.CreateInstance(t)
'Show the form
Dim mainForm As Form = CType(o, Form)
mainForm.Show()
-
Oct 2nd, 2003, 11:06 AM
#5
I wonder how many charact
Ok... So if I have a tabpageclass named "AdditionalInfo", this should work (but it doesn't)
VB Code:
Dim o As Object = Activator.CreateInstanceFrom("ReportExecHelper.AdditionalInformationTabPageClass.vb", "TabPage")
myTabPage = CType(o, TabPage)
This is the error I get:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Additional information: File or assembly name ReportExecHelper.AdditionalInformationTabPageClass, or one of its dependencies, was not found.
So I switched the original line with this ..
VB Code:
Dim o As Object = Activator.CreateInstanceFrom("ReportExecHelper.dll", "AdditionalInformationTabPageClass")
And got this...
An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll
Additional information: Could not load type AdditionalInformationTabPageClass from assembly ReportExecHelper, Version=1.0.1370.15851, Culture=neutral, PublicKeyToken=null.
So what gives? All i want to do is create an instance of the object using its Name ... i.e. CreateInstance("MyForm1")
-
Oct 2nd, 2003, 11:16 AM
#6
You have to use the full namespace of the object:
Dim o As Object = Activator.CreateInstanceFrom("ReportExecHelper.dll", "RootOrOtherNamespaceHere.ObjectName")
I think you also might need to use the full path of the assembly.
Last edited by Edneeis; Oct 2nd, 2003 at 11:31 AM.
-
Oct 2nd, 2003, 11:34 AM
#7
I wonder how many charact
Yea... I don't know,I can't get it to work.
Does this only work with class libraries? (.dll's)?
So if I made a test project with two forms, I couldn't possibly call the second form using .CreateInstanceFrom("Form2")
-
Oct 2nd, 2003, 11:42 AM
#8
Actually I pulled some old code where I used this and if you use CreateInstanceFrom or CreateInstance with the assemblyname parameter then it returns an ObjectHandle which can be used in remoting (and maybe other ways too) instead of the actual object. If you were to use that you'd have to unwrap it as the type that it is, but like I said I don't know if that is specific to remoting or not. If you just want to dynamically load something try this:
VB Code:
'load assembly via the path
Dim asm As Reflection.Assembly = Reflection.Assembly.LoadFrom("E:\Code\NET\Practice\TestBed6\bin\TestBed6.exe")
'load the type from the assembly via the full namespace
Dim obj As Object = Activator.CreateInstance(asm.GetType("TestBed6.Form5"))
DirectCast(obj, Form).ShowDialog()
And no it doesn't have to be a dll.
-
Oct 2nd, 2003, 11:46 AM
#9
I wonder how many charact
I got it to work finally.
VB Code:
Dim ty As Type = Type.GetType("ReportExecHelper.AdditionalInformationTabPageClass")
myobj = Activator.CreateInstance(ty)
TabControl1.TabPages.Add(CType(myobj, TabPage))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|