Trouble creating object by name

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have some code below that is trying to instantiate a form via a string containing the class name of the form
The assembly by 'partial name' seems to be ok, it returns an object
The GetType(string) is returning Nothing.
I can type in the string as is in the Command Window or Code Window and intellisense is showing the properties and methods ("PECS.SelectPOTypeForm")

I've also tried the same with
System.Reflection.Assembly.GetExecutingAssembly.GetTyp
since 'PECS' is the name of the executing assembly
Does this have anything to do with my form being a 'RunTimeType'

thanks
tro
=============== My Code...========

Private Function CreateClassByName(ByVal PartialAssemblyName As String, ByVal FormName As String) As Objec
Dim QualifiedClassName As String = "PECS." & FormNam
Return Activator.CreateInstance(
System.Reflection.Assembly.LoadWithPartialName("PECS").GetType(QualifiedClassName)

End Functio
==============================
=============== From Debug Window =
?gettype(PECS.SelectPOTypesForm).Typehandl
{System.RuntimeTypeHandle
Value: 14763622
?System.Reflection.Assembly.LoadWithPartialName("PECS").GetType(QualifiedClassName
Nothin
==============================
 
Thanks Ken
I tried your suggestion and it works great (code is below if anyone else wants to see it or comment further

I have some events on the form that I tried to add handlers to..
This next line fails since the Event is not defined for type System.Objec
AddHandler obj.MyEvent, AddressOf MyHandle

I was thinking of something along these lines..
dim TheObj as [tried several here] '(how to dim as a variable type?
TheObj = DirectCast(obj, [tried several here]
AddHandler TheObj.MyEvent, AddressOf MyHandle
but I'm having problems with the dim and/or DirectCast. I've tried MyType, MyType.GetType(), MyType.FullName and some others, but they all end up with 'type MyType is not defined'. Its of type 'Type'?

I can work around this with the changes I'm doing but it would be awsome to be able to handle the events too
I'll fool around with it a little more. Any suggestions on the handlers anyone

thx
tro

=========================== MY NEW CODE, THANKS =====

Dim objAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembl
Dim myTypes As System.Type() = objAssembly.GetTypes(
Dim mytype As System.Type 'System.Reflection.MemberInfo.typ
Dim obj As Object 'System.Windows.Forms.For
For Each mytype In myTypes ' look at each on
If mytype.Name = FormName Then ' is it the form ?
obj = objAssembly.CreateInstance(mytype.FullName) ' if so instantiate i
'NEED TO CAST OBJ AS THE ACTUAL TYPE HER
'NEED TO ADD EVENT HANDLER(S) HER
obj.ShowDialog(Me
End I
Nex
==============================================
 
Back
Top