471,603 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,603 software developers and data experts.

Dynamically set property

Hi there,

I'm trying to create a function to dynamically set a property from a class.
The idea is to get a general function that works for every class. Although
this function works for classes i created myself, and for standard classes,
this function does not seem to work for classes from webreferences (wsdl).

I wonder why...

This is my code:

public void setProperty(object oTargetObject, string strPropname, object
oProperty)

{

System.Type myType = oTargetObject.GetType();

System.Reflection.PropertyInfo[] properties = myType.GetProperties();

foreach(System.Reflection.PropertyInfo pi in properties)

{

if (pi.CanWrite && pi.Name == strPropname)

{

pi.SetValue(oTargetObject, oProperty, null);

}

}

}

Feb 2 '06 #1
2 2472
Mark,

What do you mean it doesn't work? What happens?

As for the web references, I assume you are referring to properties of
the proxy, and not on the web service. The idea of properties on web
services doesn't make sense.

If there is an exception, you might want to show the details. Also, you
might want to try the GetProperty method on the type instead of cycling
through all of the properties. After all, you are looking for just one
property, and there is no reason to loop through all of them.

Also, this might be an issue with the binding flags. Are you sure you
want only public properties on instances?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark" <vl********@hotmail.com> wrote in message
news:eF*************@tk2msftngp13.phx.gbl...
Hi there,

I'm trying to create a function to dynamically set a property from a
class.
The idea is to get a general function that works for every class. Although
this function works for classes i created myself, and for standard
classes,
this function does not seem to work for classes from webreferences (wsdl).

I wonder why...

This is my code:

public void setProperty(object oTargetObject, string strPropname, object
oProperty)

{

System.Type myType = oTargetObject.GetType();

System.Reflection.PropertyInfo[] properties = myType.GetProperties();

foreach(System.Reflection.PropertyInfo pi in properties)

{

if (pi.CanWrite && pi.Name == strPropname)

{

pi.SetValue(oTargetObject, oProperty, null);

}

}

}

Feb 2 '06 #2
If your proxy class was created from V1.1 of WSDL.exe or by adding a web
reference in Visual Studio .Net 2003, then the problem may be that either of
those processes turn public properties on your web service class into public
fields at the proxy class.

To see your web service class properties as properties on the consumer end,
you may want to read my series on passing custom classes to and from a web
service at
http://www.dalepreston.com/Blog/2005...-from-web.html

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Mark" wrote:
Hi there,

I'm trying to create a function to dynamically set a property from a class.
The idea is to get a general function that works for every class. Although
this function works for classes i created myself, and for standard classes,
this function does not seem to work for classes from webreferences (wsdl).

I wonder why...

This is my code:

public void setProperty(object oTargetObject, string strPropname, object
oProperty)

{

System.Type myType = oTargetObject.GetType();

System.Reflection.PropertyInfo[] properties = myType.GetProperties();

foreach(System.Reflection.PropertyInfo pi in properties)

{

if (pi.CanWrite && pi.Name == strPropname)

{

pi.SetValue(oTargetObject, oProperty, null);

}

}

}

Feb 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Eric | last post: by
2 posts views Thread by Duncan Welch | last post: by
reply views Thread by Diane Yocom | last post: by
3 posts views Thread by Guadala Harry | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | 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.