Dynamically set property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    #1

    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(obj ect oTargetObject, string strPropname, object
    oProperty)

    {

    System.Type myType = oTargetObject.G etType();

    System.Reflecti on.PropertyInfo[] properties = myType.GetPrope rties();

    foreach(System. Reflection.Prop ertyInfo pi in properties)

    {

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

    {

    pi.SetValue(oTa rgetObject, oProperty, null);

    }

    }

    }



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Dynamically set property

    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]
    - mvp@spam.guard. caspershouse.co m

    "Mark" <vlad_error@hot mail.com> wrote in message
    news:eF0DnP9JGH A.208@tk2msftng p13.phx.gbl...[color=blue]
    > 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(obj ect oTargetObject, string strPropname, object
    > oProperty)
    >
    > {
    >
    > System.Type myType = oTargetObject.G etType();
    >
    > System.Reflecti on.PropertyInfo[] properties = myType.GetPrope rties();
    >
    > foreach(System. Reflection.Prop ertyInfo pi in properties)
    >
    > {
    >
    > if (pi.CanWrite && pi.Name == strPropname)
    >
    > {
    >
    > pi.SetValue(oTa rgetObject, oProperty, null);
    >
    > }
    >
    > }
    >
    > }
    >
    >
    >[/color]


    Comment

    • Dale

      #3
      RE: Dynamically set property

      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


      HTH
      --
      Dale Preston
      MCAD C#
      MCSE, MCDBA


      "Mark" wrote:
      [color=blue]
      > 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(obj ect oTargetObject, string strPropname, object
      > oProperty)
      >
      > {
      >
      > System.Type myType = oTargetObject.G etType();
      >
      > System.Reflecti on.PropertyInfo[] properties = myType.GetPrope rties();
      >
      > foreach(System. Reflection.Prop ertyInfo pi in properties)
      >
      > {
      >
      > if (pi.CanWrite && pi.Name == strPropname)
      >
      > {
      >
      > pi.SetValue(oTa rgetObject, oProperty, null);
      >
      > }
      >
      > }
      >
      > }
      >
      >
      >
      >[/color]

      Comment

      Working...