473,404 Members | 2,187 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,404 software developers and data experts.

Programaticly call a method from a different object?

I'm looking for a way to programaticly call a method from a different object
and associate the two objects at runtime.

Example: Object A exist and is unknow, I want object B to be able to call a
method on object A whenever needed.

Like addhandler but I don't know what the method is and must discover it
through reflection.

Maybe something like this:

Public Sub attach(ByVal valueA As Object)

Dim meth() As MethodInfo = value.GetType.GetMethods

Dim btnB As Button = New Button

Dim mitem As MethodInfo = meth(0) 'find the method I want somehow

AddHandler btnB.Click, mitem.[Selected_Method] '???

End Sub

Any ideas would be great.

Thanks,

Schneider


Jan 26 '07 #1
10 1348
Dim obj As Object = Nothing
Dim methods As MethodInfo() = obj.GetType().GetMethods()
Dim method As MethodInfo = FindTheMethodYouWant(methods)
'store class-level reference to "method" and "obj"...
mTheMethod = method
mTheObject = object
AddHandler btnB.Click, AddressOf DoTheMethod
....
end sub

private mTheMethod as Method
private mTheObject as object

sub DoTheMethod()
mTheMethod.Invoke(mTheObject, params) '(whatever the params are)
end sub

"schneider" wrote:
I'm looking for a way to programaticly call a method from a different object
and associate the two objects at runtime.

Example: Object A exist and is unknow, I want object B to be able to call a
method on object A whenever needed.

Like addhandler but I don't know what the method is and must discover it
through reflection.

Maybe something like this:

Public Sub attach(ByVal valueA As Object)

Dim meth() As MethodInfo = value.GetType.GetMethods

Dim btnB As Button = New Button

Dim mitem As MethodInfo = meth(0) 'find the method I want somehow

AddHandler btnB.Click, mitem.[Selected_Method] '???

End Sub

Any ideas would be great.

Thanks,

Schneider


Jan 26 '07 #2
Thanks Ben,

Was hoping for a cleaner approach, but that will work...

Schneider

"Ben" <Be*@discussions.microsoft.comwrote in message
news:04**********************************@microsof t.com...
Dim obj As Object = Nothing
Dim methods As MethodInfo() = obj.GetType().GetMethods()
Dim method As MethodInfo = FindTheMethodYouWant(methods)
'store class-level reference to "method" and "obj"...
mTheMethod = method
mTheObject = object
AddHandler btnB.Click, AddressOf DoTheMethod
...
end sub

private mTheMethod as Method
private mTheObject as object

sub DoTheMethod()
mTheMethod.Invoke(mTheObject, params) '(whatever the params are)
end sub

"schneider" wrote:
>I'm looking for a way to programaticly call a method from a different
object
and associate the two objects at runtime.

Example: Object A exist and is unknow, I want object B to be able to call
a
method on object A whenever needed.

Like addhandler but I don't know what the method is and must discover it
through reflection.

Maybe something like this:

Public Sub attach(ByVal valueA As Object)

Dim meth() As MethodInfo = value.GetType.GetMethods

Dim btnB As Button = New Button

Dim mitem As MethodInfo = meth(0) 'find the method I want somehow

AddHandler btnB.Click, mitem.[Selected_Method] '???

End Sub

Any ideas would be great.

Thanks,

Schneider



Jan 27 '07 #3
What is the objective? Do you want to be able to develop a plug-in for your
code, after the system is released, and have your system recognize it?

If so, that's covered loosly by traditional Design Patterns. I would
recommend you investigate the Gang of Four Design Patterns, especially
Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
patterns: http://home.earthlink.net/~huston2/dp/patterns.html )

Once you understand the design patterns that lead to frameworks, go ahead
and dive into a light framwork like Spring.Net. Here's an article on
Dependency Injection in .Net:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"schneider" <es********@starkinvestments.cemwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
I'm looking for a way to programaticly call a method from a different
object and associate the two objects at runtime.

Example: Object A exist and is unknow, I want object B to be able to call
a method on object A whenever needed.

Like addhandler but I don't know what the method is and must discover it
through reflection.

Maybe something like this:

Public Sub attach(ByVal valueA As Object)

Dim meth() As MethodInfo = value.GetType.GetMethods

Dim btnB As Button = New Button

Dim mitem As MethodInfo = meth(0) 'find the method I want somehow

AddHandler btnB.Click, mitem.[Selected_Method] '???

End Sub

Any ideas would be great.

Thanks,

Schneider


Jan 29 '07 #4
No, I know how to do a plug-in design, and already using it.

Trying to wire two methods together during runtime, similar to addhandler
but method to be called is discovered by reflection.

Currently using the Ben pattern, I just thought I might me missing cleaner
language feature...

Schneider
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.comwrote in message
news:0_******************************@comcast.com. ..
What is the objective? Do you want to be able to develop a plug-in for
your code, after the system is released, and have your system recognize
it?

If so, that's covered loosly by traditional Design Patterns. I would
recommend you investigate the Gang of Four Design Patterns, especially
Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
patterns: http://home.earthlink.net/~huston2/dp/patterns.html )

Once you understand the design patterns that lead to frameworks, go ahead
and dive into a light framwork like Spring.Net. Here's an article on
Dependency Injection in .Net:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"schneider" <es********@starkinvestments.cemwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>I'm looking for a way to programaticly call a method from a different
object and associate the two objects at runtime.

Example: Object A exist and is unknow, I want object B to be able to call
a method on object A whenever needed.

Like addhandler but I don't know what the method is and must discover it
through reflection.

Maybe something like this:

Public Sub attach(ByVal valueA As Object)

Dim meth() As MethodInfo = value.GetType.GetMethods

Dim btnB As Button = New Button

Dim mitem As MethodInfo = meth(0) 'find the method I want somehow

AddHandler btnB.Click, mitem.[Selected_Method] '???

End Sub

Any ideas would be great.

Thanks,

Schneider



Jan 29 '07 #5
What's wrong with Visitor or Chain of Responsibility? You can set them up
at runtime.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"schneider" <es********@starkinvestments.cemwrote in message
news:eY**************@TK2MSFTNGP05.phx.gbl...
No, I know how to do a plug-in design, and already using it.

Trying to wire two methods together during runtime, similar to addhandler
but method to be called is discovered by reflection.

Currently using the Ben pattern, I just thought I might me missing cleaner
language feature...

Schneider
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.comwrote in message
news:0_******************************@comcast.com. ..
>What is the objective? Do you want to be able to develop a plug-in for
your code, after the system is released, and have your system recognize
it?

If so, that's covered loosly by traditional Design Patterns. I would
recommend you investigate the Gang of Four Design Patterns, especially
Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
patterns: http://home.earthlink.net/~huston2/dp/patterns.html )

Once you understand the design patterns that lead to frameworks, go ahead
and dive into a light framwork like Spring.Net. Here's an article on
Dependency Injection in .Net:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"schneider" <es********@starkinvestments.cemwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>>I'm looking for a way to programaticly call a method from a different
object and associate the two objects at runtime.

Example: Object A exist and is unknow, I want object B to be able to
call a method on object A whenever needed.

Like addhandler but I don't know what the method is and must discover it
through reflection.

Maybe something like this:

Public Sub attach(ByVal valueA As Object)

Dim meth() As MethodInfo = value.GetType.GetMethods

Dim btnB As Button = New Button

Dim mitem As MethodInfo = meth(0) 'find the method I want somehow

AddHandler btnB.Click, mitem.[Selected_Method] '???

End Sub

Any ideas would be great.

Thanks,

Schneider




Jan 30 '07 #6
Look Nick the pattern is not the issue, I know what I want to do, and Ben
seems to understand, it's a language detail.

Either read the prior messages or scram...

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.comwrote in message
news:aa******************************@comcast.com. ..
What's wrong with Visitor or Chain of Responsibility? You can set them up
at runtime.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"schneider" <es********@starkinvestments.cemwrote in message
news:eY**************@TK2MSFTNGP05.phx.gbl...
>No, I know how to do a plug-in design, and already using it.

Trying to wire two methods together during runtime, similar to addhandler
but method to be called is discovered by reflection.

Currently using the Ben pattern, I just thought I might me missing
cleaner language feature...

Schneider
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.comwrote in message
news:0_******************************@comcast.com ...
>>What is the objective? Do you want to be able to develop a plug-in for
your code, after the system is released, and have your system recognize
it?

If so, that's covered loosly by traditional Design Patterns. I would
recommend you investigate the Gang of Four Design Patterns, especially
Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
patterns: http://home.earthlink.net/~huston2/dp/patterns.html )

Once you understand the design patterns that lead to frameworks, go
ahead and dive into a light framwork like Spring.Net. Here's an article
on Dependency Injection in .Net:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"schneider" <es********@starkinvestments.cemwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
I'm looking for a way to programaticly call a method from a different
object and associate the two objects at runtime.

Example: Object A exist and is unknow, I want object B to be able to
call a method on object A whenever needed.

Like addhandler but I don't know what the method is and must discover
it through reflection.

Maybe something like this:

Public Sub attach(ByVal valueA As Object)

Dim meth() As MethodInfo = value.GetType.GetMethods

Dim btnB As Button = New Button

Dim mitem As MethodInfo = meth(0) 'find the method I want somehow

AddHandler btnB.Click, mitem.[Selected_Method] '???

End Sub

Any ideas would be great.

Thanks,

Schneider




Jan 30 '07 #7
Is the method set that B needs to call a known, finite set? If so, can you
have A's type implement an interface? That's generally how a plug-in is
implemented, but if you're talking about wiring up objects of any type based
on configuration values (I have an app that does this because it has to wire
up a remotable event-listening system but predates WS-*), then you've got to
you reflection to get the MethodInfo and call Invoke. The call is
A.GetType().GetMethod("MethodName").Invoke(...)


Jan 31 '07 #8
You completely missed the point, I'm afraid.

In most cases, reflection is not necessary. A better design removes the
need for the crutch. That's not to say that reflection is somehow bad, but
when used as an alternative to simple, easily described patterns that use
interfaces, declare intent, and are managed by the developer, and not 'self
modifying code', reflection can open the door to a wide array of design
issues that can make maintenance expensive.

I'm not criticizing the code. I'm expressing reservations about the need to
use it.

If a gentleman comes to my door with his arms full of packages, opening the
door is polite. If the packages are on fire, opening the door is quite
inconsiderate and potentially self-destructive. You have asked me to open
the door. I'm smelling smoke.

--
--- Nick Malik [Microsoft]
Enterprise Application Architect
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"schneider" <es********@starkinvestments.cemwrote in message
news:e4**************@TK2MSFTNGP05.phx.gbl...
Look Nick the pattern is not the issue, I know what I want to do, and Ben
seems to understand, it's a language detail.

Either read the prior messages or scram...

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.comwrote in message
news:aa******************************@comcast.com. ..
>What's wrong with Visitor or Chain of Responsibility? You can set them
up at runtime.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"schneider" <es********@starkinvestments.cemwrote in message
news:eY**************@TK2MSFTNGP05.phx.gbl...
>>No, I know how to do a plug-in design, and already using it.

Trying to wire two methods together during runtime, similar to
addhandler but method to be called is discovered by reflection.

Currently using the Ben pattern, I just thought I might me missing
cleaner language feature...

Schneider
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.comwrote in message
news:0_******************************@comcast.co m...
What is the objective? Do you want to be able to develop a plug-in for
your code, after the system is released, and have your system recognize
it?

If so, that's covered loosly by traditional Design Patterns. I would
recommend you investigate the Gang of Four Design Patterns, especially
Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
patterns: http://home.earthlink.net/~huston2/dp/patterns.html )

Once you understand the design patterns that lead to frameworks, go
ahead and dive into a light framwork like Spring.Net. Here's an
article on Dependency Injection in .Net:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"schneider" <es********@starkinvestments.cemwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl.. .
I'm looking for a way to programaticly call a method from a different
object and associate the two objects at runtime.
>
Example: Object A exist and is unknow, I want object B to be able to
call a method on object A whenever needed.
>
Like addhandler but I don't know what the method is and must discover
it through reflection.
>
Maybe something like this:
>
Public Sub attach(ByVal valueA As Object)
>
Dim meth() As MethodInfo = value.GetType.GetMethods
>
Dim btnB As Button = New Button
>
Dim mitem As MethodInfo = meth(0) 'find the method I want somehow
>
AddHandler btnB.Click, mitem.[Selected_Method] '???
>
End Sub
>
Any ideas would be great.
>
Thanks,
>
Schneider
>
>
>
>




Jan 31 '07 #9
No the method is not know, in fact it's unknown user/developer code/library,
and the reflection that I'm using are things used by the framework for
similar things. As I explained earlier, a plug-in pattern does not work for
me in this case, but I use it for other things. This needs to be loosely
bound to be of any value to a developer.

Schneider
"Keith Patrick" <ri*******************@nospam.hotmail.comwrote in message
news:eF**************@TK2MSFTNGP06.phx.gbl...
Is the method set that B needs to call a known, finite set? If so, can you
have A's type implement an interface? That's generally how a plug-in is
implemented, but if you're talking about wiring up objects of any type
based on configuration values (I have an app that does this because it has
to wire up a remotable event-listening system but predates WS-*), then
you've got to you reflection to get the MethodInfo and call Invoke. The
call is A.GetType().GetMethod("MethodName").Invoke(...)


Jan 31 '07 #10
In that case, I think GetType().GetMethod().Invoke() would be your best
route. As Nick mentioned in another part of the thread, though, you're
putting a lot of trust into what you're calling, basically becoming an
operating system/runtime in itself, so I'd recommend restricting the
permissions that are allowed in that execution call (run it in a .Net
security-based sandbox within your app)


Jan 31 '07 #11

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

Similar topics

0
by: ding feng | last post by:
I have got formatted files. All of these file takes this form: obj1 method1 obj2 string1 obj3 string2 argument. Of course, each file can have maximum fields as shown above, or minimun fields...
17
by: Asfand Yar Qazi | last post by:
Basically this: //file 'B.hh' class B { protected: void f() {} }; //file 'C.hh'
8
by: ThomasR | last post by:
I understand that virtual methods on inherited objects are slower than non-virtual methods because of the indirection required to support the call. However, when looking at IL code produced by...
3
by: feng | last post by:
Hi, I have a MDI container that contains a treeview and a child form. Clicking on a tree node will open the child form. Right now, the focus stays with the treeview, even after the child form...
4
by: Martin Arvidsson | last post by:
Hi! I have used the SqlCommand and DataReader to read data to a DataSet This dataset is the used to set the properties; DataMember and DataSource of the DataGridView. Now what i want to do is...
8
by: hoofbeats95 | last post by:
I don't think this should be this complicated, but I can't figure it out. I've worked with C# for several years now, but in a web environment, not with windows form. I have a form with a query...
3
by: Pawel_Iks | last post by:
I have following class definition (A.h file): class A { public: int N; int count1(int n) {n++;} int count2(int n) {n+=5;} int otherFun(int n, int (*fun)(int)); }
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.