473,320 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reflection question

JMZ
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.
Nov 21 '05 #1
4 1297
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
JMZ
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
SomeWayToGetTheAddressOfTheMethodWhoseNameIsInThis DataRow)

Next

Hopefully, this mess makes sense to you.

Thanks again
"Mattias Sjögren" wrote:
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #3
>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #4
JMZ
Mattias,

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

Many thanks!

"Mattias Sjögren" wrote:
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
2
by: Dan | last post by:
Let's say I have a class like: class Dummy { public const string CONE = "one"; public const string CTWO = "two"; ... other stuff .... }
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
1
by: Pieter Breed | last post by:
Hi All, I am trying to use the attribute/reflection system to as much potential as I can think of, but I've run into a snag. I would appreciate it if someone would point me in the right...
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.