Assign value to an object created by a reference

A

andrea

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
Andre
 
J

Joanna Carter [TeamB]

"andrea" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
A

andrea

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
Andre
 
A

andrea

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
 
J

Joanna Carter [TeamB]

"andrea" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
B

Bruce Wood

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>();
 
A

andrea

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top