473,396 Members | 1,996 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,396 software developers and data experts.

Reflection Vs Events - Performance

HL
The requirement is to send some information to other objects. The objects to
whom the information has to be sent is not available at compile time. The
names of the types (objects) will be provided through an external file in
which case we can use reflection to create objects of that type at run time.

Case 1:
The methods of those types can be invoked through reflection.

Case 2:
The caller can publish certain events. The types (objects) can subscribe for
the interested events.

Which of the following is faster?
Calling a method through reflection or Firing events to the objects?
Dec 8 '05 #1
3 2310
With case 2 (objects subscibing to the caller's events), you'd still need a
way of passing the object reference to the types in order for them to
register with the events.

Personally I don't think reflection is a great idea here: how about a third
way? Could you make the objects implement a specific interface, and then
fire through that? Maximum speed, plus full type safety etc.

You would presumably need to compile your interface class into a separate
(pref. strongly named) assembly, and reference it from your main program and
from each of the implementing objects. You'd then essentially have an
interface-based class-factory, so you'd be doing something like:

IMyInterface obj = (IMyInterface)
SomeMethodThatCreatesObjectsFromYourExternalAssemb ly();
obj.SomeMethod();

(assuming that your IMyInterface exposes a void SomeMethod() function)

Marc

"HL" <HL@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
The requirement is to send some information to other objects. The objects
to
whom the information has to be sent is not available at compile time. The
names of the types (objects) will be provided through an external file in
which case we can use reflection to create objects of that type at run
time.

Case 1:
The methods of those types can be invoked through reflection.

Case 2:
The caller can publish certain events. The types (objects) can subscribe
for
the interested events.

Which of the following is faster?
Calling a method through reflection or Firing events to the objects?

Dec 8 '05 #2
HL
Hi Marc,

Thanks for the immediate response.

I need to give more details for clarity. Actually the objects do not
share a common functionality. They need to be called by the caller for
various purposes. Lets say ObjectA's - 'methodA' needs to be called when the
user performs 'ActionA'. ObjectB's - 'methodB' needs to be called when the
user performs 'ActionB'. Also, there could be more than one object
implementing 'methodB' functionality.

Considering the above, I thought I could have a singleton class (say,
Publisher) that exposes methods and publishes events. The callee objects can
instantiate the singleton publisher and register for the events that they are
interested in. The caller can get the Singleton Publisher and call a method
indicating that 'ActionA' has happened. The Publisher can then fire events to
all the registered clients.

Will this approach hold good for the above mentioned scenario? Or Is
there a better approach to this problem?

Since the objects do not have a common functionality, I may not be able
to use the interface approach.

Thanks.

"Marc Gravell" wrote:
With case 2 (objects subscibing to the caller's events), you'd still need a
way of passing the object reference to the types in order for them to
register with the events.

Personally I don't think reflection is a great idea here: how about a third
way? Could you make the objects implement a specific interface, and then
fire through that? Maximum speed, plus full type safety etc.

You would presumably need to compile your interface class into a separate
(pref. strongly named) assembly, and reference it from your main program and
from each of the implementing objects. You'd then essentially have an
interface-based class-factory, so you'd be doing something like:

IMyInterface obj = (IMyInterface)
SomeMethodThatCreatesObjectsFromYourExternalAssemb ly();
obj.SomeMethod();

(assuming that your IMyInterface exposes a void SomeMethod() function)

Marc

"HL" <HL@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
The requirement is to send some information to other objects. The objects
to
whom the information has to be sent is not available at compile time. The
names of the types (objects) will be provided through an external file in
which case we can use reflection to create objects of that type at run
time.

Case 1:
The methods of those types can be invoked through reflection.

Case 2:
The caller can publish certain events. The types (objects) can subscribe
for
the interested events.

Which of the following is faster?
Calling a method through reflection or Firing events to the objects?


Dec 9 '05 #3
If you can't use interfaces, then given the extra detail: yes I would
*imagine* that the event-based solution would be quicker and more versatile
than reflection - of course, your singleton would then have to be in a
bottom-level assembly (to be referenced by both the primary and external
assemblies), but that's not an issue. If core performance is your primary
concern (real-time system or something) then I would advise simply testing
both in a race, but to me events are a much cleaner solution.

Kind of off-topic (and my own view: form your own opinioin), but
*personally*, I always think of reflection as a last-ditch effort. I'll
happily use reflection at a UI level to simplify displaying / editing
properties (e.g. data-binding on a form, that kind of thing), and I
routinely use a reflection-based popup dialog (even in production code
running as an admin) to review all of the runtime properties of an object
(for debugging / fault identification), but if I find myself using
reflection to invoke methods I always ask: "am I sure I want to do this?".
Sometimes I am, but usually there is a better route.

Marc

"HL" <HL@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
Hi Marc,

Thanks for the immediate response.

I need to give more details for clarity. Actually the objects do not
share a common functionality. They need to be called by the caller for
various purposes. Lets say ObjectA's - 'methodA' needs to be called when
the
user performs 'ActionA'. ObjectB's - 'methodB' needs to be called when the
user performs 'ActionB'. Also, there could be more than one object
implementing 'methodB' functionality.

Considering the above, I thought I could have a singleton class (say,
Publisher) that exposes methods and publishes events. The callee objects
can
instantiate the singleton publisher and register for the events that they
are
interested in. The caller can get the Singleton Publisher and call a
method
indicating that 'ActionA' has happened. The Publisher can then fire events
to
all the registered clients.

Will this approach hold good for the above mentioned scenario? Or Is
there a better approach to this problem?

Since the objects do not have a common functionality, I may not be
able
to use the interface approach.

Thanks.

"Marc Gravell" wrote:
With case 2 (objects subscibing to the caller's events), you'd still need
a
way of passing the object reference to the types in order for them to
register with the events.

Personally I don't think reflection is a great idea here: how about a
third
way? Could you make the objects implement a specific interface, and then
fire through that? Maximum speed, plus full type safety etc.

You would presumably need to compile your interface class into a separate
(pref. strongly named) assembly, and reference it from your main program
and
from each of the implementing objects. You'd then essentially have an
interface-based class-factory, so you'd be doing something like:

IMyInterface obj = (IMyInterface)
SomeMethodThatCreatesObjectsFromYourExternalAssemb ly();
obj.SomeMethod();

(assuming that your IMyInterface exposes a void SomeMethod() function)

Marc

"HL" <HL@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
> The requirement is to send some information to other objects. The
> objects
> to
> whom the information has to be sent is not available at compile time.
> The
> names of the types (objects) will be provided through an external file
> in
> which case we can use reflection to create objects of that type at run
> time.
>
> Case 1:
> The methods of those types can be invoked through reflection.
>
> Case 2:
> The caller can publish certain events. The types (objects) can
> subscribe
> for
> the interested events.
>
> Which of the following is faster?
> Calling a method through reflection or Firing events to the objects?
>
>


Dec 9 '05 #4

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

Similar topics

5
by: BK | last post by:
Hi, I have a class which has a lot of events (>100). For some reasons, I have to go through all invocation lists to do something. What I'm wondering is that, is there any way to use reflection...
0
by: James Cohen | last post by:
Hi, I hope somebody can help me out, Im looking for a way to retreive the original filename & line number of a Reflection.MemberInfo instance. Ill provide some background on what it is that im...
2
by: Wiktor Zychla | last post by:
Let's start with an example: if you have: class Test { public delegate int MyDelegate( int n ); event MyDelegate MyEvent; } then you can get MyEvent using reflection:
2
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
4
by: .NET VB NewBEE | last post by:
hi after going thru some articles i'm confused with "How to implement Reflection in my program" my requirement is to dynamically load dll(whih got a specific Interface) files from the same...
5
by: Charles Bazi | last post by:
Hi, I have a base UserControl named UserControlEx. I have created an event, and a dummy handler. Then I create several UCs with UserControlEx base. Some of those have a handler for my custom...
2
by: mswlogo | last post by:
I looked high and low for code to do this and finally found some VB code that did it right. This is a C# flavor of it. public event EventHandler<EventArgsMyEventToBeFired; public void...
8
by: rowe_newsgroups | last post by:
I know this has to be answered in the archives somewhere, but the search results are just confusing me more :-( Anyways, I authored a control library that contains an Interface definition and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.