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

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 2070
"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
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
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
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
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
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
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
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
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
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.