Question Create Instance of Class from Class Name

Bryce Gough

Active member
Joined
Mar 8, 2011
Messages
36
Location
Perth, Australia
Programming Experience
1-3
Hey Guys,

Pretty much what I have is many many classes created, now the user will type a class name and a method and the application will run that method with the supplied arguments. Now I have got it working using:

Dim method As System.Reflection.MethodInfo
method = Type.GetType("Code." & Import).GetMethod(MethodName)
method.Invoke(<New Instance Of Class>, Arguments)


But I cannot find out how to create an instance of the class from a string.
'"Code." & Import' is that full class name that I want to create as I have used it in Type.GetType, but that is just getting the method from the class and I need to create an instance of the class to invoke that method.


Never mind guys, I've fixed it! :D
I used:

Assembly.GetExecutingAssembly().CreateInstance("Code." & Import)


and it seemed to work fine :)


Thanks,
Bryce Gough
 
Last edited:
For future reference, please don't edit your existing post unless you want to change what you posted. If you want to add further information then please add a new post. It just makes things easier to follow. Also, please stick with colours that are actually readable.

Just as you're getting the appropriate MethodInfo from the Type and invoking it, so you can get the appropriate ConstructorInfo from the type and invoke that. Alternatively, you can also use the Activator.CreateInstance method.
 
Back
Top