473,396 Members | 1,998 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.

Passing values to object during object instantiation using reflection, Possible?


Hi

Is it (any way) possible using reflection to set an indicator (property,
variable, attribute,..). on an object before it actually instantiated, so
that this indicator can be used in the constructor to determine how the was
created, or who created the object.. ??

TIA

Søren
Sep 15 '06 #1
4 1837
Before it is instantiated, you can't do /anything/. However, you could
perhaps provide a separate constructor that is called (by reflection)
specifying this information as a parameter?

Marc

Sep 15 '06 #2
You can overload the constructor and just call them unless you have a need to
call instantiate the object via reflection (which is one of the slowest
reflection operations). Or you could (although this too is slow) inspect the
stack trace in the constructor to see who created the object.
Or you could have a static property on the object type which you can set
with information for the constructor to use. You could make this thread safe
by marking it with the ThreadStatic attribute.

I prefer the overloaded contructor option. Its the 'normal' way of coding,
the other are for if you need them for a reason.

HTH
Ciaran O'Donnell

"Søren M. Olesen" wrote:
>
Hi

Is it (any way) possible using reflection to set an indicator (property,
variable, attribute,..). on an object before it actually instantiated, so
that this indicator can be used in the constructor to determine how the was
created, or who created the object.. ??

TIA

Søren
Sep 15 '06 #3
Thanks guys....

I've already tried the constructor way, and it works like a charme, I was
just wondering if there was a way to implement it without adding an extra
constructor...now that I'm creating the object using reflection anyways....

BTW.

Is there a faster way then reflection to create objects dynamically based on
their type name ???

"Søren M. Olesen" <sm******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
Hi

Is it (any way) possible using reflection to set an indicator (property,
variable, attribute,..). on an object before it actually instantiated, so
that this indicator can be used in the constructor to determine how the
was
created, or who created the object.. ??

TIA

Søren


Sep 15 '06 #4
You can generate code, say as static function on a class, using the CodeDom
and compile it in memory (only in .NET2, in one it compiles to a file), then
load it as an assembly and call the function. This is faster if doing few
times but probably not for just one instantiation.
This works best if you have an interface to cast it to, other wise you will
need to reflect its methods anyway.

Ciaran O'Donnell

"Søren M. Olesen" wrote:
Thanks guys....

I've already tried the constructor way, and it works like a charme, I was
just wondering if there was a way to implement it without adding an extra
constructor...now that I'm creating the object using reflection anyways....

BTW.

Is there a faster way then reflection to create objects dynamically based on
their type name ???

"Søren M. Olesen" <sm******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...

Hi

Is it (any way) possible using reflection to set an indicator (property,
variable, attribute,..). on an object before it actually instantiated, so
that this indicator can be used in the constructor to determine how the
was
created, or who created the object.. ??

TIA

Søren


Sep 15 '06 #5

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

Similar topics

2
by: Marcin | last post by:
Hello! Is there any method to detect parameters values passed to called method? For example: public Guid ApplicationLogin(string userName, string password, int dbId)
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
7
by: Dave L | last post by:
I am inquiring about the feasability of being able to determine when an object of a certain type is instantiated. I have a base type that I want to be able to determine when each instance is...
2
by: Sergey Ilinsky | last post by:
Well, I've been working with JS for three years and have a great experience here. But! I still have no really acceptable answer to the following question: What is the principle difference between...
6
by: Catherine Jones | last post by:
Hi all, we need urgent help in a matter. We are trying to pass a COM object from the client to server and are facing some problems in the same. We've our client in C# as well as the Server...
8
by: yo_mismo | last post by:
Hi, I send a parameter from a Form (Form1) to an other form (Form2): Form2 frm2 = new Form2(); frm2.number_frm2 = number_frm1; frm2.Show(); The problem i got is that the variable...
6
by: B. Chernick | last post by:
(I'm not too comfortable with inheritance in VB and I don't think I've ever read about a situation like this.) Let's assume Dot Net 1.1 and VB.Net. Let's assume a main Winforms program and a...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
6
by: Ritesh Raj Sarraf | last post by:
Hi, I've been very confused about why this doesn't work. I mean I don't see any reason why this has been made not to work. class Log: def __init__(self, verbose, lock = None): if verbose...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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.