473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assign value to an object created by a reference

I want to setup properties value to an object created by reference.

I do the following.

T t = Activator.CreateInstance<T>();
Type newItem = typeof(T);
newItem.GetProperty("PropertyA").SetValue(t, "Hello", null);

It works fine, but I see that there is a GetProperties method that returns
all properties available for a certain
type, and I suppose that instead calling a getproperty every time like the
code above, modifying the value to the
array of PropertyInfos it should be faster and with less memory consuming.

Unfortunately when I try to do something like that

PropertyInfors[] pi = newItem.GetProperties(MyBindingValues);
pi.SetValue("Hello", 0);

the compiler at run-time say that I cannot do something like that and throw
an InvalidArgumentCastException.

I was looking for some sample over the net, but nothing usefull. Someone
can let me understand?

Thank you
Andrea

Feb 22 '06 #1
6 2077
"andrea" <go**********@freemail.it> a écrit dans le message de news:
9f**************************@news.microsoft.com...

| T t = Activator.CreateInstance<T>();
| Type newItem = typeof(T);
| newItem.GetProperty("PropertyA").SetValue(t, "Hello", null);
Hi Andrea, I hadn't forgotten you, I was trying to get the time to create an
example and am about half done.

You are on the right track here and, yes, you do need to use
GetProperties().

| Unfortunately when I try to do something like that
|
| PropertyInfors[] pi = newItem.GetProperties(MyBindingValues);
| pi.SetValue("Hello", 0);

Shoould be something like this :

PropertyInfo[] propInfos = typeof(T).GetProperties();
foreach (PropertyInfo pi in propInfos)
{
pi.SetValue("Hello", null);
...

....except you would have to get the property names from a list and then get
the data from a dataset.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 22 '06 #2
J>Hi Andrea, I hadn't forgotten you,
Don't worry, meanwhile I used your suggestion and finally I got a working
code.

J> PropertyInfo[] propInfos = typeof(T).GetProperties();
J> foreach (PropertyInfo pi in propInfos)
J> {
J> pi.SetValue("Hello", null);
J> ...

About your sample, this mean that use the recursion while getting data from
db that
should be necessary casted to the right type isn't the suitable solution.
Is it right?

I suppose that make a lot call as I do or do it in a cycle, internally do
the same things?
Right?

Bye
Andrea

Feb 22 '06 #3
Joanna ... It doesn't work. I continue get A TargetException ... and I don't
know.
I use the same variable used for the extended item.GetProperties().SetValue
....

Thanks
Andrea


Feb 22 '06 #4
"andrea" <go**********@freemail.it> a écrit dans le message de news:
9f**************************@news.microsoft.com...

| Joanna ... It doesn't work. I continue get A TargetException ... and I
don't
| know.
| I use the same variable used for the extended
item.GetProperties().SetValue

I'm sorry, but you will have to explain better what the problem is. Please
post the code and point out *exactly* the point that is causing the error.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 22 '06 #5
The problem is in the call to pi.SetValue(). The PropertyInfo object is
pure reflective data; in other words, it knows only about a type, and
nothing about any particular instance of that type. Look at how you got
it:

PropertyInfo[] propinfos = typeof(T).GetProperties();

So, when you call SetValue, you have to say for which object (of type
T) you want to set the property value. In other words, your call has to
look something more like this:

pi.SetValue(t, "Hello", null);

where t is the original object created in andrea's example:

T t = Activator.CreateInstance<T>();

Feb 23 '06 #6
BW> PropertyInfo[] propinfos = typeof(T).GetProperties();

that's the point. I was passsing the type itself and not the instance object.
Sorry for the mistake and thanks for the reply

Bye
Andrea

Feb 23 '06 #7

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

Similar topics

4
12540
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
9
2914
by: ckerns | last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0). If value is null or non-numeric I want to assign the value of "0". rowString = "L411" //conrol name if (isNaN(eval...
16
25390
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
1
18134
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
7
2501
by: Edward Diener | last post by:
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable of the same type or base class of that type, I assume that...
42
1900
by: blisspikle | last post by:
I tried closely copying some code that I found on this group for assigning a type at runtime, but I cannot get it to work. Can someone see what is wrong with my logic? Thanks, Private Sub...
18
3912
by: joaotsetsemoita | last post by:
Hello everyone, I'm having troubles assigning an onclick event to a cell. Im trying something like cursorPoint.cells.style.cursor = "hand"; cursorPoint.cells.width = "20";...
6
2608
by: TriFuFoos | last post by:
Hi there, I was wondering if anyone knew if/how to assign an event to a global variable? I tried to do it and IE 7 came back with an error saying "Member not found" My code looked similar to...
5
8063
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
0
7278
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
7328
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...
1
6988
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...
0
5578
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,...
1
5011
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...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
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...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
379
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...

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.