473,756 Members | 6,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2329
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)
SomeMethodThatC reatesObjectsFr omYourExternalA ssembly();
obj.SomeMethod( );

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

Marc

"HL" <HL@discussions .microsoft.com> wrote in message
news:42******** *************** ***********@mic rosoft.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)
SomeMethodThatC reatesObjectsFr omYourExternalA ssembly();
obj.SomeMethod( );

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

Marc

"HL" <HL@discussions .microsoft.com> wrote in message
news:42******** *************** ***********@mic rosoft.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******** *************** ***********@mic rosoft.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)
SomeMethodThatC reatesObjectsFr omYourExternalA ssembly();
obj.SomeMethod( );

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

Marc

"HL" <HL@discussions .microsoft.com> wrote in message
news:42******** *************** ***********@mic rosoft.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
5307
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 to get their InvocationList without going through each event? // tedious foreach (Delegate handler in Event1.GetInvocationList()) {...} foreach (Delegate handler in Event2.GetInvocationList()) {...}
0
1472
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 doing: I have an attribute defined as follows: public class CodeAttribute : Attribute
2
6170
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
2012
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 information would then be logged to a database along with some other information about the user. Thanks in advance. Mark
4
1283
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 directory when the program comes there i read the memory leakes can come can u plz suggest the best way of implemening Reflection in this senario.Some good tutorial links will be much helpful
5
3608
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 event, others, no... I manage to have the list of events with EventInfo, but I'm unable to see if some event has a handler associated...
2
2666
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 FireEvent(Guid instanceId, string handler) { EventArgs e = new EventArgs(instanceId);
8
3533
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 then some usercontrols that implement that interface. In a separate project I want to load the dll and then load the usercontrols onto tabpages on my form. I just cannot seem to figure out how to cast the usercontrols into the interface type so I...
0
10040
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9846
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.