StrataFrame Forum

How to Instantiate a Class a runtime from a String value

http://forum.strataframe.net/Topic3847.aspx

By Paul Chase - 10/25/2006

I remember seeing how to do this using reflection but cannot remember how it was done. Below is psuedo code of what I want to do.

public sub whatever(classname as string)

dim something as new classname

end sub

Thanks

Paul

By StrataFrame Team - 10/25/2006

Here ya go... If you end up doing a lot of reflection, you'll probably want to import System.Reflection...

Private Sub DoSomething(ByVal ClassName As String, ByVal MethodName As String)
 '-- Establish locals
 Dim loObject As Object
 Dim loType As Type
 Dim loMethod As Reflection.MethodInfo

 '-- Get the type
 loType = Type.GetType(ClassName)

 '-- Create the object
 loObject = Activator.CreateInstance(loType)

 '-- Get the method
 loMethod = loType.GetMethod(MethodName)

 '-- Execute the method
 loMethod.Invoke(loObject, Nothing)
End Sub

You can also use the Object Browser in visual studio and look at everything in the System.Reflection namespace.  You'll get lots of ideas of what you might want to do Smile

By Paul Chase - 10/26/2006

Thanks Ben,

That was what I was looking for! Once I seen it the light came on. Thanks man

Paul 

By StrataFrame Team - 10/26/2006

No problem Smile

Reflection is fun because you can do literally ANYTHING with it... access private members, create instances of private types, etc.; you can even use the stuff within the Emit namespace to create dynamic types, methods, and assemblies.  However, you'll want to try to use it as sparingly as possible because it is slow... in fact, it's even slower than late binding, but there are times when it is unavoidable.  We go to great lengths within the framework to avoid the use of reflection where ever possible (hence the creation of the property descriptors within the business object... if you don't use them, then .NET uses reflection to accomplish all of the binding tasks).

By Ivan George Borges - 10/26/2006

Right ... after understanding Serialization, this is my next challenge.Hehe
By Paul Chase - 10/26/2006

Don't worry Ivan i have full faith that as soon as we get comfortable using .net MS will throw something new at us to learn.Smile

Reflection seems like macro substitution "&" in foxpro but on steriods.

Now I am struggling with type converters.

By Trent L. Taylor - 10/26/2006

Reflection seems like macro substitution "&" in foxpro but on steriods.

You are exactly right here...just a much better strong-typed version that is not as likely to blow your fingers off when you use it Smile

By Paul Chase - 10/26/2006

BigGrin You have that right!!
By Ivan George Borges - 10/26/2006

Paul Chase (10/26/2006)
Don't worry Ivan i have full faith that as soon as we get comfortable using .net MS will throw something new at us to learn.Smile

Should I seet and wait then?Tongue

Reflection seems like macro substitution "&" in foxpro but on steriods.

Oh, good to know. I will certainly bump into it soon.

Now I am struggling with type converters.

Don't think you're allowed to swear in the forum!

By Trent L. Taylor - 10/26/2006

Ivan,

You made me laugh Hehe Your posts are always enjoyable to read! Smile