How to Instantiate a Class a runtime from a String value


Author
Message
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Ivan,

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

Ivan George Borges
Ivan George Borges
Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)
Group: StrataFrame MVPs
Posts: 1.9K, Visits: 21K
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!

Paul Chase
Paul Chase
Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)
Group: Forum Members
Posts: 414, Visits: 2.8K
BigGrin You have that right!!
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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

Paul Chase
Paul Chase
Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)
Group: Forum Members
Posts: 414, Visits: 2.8K
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.

Ivan George Borges
Ivan George Borges
Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)
Group: StrataFrame MVPs
Posts: 1.9K, Visits: 21K
Right ... after understanding Serialization, this is my next challenge.Hehe
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
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).

Paul Chase
Paul Chase
Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)
Group: Forum Members
Posts: 414, Visits: 2.8K
Thanks Ben,

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

Paul 

StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
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

Paul Chase
Paul Chase
Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)
Group: Forum Members
Posts: 414, Visits: 2.8K
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

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search