471,337 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 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 2013
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Eric | last post: by
9 posts views Thread by ckerns | last post: by
7 posts views Thread by Edward Diener | last post: by
42 posts views Thread by blisspikle | last post: by
18 posts views Thread by joaotsetsemoita | last post: by
6 posts views Thread by TriFuFoos | last post: by
5 posts views Thread by howa | last post: by
reply views Thread by rosydwin | last post: by

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.