473,808 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating instances by reflection - how expensive?

Hi All, I was wondering what peoples opinions, or experience were with
creating instances by reflection. How expensive is the following code? It is
from a factory class, I was wondering whether to create a cache of the
instances when first created & then return clones.

Command returnCommand = null;
String requestedComman d = request.getPath Info();
if(requestedCom mand != null){
try {
Class commandClass = Class.forName(r equestedCommand );
Class[] parameterTypes = new Class[0];
Constructor commandConstruc tor =
commandClass.ge tConstructor(pa rameterTypes);
Object[] initArgs = new Object[0];
returnCommand = (Command)
commandConstruc tor.newInstance (initArgs);
}
catch (ClassNotFoundE xception ....etc etc

Thanks :-)
--
-P
Jul 17 '05 #1
1 2611
That's one of those things where you'll just have to Try It And See(TM).
Run some benchmarks on different VMs and operating environments.
I'm sure they'll come in handy to yourself or someone else one day - post
them to a web site even.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
Home : +61 7 5502 7987
Work : +61 7 5552 4076
Mobile : 0408 711 099
(2003 VTR1000F)
"PerfectDayToCh aseTornados" <pd***@emailadd ress.invalid> wrote in message
news:Z9******** *****@news-binary.blueyond er.co.uk...
Hi All, I was wondering what peoples opinions, or experience were with
creating instances by reflection. How expensive is the following code? It is from a factory class, I was wondering whether to create a cache of the
instances when first created & then return clones.

Command returnCommand = null;
String requestedComman d = request.getPath Info();
if(requestedCom mand != null){
try {
Class commandClass = Class.forName(r equestedCommand );
Class[] parameterTypes = new Class[0];
Constructor commandConstruc tor =
commandClass.ge tConstructor(pa rameterTypes);
Object[] initArgs = new Object[0];
returnCommand = (Command)
commandConstruc tor.newInstance (initArgs);
}
catch (ClassNotFoundE xception ....etc etc

Thanks :-)
--
-P

Jul 17 '05 #2

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

Similar topics

2
3571
by: Carl Rosenberger | last post by:
Hi all, I would like to create an instance of a struct with reflection. The way I am used to doing this with classes: - get a ConstructorInfo from the Type - call Invoke() However, if a struct does not have a constructor
1
2379
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 direction. First my question: How am I supposed to make instances of delegates when I have the method (that I want to make the delegate of) retrieved through reflection?
0
1046
by: Pohihihi | last post by:
Hello NG, Never used interOps for programming so for many this might be a very small problem. Basically I have 2 questions -- 1- Code given below creates WinWord.exe instance and even after application terminate MS-Word is left open. If I use task manager to see how many instances are created then one instance is created and terminates but other stays. In other words, 2 instances are created and one is left active. I know code is...
4
1286
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
6
11092
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
1
4471
by: bowser | last post by:
Hello, I have a problem with reflection. I posted it already a month ago, but I had no luck with the answers... Having two variables: Type t; string id; initialized for example as t="Foo" and id="foo" I need to make the equivalent of :
26
5378
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
9
2987
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
19
1956
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { switch (((Sale)e.Row.DataItem).SzPN) {
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10631
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...
0
10374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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
10114
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
9196
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...
0
5548
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...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.