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

Refelection - Dynamic programming

Hello,

I have to explain my problem with an example :

public class DecisionTables
{
public ObjectName ObjectName; // ObjectName defined in
other .cs
public DecisionTables()
{
functX();
}
public functX()
{
// Here I read from a database a name of an object and
put it in a string (normally in a loop)
string whichobject = "ObjectName";

// Create Instance of whichobject
Type collectieType = Type.GetType(whichobject );
Object objCollectie = System.Activator.CreateInstance
(collectieType);

// My problem : I will dynamically set the reference
of this new object to this.ObjectName.

// Not dynamically I do the following
this.ObjectName = (ObjectName)objCollectie;

// I don't want this, I think first I have to search for
a member in DecisionTables of the type ObjectName and then
I will give it a reference. But also the parsing must by
dynamically (I have to do the parsing).

}

Thanks,
Nicky
Nov 15 '05 #1
4 2066
// I don't want this, I think first I have to search for
a member in DecisionTables of the type ObjectName and then
I will give it a reference. But also the parsing must by
dynamically (I have to do the parsing).


You can get a FieldInfo reference representing the
DecisionTables.ObjectName field with typeof(DecisionTables).GetField()
or .GetFields(). You can then assign objCollectie to it with
FieldInfo.SetValue().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
Hello,

Thanks for the answer, but I still have a little problem.
I did the following and it works :

FieldInfo fld = typeof(DecisionTables).GetField
("ObjectName");
fld.SetValue(this.ObjectName ,objCollectie);
Only this.ObjectName is not dynamical.

Do I have to make an instance of it like this :
newobj= System.Activator.CreateInstance(Type.GetType
(strobjectname));

and then

fld.SetValue(newobj ,objCollectie);

Why instantiate a new object of it? In my not dynamically
example I did not instantiate the object:
this.ObjectName = (ObjectName)objCollectie;

Do i mis somethin essential here?

Thanks for help,
Nicky

-----Original Message-----
// I don't want this, I think first I have to search for a member in DecisionTables of the type ObjectName and then I will give it a reference. But also the parsing must by
dynamically (I have to do the parsing).
You can get a FieldInfo reference representing the
DecisionTables.ObjectName field with typeof

(DecisionTables).GetField()or .GetFields(). You can then assign objCollectie to it withFieldInfo.SetValue().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
.

Nov 15 '05 #3
Nicky <ni**************@pwc.be> wrote:
Thanks for the answer, but I still have a little problem.
I did the following and it works :

FieldInfo fld = typeof(DecisionTables).GetField
("ObjectName");
fld.SetValue(this.ObjectName ,objCollectie);


That's very strange - you *should* be doing:

fld.SetValue (this, objCollectie);

You're setting the field within *this* object, not within the one
referred to by this.ObjectName. I'm very surprised that worked at all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
What I just wrote, is not good. In this case I have to
standalone objects and thats not what I want.

Only in
fld.SetValue(this.ObjectName,objCollectie);

this.ObjectName must be dynamically, because somtimes it
can be this.ObjectName2 or this.ObjectName3 ....

like objcollectie depends of the string whichobject. alse
this.ObjectName depends if a string lest say Whichobject2.

Thanks in advance
-----Original Message-----
Hello,

Thanks for the answer, but I still have a little problem.
I did the following and it works :

FieldInfo fld = typeof(DecisionTables).GetField
("ObjectName");
fld.SetValue(this.ObjectName ,objCollectie);
Only this.ObjectName is not dynamical.

Do I have to make an instance of it like this :
newobj= System.Activator.CreateInstance(Type.GetType
(strobjectname));

and then

fld.SetValue(newobj ,objCollectie);

Why instantiate a new object of it? In my not dynamically
example I did not instantiate the object:
this.ObjectName = (ObjectName)objCollectie;

Do i mis somethin essential here?

Thanks for help,
Nicky

-----Original Message-----
// I don't want this, I think first I have to search for a member in DecisionTables of the type ObjectName and then I will give it a reference. But also the parsing must by dynamically (I have to do the parsing).


You can get a FieldInfo reference representing the
DecisionTables.ObjectName field with typeof

(DecisionTables).GetField()
or .GetFields(). You can then assign objCollectie to it

with
FieldInfo.SetValue().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
.

.

Nov 15 '05 #5

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

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
11
by: Neuruss | last post by:
I've been reading an article published in E-Week entitled "Microsoft Lures Open-Source Programmer", which contains a definition for dynamic languages as follows: "Dynamic programming languages...
5
by: The Directive | last post by:
Does C++ support dynamic programming? I hope I'm using the correct term. I want to write code that can dynamically rewrite itself! I want to dynamically create functions and call them and etc. If...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
2
by: Thomas | last post by:
Hello, I'm programming a program, where I don't know which objects are needed. So I want to use the mechanism of reflection. I can create the objects I need by reflection, but there are some...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
0
by: alexandre.bergel | last post by:
Dear colleges, You might want to consider Dyla'07 as a good venue to present your work and your favourite programming language. Regards, Alexandre ...
5
by: Guillermo Schwarz | last post by:
C++ers, I have programmed in C++ since 1991 and I probably created my best abstraction layers in C++ in those days. Since 2001 I've been programming in Java (at first against my will, then I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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...
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,...

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.