StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      



How to Instantiate a Class a runtime from a...Expand / Collapse
Author
Message
Posted 10/25/2006 3:59:40 PM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 09/05/2008 9:57:46 AM
Posts: 354, Visits: 2,247
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

Post #3847
Posted 10/25/2006 4:13:51 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Yesterday @ 10:38:53 PM
Posts: 2,683, Visits: 1,883
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


www.bungie.net
Post #3849
Posted 10/26/2006 7:48:22 AM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 09/05/2008 9:57:46 AM
Posts: 354, Visits: 2,247
Thanks Ben,

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

Paul 

Post #3856
Posted 10/26/2006 8:16:11 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Yesterday @ 10:38:53 PM
Posts: 2,683, Visits: 1,883
No problem

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).


www.bungie.net
Post #3859
Posted 10/26/2006 11:42:13 AM


Advanced StrataFrame User

Advanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame User

Group: StrataFrame Users
Last Login: 2 days ago @ 2:02:58 PM
Posts: 644, Visits: 10,954
Right ... after understanding Serialization, this is my next challenge.
Post #3862
Posted 10/26/2006 1:54:10 PM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 09/05/2008 9:57:46 AM
Posts: 354, Visits: 2,247
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.

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

Now I am struggling with type converters.

Post #3870
Posted 10/26/2006 1:55:57 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Yesterday @ 4:58:13 AM
Posts: 4,379, Visits: 4,421
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

Post #3872
Posted 10/26/2006 2:23:26 PM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 09/05/2008 9:57:46 AM
Posts: 354, Visits: 2,247
You have that right!!
Post #3879
Posted 10/26/2006 3:16:21 PM


Advanced StrataFrame User

Advanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame UserAdvanced StrataFrame User

Group: StrataFrame Users
Last Login: 2 days ago @ 2:02:58 PM
Posts: 644, Visits: 10,954
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.

Should I seet and wait then?

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!

Post #3880