473,395 Members | 1,574 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,395 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 2530
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 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)"; ...
4
by: Ahmet | last post by:
Hi all; I have one application in which I read form names from database to be opened. I open form with the code below, and call its show method for form to be shown but before this, I must set...
2
by: Duncan Welch | last post by:
Hi, I've got a fairly simple date control, that I'm creating dynamically in my page. The reason for creating it dynamically is that I want the ID to vary, depending on it's situation. The...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
3
by: Guadala Harry | last post by:
I have this scenario: ASPX Page dynamically loads userControl1 - which in turn dynamically loads userControl2. UserControl2 contains a TextBox Web Server control. When a user clicks a button in...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
3
by: MarkusJ_NZ | last post by:
Hi, I am trying to dynamically populate an objects properties using reflection. Specifically, I have an object called the theObj and it has a number of properties. property is a PropertyInfo...
8
by: BillE | last post by:
When I create a control dynamically and it grows according to the content, the control Height property still returns the original default control height instead of the height after expanding. ...
3
by: coolcoder2007 | last post by:
Hi All, I need to create property in a class dynamically. Property names will come from database and also the total number of properies can be changed according to database value. I have tried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.