Reflection question

G

Guest

I want to look up method names in a database which will be present in the
executing object and use the names to find the method address to pass to the
addressof operator at runtime.

Is that possible? If so, what reflection classes enable me to do that?

Thanks.
 
M

Mattias Sjögren

I want to look up method names in a database which will be present in the
executing object and use the names to find the method address to pass to the
addressof operator at runtime.

So in other words you want to create a delegate dynamically.

Is that possible? If so, what reflection classes enable me to do that?

System.Delegate.CreateDelegate()



Mattias
 
G

Guest

Thanks, but that did not work. CreateDelegate throws an exception when I try
to get the type of my custom type, using Type.GetType.

Here is what I want to do:

Private OneStep As StepSub
Delegate Sub StepSub()
Private Steps(ALL_STEPS) As StepSub

Eventually, I will use the Combine method on Steps:

OneStep = OneStep.Combine(Steps)

And then call all the subs contained in the original Steps array:

OneStep.DynamicInvoke(Nothing)

Before I do that however, I want to build the original super list (Steps)
based on a database table which contains the method names and their
corresponding sequence (index into the Steps array). At runtime, I will have
a structure to tell me which of the steps to leave in the Steps array and
which ones to remove (by setting the corresponding Steps entry to Nothing).

Everything works just fine except that I want to be able to build the Steps
array at runtime with no prior knowledge of the methods. Which means I want
to use a string representation of the method name and turn it into a method
address compatible with the delegate type of my methods to pass to the
AddressOf operator (or in some way resolve to a compatible value).

So instead of:

Steps(0) = New StepSub(AddressOf FirstMethod)
Steps(1) = New StepSub(AddressOf SecondMethod)
Steps(2) = New StepSub(AddressOf ThirdMethod)
....
Steps(n)= New StepSub(AddressOf LasMethod)

I want to write:
Dim ds as DataSet = GetMyListOfMethodsAndSequences
Dim dr as DataRow

For Each dr in ds.Tables(0).Rows

Steps(dr.Item("MethodStepIndex")) = new StepSub(AddressOf
SomeWayToGetTheAddressOfTheMethodWhoseNameIsInThisDataRow)

Next

Hopefully, this mess makes sense to you.

Thanks again
 
M

Mattias Sjögren

Thanks, but that did not work. CreateDelegate throws an exception when I try
to get the type of my custom type, using Type.GetType.

Can you show us that part of your code, and tell us what kind of
exception you get?

Since you have compile-time knowledge about what type of delegate to
create, you can use VB's GetType operator instead of the Type.GetType
method.



Mattias
 
G

Guest

Mattias,

Thanks very much! The VB GetType works like a champ and the code runs
exactly as I intended.

Many thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top