Cannot get PropertyInfo SetValue to Work for a Structure

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

    Cannot get PropertyInfo SetValue to Work for a Structure

    I have adapted the following code from the MSDN help for PropertyInfo
    SetValue. In the original code, the structure MyStructure is defined as a
    class MyProperty, and it works as expected. There is also a minor change in
    class Mypropertyinfo, which I have commented out.

    When using a structure, however, the second call to GetValue returns
    "Default caption". Can anyone tell me why, and how I can make this work?

    <code>
    Imports System
    Imports System.Reflecti on

    Public Structure MyStructure

    Private myCaption As String

    Public Property Caption() As String
    Get
    Return myCaption
    End Get
    Set(ByVal Value As String)
    If myCaption <> Value Then
    myCaption = Value
    End If
    End Set
    End Property
    End Structure

    Class Mypropertyinfo

    Public Shared Function Main() As Integer

    Console.WriteLi ne("Reflection. PropertyInfo")

    ''Dim af As New MyProperty ' original
    Dim af As MyStructure

    af.Caption = "Default caption"

    ' Get the type and PropertyInfo
    Dim MyType As Type = af.GetType
    Dim Mypropertyinfo As PropertyInfo = MyType.GetPrope rty("Caption")

    ' Get and display the GetValue method
    Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
    Nothing).ToStri ng())

    ' Use the SetValue method to change the caption
    Mypropertyinfo. SetValue(af, "This caption has been changed.",
    Nothing)

    ' Get the caption again and display it
    Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
    Nothing).ToStri ng())

    Console.ReadLin e()

    Return 0

    End Function
    End Class
    </code>

    TIA

    Charles


  • Imran Koradia

    #2
    Re: Cannot get PropertyInfo SetValue to Work for a Structure

    This is because a structure is a value type and both SetValue and GetValue
    expect Objects causing the arguments to boxed. As a result, the GetValue and
    SetValue are working the boxed object rather than on the actual structure. I
    believe this technique won't work on structures - I could be wrong though.

    hope that helps..
    Imran.

    "Charles Law" <blank@nowhere. com> wrote in message
    news:et0QPzaxEH A.260@TK2MSFTNG P11.phx.gbl...[color=blue]
    > I have adapted the following code from the MSDN help for PropertyInfo
    > SetValue. In the original code, the structure MyStructure is defined as a
    > class MyProperty, and it works as expected. There is also a minor change[/color]
    in[color=blue]
    > class Mypropertyinfo, which I have commented out.
    >
    > When using a structure, however, the second call to GetValue returns
    > "Default caption". Can anyone tell me why, and how I can make this work?
    >
    > <code>
    > Imports System
    > Imports System.Reflecti on
    >
    > Public Structure MyStructure
    >
    > Private myCaption As String
    >
    > Public Property Caption() As String
    > Get
    > Return myCaption
    > End Get
    > Set(ByVal Value As String)
    > If myCaption <> Value Then
    > myCaption = Value
    > End If
    > End Set
    > End Property
    > End Structure
    >
    > Class Mypropertyinfo
    >
    > Public Shared Function Main() As Integer
    >
    > Console.WriteLi ne("Reflection. PropertyInfo")
    >
    > ''Dim af As New MyProperty ' original
    > Dim af As MyStructure
    >
    > af.Caption = "Default caption"
    >
    > ' Get the type and PropertyInfo
    > Dim MyType As Type = af.GetType
    > Dim Mypropertyinfo As PropertyInfo = MyType.GetPrope rty("Caption")
    >
    > ' Get and display the GetValue method
    > Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
    > Nothing).ToStri ng())
    >
    > ' Use the SetValue method to change the caption
    > Mypropertyinfo. SetValue(af, "This caption has been changed.",
    > Nothing)
    >
    > ' Get the caption again and display it
    > Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
    > Nothing).ToStri ng())
    >
    > Console.ReadLin e()
    >
    > Return 0
    >
    > End Function
    > End Class
    > </code>
    >
    > TIA
    >
    > Charles
    >
    >[/color]


    Comment

    • Charles Law

      #3
      Re: Cannot get PropertyInfo SetValue to Work for a Structure

      Hi Imran

      Thanks for a quick response. It does make sense, but sadly, if it is true, I
      am a bit stuck, as I need to be able to use this or a similar technique to
      set property values.

      Do you, or anyone else know of a workaround, or another way of doing it?

      Charles


      "Imran Koradia" <nospam@microso ft.com> wrote in message
      news:OEfqOJbxEH A.2620@TK2MSFTN GP10.phx.gbl...[color=blue]
      > This is because a structure is a value type and both SetValue and GetValue
      > expect Objects causing the arguments to boxed. As a result, the GetValue
      > and
      > SetValue are working the boxed object rather than on the actual structure.
      > I
      > believe this technique won't work on structures - I could be wrong though.
      >
      > hope that helps..
      > Imran.
      >
      > "Charles Law" <blank@nowhere. com> wrote in message
      > news:et0QPzaxEH A.260@TK2MSFTNG P11.phx.gbl...[color=green]
      >> I have adapted the following code from the MSDN help for PropertyInfo
      >> SetValue. In the original code, the structure MyStructure is defined as a
      >> class MyProperty, and it works as expected. There is also a minor change[/color]
      > in[color=green]
      >> class Mypropertyinfo, which I have commented out.
      >>
      >> When using a structure, however, the second call to GetValue returns
      >> "Default caption". Can anyone tell me why, and how I can make this work?
      >>
      >> <code>
      >> Imports System
      >> Imports System.Reflecti on
      >>
      >> Public Structure MyStructure
      >>
      >> Private myCaption As String
      >>
      >> Public Property Caption() As String
      >> Get
      >> Return myCaption
      >> End Get
      >> Set(ByVal Value As String)
      >> If myCaption <> Value Then
      >> myCaption = Value
      >> End If
      >> End Set
      >> End Property
      >> End Structure
      >>
      >> Class Mypropertyinfo
      >>
      >> Public Shared Function Main() As Integer
      >>
      >> Console.WriteLi ne("Reflection. PropertyInfo")
      >>
      >> ''Dim af As New MyProperty ' original
      >> Dim af As MyStructure
      >>
      >> af.Caption = "Default caption"
      >>
      >> ' Get the type and PropertyInfo
      >> Dim MyType As Type = af.GetType
      >> Dim Mypropertyinfo As PropertyInfo =
      >> MyType.GetPrope rty("Caption")
      >>
      >> ' Get and display the GetValue method
      >> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
      >> Nothing).ToStri ng())
      >>
      >> ' Use the SetValue method to change the caption
      >> Mypropertyinfo. SetValue(af, "This caption has been changed.",
      >> Nothing)
      >>
      >> ' Get the caption again and display it
      >> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
      >> Nothing).ToStri ng())
      >>
      >> Console.ReadLin e()
      >>
      >> Return 0
      >>
      >> End Function
      >> End Class
      >> </code>
      >>
      >> TIA
      >>
      >> Charles
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Cannot get PropertyInfo SetValue to Work for a Structure

        Charles,
        The "easiest" way would be not to use a Structure.

        Otherwise I believe you will need to use C#, as VB.NET adds calls to a
        RuntimeHelpers. GetObjectValue whenever you try to do something with an
        object variable.

        The "problem" with using a Structure is that a Structure is a value type,
        hence the call to GetObjectValue which "boxes a value type". GetObjectValue
        is used to ensure that when you assign a value type in an object variable to
        another object variable a copy of the value is made, not a reference to the
        same instance. Which is in keeping with how typed value types work.

        You can use ILDASM.EXE to see when VB.NET adds calls to
        RuntimeHelpers. GetObjectValue.

        I would suggest you try CallByName, however it appears to call
        RuntimeHelpers. GetObjectValue first also...

        Hope this helps
        Jay


        "Charles Law" <blank@nowhere. com> wrote in message
        news:%23AcQOTbx EHA.1076@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > Hi Imran
        >
        > Thanks for a quick response. It does make sense, but sadly, if it is true,
        > I am a bit stuck, as I need to be able to use this or a similar technique
        > to set property values.
        >
        > Do you, or anyone else know of a workaround, or another way of doing it?
        >
        > Charles
        >
        >
        > "Imran Koradia" <nospam@microso ft.com> wrote in message
        > news:OEfqOJbxEH A.2620@TK2MSFTN GP10.phx.gbl...[color=green]
        >> This is because a structure is a value type and both SetValue and
        >> GetValue
        >> expect Objects causing the arguments to boxed. As a result, the GetValue
        >> and
        >> SetValue are working the boxed object rather than on the actual
        >> structure. I
        >> believe this technique won't work on structures - I could be wrong
        >> though.
        >>
        >> hope that helps..
        >> Imran.
        >>
        >> "Charles Law" <blank@nowhere. com> wrote in message
        >> news:et0QPzaxEH A.260@TK2MSFTNG P11.phx.gbl...[color=darkred]
        >>> I have adapted the following code from the MSDN help for PropertyInfo
        >>> SetValue. In the original code, the structure MyStructure is defined as
        >>> a
        >>> class MyProperty, and it works as expected. There is also a minor change[/color]
        >> in[color=darkred]
        >>> class Mypropertyinfo, which I have commented out.
        >>>
        >>> When using a structure, however, the second call to GetValue returns
        >>> "Default caption". Can anyone tell me why, and how I can make this work?
        >>>
        >>> <code>
        >>> Imports System
        >>> Imports System.Reflecti on
        >>>
        >>> Public Structure MyStructure
        >>>
        >>> Private myCaption As String
        >>>
        >>> Public Property Caption() As String
        >>> Get
        >>> Return myCaption
        >>> End Get
        >>> Set(ByVal Value As String)
        >>> If myCaption <> Value Then
        >>> myCaption = Value
        >>> End If
        >>> End Set
        >>> End Property
        >>> End Structure
        >>>
        >>> Class Mypropertyinfo
        >>>
        >>> Public Shared Function Main() As Integer
        >>>
        >>> Console.WriteLi ne("Reflection. PropertyInfo")
        >>>
        >>> ''Dim af As New MyProperty ' original
        >>> Dim af As MyStructure
        >>>
        >>> af.Caption = "Default caption"
        >>>
        >>> ' Get the type and PropertyInfo
        >>> Dim MyType As Type = af.GetType
        >>> Dim Mypropertyinfo As PropertyInfo =
        >>> MyType.GetPrope rty("Caption")
        >>>
        >>> ' Get and display the GetValue method
        >>> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
        >>> Nothing).ToStri ng())
        >>>
        >>> ' Use the SetValue method to change the caption
        >>> Mypropertyinfo. SetValue(af, "This caption has been changed.",
        >>> Nothing)
        >>>
        >>> ' Get the caption again and display it
        >>> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
        >>> Nothing).ToStri ng())
        >>>
        >>> Console.ReadLin e()
        >>>
        >>> Return 0
        >>>
        >>> End Function
        >>> End Class
        >>> </code>
        >>>
        >>> TIA
        >>>
        >>> Charles
        >>>
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Charles Law

          #5
          Re: Cannot get PropertyInfo SetValue to Work for a Structure

          Hi Jay

          I take your point about not using a structure. Unfortunately, the structure
          is in a library already in use by the client, so I don't really have the
          option of changing it to a class. I have just tried CallByName, but as you
          suggest it exhibits the same problem.

          Can you think of any way to circumvent the boxing? I take it that it is the
          first parameter to SetValue that is being boxed. Would there be a way of
          boxing the original structure so that all operations occur on the same
          object?

          Charles


          "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
          news:eCvrmkbxEH A.3476@TK2MSFTN GP15.phx.gbl...[color=blue]
          > Charles,
          > The "easiest" way would be not to use a Structure.
          >
          > Otherwise I believe you will need to use C#, as VB.NET adds calls to a
          > RuntimeHelpers. GetObjectValue whenever you try to do something with an
          > object variable.
          >
          > The "problem" with using a Structure is that a Structure is a value type,
          > hence the call to GetObjectValue which "boxes a value type".
          > GetObjectValue is used to ensure that when you assign a value type in an
          > object variable to another object variable a copy of the value is made,
          > not a reference to the same instance. Which is in keeping with how typed
          > value types work.
          >
          > You can use ILDASM.EXE to see when VB.NET adds calls to
          > RuntimeHelpers. GetObjectValue.
          >
          > I would suggest you try CallByName, however it appears to call
          > RuntimeHelpers. GetObjectValue first also...
          >
          > Hope this helps
          > Jay
          >
          >
          > "Charles Law" <blank@nowhere. com> wrote in message
          > news:%23AcQOTbx EHA.1076@TK2MSF TNGP10.phx.gbl. ..[color=green]
          >> Hi Imran
          >>
          >> Thanks for a quick response. It does make sense, but sadly, if it is
          >> true, I am a bit stuck, as I need to be able to use this or a similar
          >> technique to set property values.
          >>
          >> Do you, or anyone else know of a workaround, or another way of doing it?
          >>
          >> Charles
          >>
          >>
          >> "Imran Koradia" <nospam@microso ft.com> wrote in message
          >> news:OEfqOJbxEH A.2620@TK2MSFTN GP10.phx.gbl...[color=darkred]
          >>> This is because a structure is a value type and both SetValue and
          >>> GetValue
          >>> expect Objects causing the arguments to boxed. As a result, the GetValue
          >>> and
          >>> SetValue are working the boxed object rather than on the actual
          >>> structure. I
          >>> believe this technique won't work on structures - I could be wrong
          >>> though.
          >>>
          >>> hope that helps..
          >>> Imran.
          >>>
          >>> "Charles Law" <blank@nowhere. com> wrote in message
          >>> news:et0QPzaxEH A.260@TK2MSFTNG P11.phx.gbl...
          >>>> I have adapted the following code from the MSDN help for PropertyInfo
          >>>> SetValue. In the original code, the structure MyStructure is defined as
          >>>> a
          >>>> class MyProperty, and it works as expected. There is also a minor
          >>>> change
          >>> in
          >>>> class Mypropertyinfo, which I have commented out.
          >>>>
          >>>> When using a structure, however, the second call to GetValue returns
          >>>> "Default caption". Can anyone tell me why, and how I can make this
          >>>> work?
          >>>>
          >>>> <code>
          >>>> Imports System
          >>>> Imports System.Reflecti on
          >>>>
          >>>> Public Structure MyStructure
          >>>>
          >>>> Private myCaption As String
          >>>>
          >>>> Public Property Caption() As String
          >>>> Get
          >>>> Return myCaption
          >>>> End Get
          >>>> Set(ByVal Value As String)
          >>>> If myCaption <> Value Then
          >>>> myCaption = Value
          >>>> End If
          >>>> End Set
          >>>> End Property
          >>>> End Structure
          >>>>
          >>>> Class Mypropertyinfo
          >>>>
          >>>> Public Shared Function Main() As Integer
          >>>>
          >>>> Console.WriteLi ne("Reflection. PropertyInfo")
          >>>>
          >>>> ''Dim af As New MyProperty ' original
          >>>> Dim af As MyStructure
          >>>>
          >>>> af.Caption = "Default caption"
          >>>>
          >>>> ' Get the type and PropertyInfo
          >>>> Dim MyType As Type = af.GetType
          >>>> Dim Mypropertyinfo As PropertyInfo =
          >>>> MyType.GetPrope rty("Caption")
          >>>>
          >>>> ' Get and display the GetValue method
          >>>> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
          >>>> Nothing).ToStri ng())
          >>>>
          >>>> ' Use the SetValue method to change the caption
          >>>> Mypropertyinfo. SetValue(af, "This caption has been changed.",
          >>>> Nothing)
          >>>>
          >>>> ' Get the caption again and display it
          >>>> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
          >>>> Nothing).ToStri ng())
          >>>>
          >>>> Console.ReadLin e()
          >>>>
          >>>> Return 0
          >>>>
          >>>> End Function
          >>>> End Class
          >>>> </code>
          >>>>
          >>>> TIA
          >>>>
          >>>> Charles
          >>>>
          >>>>
          >>>
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Imran Koradia

            #6
            Re: Cannot get PropertyInfo SetValue to Work for a Structure

            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
            news:eCvrmkbxEH A.3476@TK2MSFTN GP15.phx.gbl...[color=blue]
            > Charles,
            > The "easiest" way would be not to use a Structure.
            >
            > Otherwise I believe you will need to use C#, as VB.NET adds calls to a
            > RuntimeHelpers. GetObjectValue whenever you try to do something with an
            > object variable.
            >
            > The "problem" with using a Structure is that a Structure is a value type,
            > hence the call to GetObjectValue which "boxes a value type".[/color]
            GetObjectValue[color=blue]
            > is used to ensure that when you assign a value type in an object variable[/color]
            to[color=blue]
            > another object variable a copy of the value is made, not a reference to[/color]
            the[color=blue]
            > same instance. Which is in keeping with how typed value types work.
            >
            > You can use ILDASM.EXE to see when VB.NET adds calls to
            > RuntimeHelpers. GetObjectValue.
            >
            > I would suggest you try CallByName, however it appears to call
            > RuntimeHelpers. GetObjectValue first also...[/color]

            Jay,

            That's true. Even CallByName does not work for structures. I would be
            interested to know if there was indeed a way to Get/Set property values for
            structures via Reflection or any other technique (apart from the straight
            forward way).

            Imran.


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Cannot get PropertyInfo SetValue to Work for a Structure

              Charles,
              The only way to circumvent the boxing is to use a Reference type.

              I believe the only way to circumvent the GetObjectValue is to use C# (I have
              not looked at the IL produced by C# enough to say for certain, not at a
              point where I can check right now).

              As I tried to state: if you have an Object variable VB.NET will call
              GetObjectValue when ever it needs the "value" of that variable. Which means
              GetObjectValue gets called on assignments & for parameters.

              First I would change the try to change the structure to a Class. If that was
              not possible, I would define a small C# function that accepted the structure
              as a parameter, modified the fields, then returned the modified structure
              back to VB. You could use your existing Reflection code to do this. (a Value
              Type CallByName replacement if you will).

              Hope this helps
              Jay

              "Charles Law" <blank@nowhere. com> wrote in message
              news:ee1If0bxEH A.3528@TK2MSFTN GP10.phx.gbl...[color=blue]
              > Hi Jay
              >
              > I take your point about not using a structure. Unfortunately, the
              > structure is in a library already in use by the client, so I don't really
              > have the option of changing it to a class. I have just tried CallByName,
              > but as you suggest it exhibits the same problem.
              >
              > Can you think of any way to circumvent the boxing? I take it that it is
              > the first parameter to SetValue that is being boxed. Would there be a way
              > of boxing the original structure so that all operations occur on the same
              > object?
              >
              > Charles
              >
              >
              > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
              > news:eCvrmkbxEH A.3476@TK2MSFTN GP15.phx.gbl...[color=green]
              >> Charles,
              >> The "easiest" way would be not to use a Structure.
              >>
              >> Otherwise I believe you will need to use C#, as VB.NET adds calls to a
              >> RuntimeHelpers. GetObjectValue whenever you try to do something with an
              >> object variable.
              >>
              >> The "problem" with using a Structure is that a Structure is a value type,
              >> hence the call to GetObjectValue which "boxes a value type".
              >> GetObjectValue is used to ensure that when you assign a value type in an
              >> object variable to another object variable a copy of the value is made,
              >> not a reference to the same instance. Which is in keeping with how typed
              >> value types work.
              >>
              >> You can use ILDASM.EXE to see when VB.NET adds calls to
              >> RuntimeHelpers. GetObjectValue.
              >>
              >> I would suggest you try CallByName, however it appears to call
              >> RuntimeHelpers. GetObjectValue first also...
              >>
              >> Hope this helps
              >> Jay
              >>
              >>
              >> "Charles Law" <blank@nowhere. com> wrote in message
              >> news:%23AcQOTbx EHA.1076@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
              >>> Hi Imran
              >>>
              >>> Thanks for a quick response. It does make sense, but sadly, if it is
              >>> true, I am a bit stuck, as I need to be able to use this or a similar
              >>> technique to set property values.
              >>>
              >>> Do you, or anyone else know of a workaround, or another way of doing it?
              >>>
              >>> Charles
              >>>
              >>>
              >>> "Imran Koradia" <nospam@microso ft.com> wrote in message
              >>> news:OEfqOJbxEH A.2620@TK2MSFTN GP10.phx.gbl...
              >>>> This is because a structure is a value type and both SetValue and
              >>>> GetValue
              >>>> expect Objects causing the arguments to boxed. As a result, the
              >>>> GetValue and
              >>>> SetValue are working the boxed object rather than on the actual
              >>>> structure. I
              >>>> believe this technique won't work on structures - I could be wrong
              >>>> though.
              >>>>
              >>>> hope that helps..
              >>>> Imran.
              >>>>
              >>>> "Charles Law" <blank@nowhere. com> wrote in message
              >>>> news:et0QPzaxEH A.260@TK2MSFTNG P11.phx.gbl...
              >>>>> I have adapted the following code from the MSDN help for PropertyInfo
              >>>>> SetValue. In the original code, the structure MyStructure is defined
              >>>>> as a
              >>>>> class MyProperty, and it works as expected. There is also a minor
              >>>>> change
              >>>> in
              >>>>> class Mypropertyinfo, which I have commented out.
              >>>>>
              >>>>> When using a structure, however, the second call to GetValue returns
              >>>>> "Default caption". Can anyone tell me why, and how I can make this
              >>>>> work?
              >>>>>
              >>>>> <code>
              >>>>> Imports System
              >>>>> Imports System.Reflecti on
              >>>>>
              >>>>> Public Structure MyStructure
              >>>>>
              >>>>> Private myCaption As String
              >>>>>
              >>>>> Public Property Caption() As String
              >>>>> Get
              >>>>> Return myCaption
              >>>>> End Get
              >>>>> Set(ByVal Value As String)
              >>>>> If myCaption <> Value Then
              >>>>> myCaption = Value
              >>>>> End If
              >>>>> End Set
              >>>>> End Property
              >>>>> End Structure
              >>>>>
              >>>>> Class Mypropertyinfo
              >>>>>
              >>>>> Public Shared Function Main() As Integer
              >>>>>
              >>>>> Console.WriteLi ne("Reflection. PropertyInfo")
              >>>>>
              >>>>> ''Dim af As New MyProperty ' original
              >>>>> Dim af As MyStructure
              >>>>>
              >>>>> af.Caption = "Default caption"
              >>>>>
              >>>>> ' Get the type and PropertyInfo
              >>>>> Dim MyType As Type = af.GetType
              >>>>> Dim Mypropertyinfo As PropertyInfo =
              >>>>> MyType.GetPrope rty("Caption")
              >>>>>
              >>>>> ' Get and display the GetValue method
              >>>>> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
              >>>>> Nothing).ToStri ng())
              >>>>>
              >>>>> ' Use the SetValue method to change the caption
              >>>>> Mypropertyinfo. SetValue(af, "This caption has been changed.",
              >>>>> Nothing)
              >>>>>
              >>>>> ' Get the caption again and display it
              >>>>> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
              >>>>> Nothing).ToStri ng())
              >>>>>
              >>>>> Console.ReadLin e()
              >>>>>
              >>>>> Return 0
              >>>>>
              >>>>> End Function
              >>>>> End Class
              >>>>> </code>
              >>>>>
              >>>>> TIA
              >>>>>
              >>>>> Charles
              >>>>>
              >>>>>
              >>>>
              >>>>
              >>>
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • Charles Law

                #8
                Re: Cannot get PropertyInfo SetValue to Work for a Structure

                Jay

                I don't use C# - would it be too much to ask for a skeleton that I could
                use?

                Charles


                "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                news:OJeKaDcxEH A.2316@TK2MSFTN GP15.phx.gbl...[color=blue]
                > Charles,
                > The only way to circumvent the boxing is to use a Reference type.
                >
                > I believe the only way to circumvent the GetObjectValue is to use C# (I
                > have not looked at the IL produced by C# enough to say for certain, not at
                > a point where I can check right now).
                >
                > As I tried to state: if you have an Object variable VB.NET will call
                > GetObjectValue when ever it needs the "value" of that variable. Which
                > means GetObjectValue gets called on assignments & for parameters.
                >
                > First I would change the try to change the structure to a Class. If that
                > was not possible, I would define a small C# function that accepted the
                > structure as a parameter, modified the fields, then returned the modified
                > structure back to VB. You could use your existing Reflection code to do
                > this. (a Value Type CallByName replacement if you will).
                >
                > Hope this helps
                > Jay
                >
                > "Charles Law" <blank@nowhere. com> wrote in message
                > news:ee1If0bxEH A.3528@TK2MSFTN GP10.phx.gbl...[color=green]
                >> Hi Jay
                >>
                >> I take your point about not using a structure. Unfortunately, the
                >> structure is in a library already in use by the client, so I don't really
                >> have the option of changing it to a class. I have just tried CallByName,
                >> but as you suggest it exhibits the same problem.
                >>
                >> Can you think of any way to circumvent the boxing? I take it that it is
                >> the first parameter to SetValue that is being boxed. Would there be a way
                >> of boxing the original structure so that all operations occur on the same
                >> object?
                >>
                >> Charles
                >>
                >>
                >> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                >> news:eCvrmkbxEH A.3476@TK2MSFTN GP15.phx.gbl...[color=darkred]
                >>> Charles,
                >>> The "easiest" way would be not to use a Structure.
                >>>
                >>> Otherwise I believe you will need to use C#, as VB.NET adds calls to a
                >>> RuntimeHelpers. GetObjectValue whenever you try to do something with an
                >>> object variable.
                >>>
                >>> The "problem" with using a Structure is that a Structure is a value
                >>> type, hence the call to GetObjectValue which "boxes a value type".
                >>> GetObjectValue is used to ensure that when you assign a value type in an
                >>> object variable to another object variable a copy of the value is made,
                >>> not a reference to the same instance. Which is in keeping with how typed
                >>> value types work.
                >>>
                >>> You can use ILDASM.EXE to see when VB.NET adds calls to
                >>> RuntimeHelpers. GetObjectValue.
                >>>
                >>> I would suggest you try CallByName, however it appears to call
                >>> RuntimeHelpers. GetObjectValue first also...
                >>>
                >>> Hope this helps
                >>> Jay
                >>>
                >>>
                >>> "Charles Law" <blank@nowhere. com> wrote in message
                >>> news:%23AcQOTbx EHA.1076@TK2MSF TNGP10.phx.gbl. ..
                >>>> Hi Imran
                >>>>
                >>>> Thanks for a quick response. It does make sense, but sadly, if it is
                >>>> true, I am a bit stuck, as I need to be able to use this or a similar
                >>>> technique to set property values.
                >>>>
                >>>> Do you, or anyone else know of a workaround, or another way of doing
                >>>> it?
                >>>>
                >>>> Charles
                >>>>
                >>>>
                >>>> "Imran Koradia" <nospam@microso ft.com> wrote in message
                >>>> news:OEfqOJbxEH A.2620@TK2MSFTN GP10.phx.gbl...
                >>>>> This is because a structure is a value type and both SetValue and
                >>>>> GetValue
                >>>>> expect Objects causing the arguments to boxed. As a result, the
                >>>>> GetValue and
                >>>>> SetValue are working the boxed object rather than on the actual
                >>>>> structure. I
                >>>>> believe this technique won't work on structures - I could be wrong
                >>>>> though.
                >>>>>
                >>>>> hope that helps..
                >>>>> Imran.
                >>>>>
                >>>>> "Charles Law" <blank@nowhere. com> wrote in message
                >>>>> news:et0QPzaxEH A.260@TK2MSFTNG P11.phx.gbl...
                >>>>>> I have adapted the following code from the MSDN help for PropertyInfo
                >>>>>> SetValue. In the original code, the structure MyStructure is defined
                >>>>>> as a
                >>>>>> class MyProperty, and it works as expected. There is also a minor
                >>>>>> change
                >>>>> in
                >>>>>> class Mypropertyinfo, which I have commented out.
                >>>>>>
                >>>>>> When using a structure, however, the second call to GetValue returns
                >>>>>> "Default caption". Can anyone tell me why, and how I can make this
                >>>>>> work?
                >>>>>>
                >>>>>> <code>
                >>>>>> Imports System
                >>>>>> Imports System.Reflecti on
                >>>>>>
                >>>>>> Public Structure MyStructure
                >>>>>>
                >>>>>> Private myCaption As String
                >>>>>>
                >>>>>> Public Property Caption() As String
                >>>>>> Get
                >>>>>> Return myCaption
                >>>>>> End Get
                >>>>>> Set(ByVal Value As String)
                >>>>>> If myCaption <> Value Then
                >>>>>> myCaption = Value
                >>>>>> End If
                >>>>>> End Set
                >>>>>> End Property
                >>>>>> End Structure
                >>>>>>
                >>>>>> Class Mypropertyinfo
                >>>>>>
                >>>>>> Public Shared Function Main() As Integer
                >>>>>>
                >>>>>> Console.WriteLi ne("Reflection. PropertyInfo")
                >>>>>>
                >>>>>> ''Dim af As New MyProperty ' original
                >>>>>> Dim af As MyStructure
                >>>>>>
                >>>>>> af.Caption = "Default caption"
                >>>>>>
                >>>>>> ' Get the type and PropertyInfo
                >>>>>> Dim MyType As Type = af.GetType
                >>>>>> Dim Mypropertyinfo As PropertyInfo =
                >>>>>> MyType.GetPrope rty("Caption")
                >>>>>>
                >>>>>> ' Get and display the GetValue method
                >>>>>> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
                >>>>>> Nothing).ToStri ng())
                >>>>>>
                >>>>>> ' Use the SetValue method to change the caption
                >>>>>> Mypropertyinfo. SetValue(af, "This caption has been changed.",
                >>>>>> Nothing)
                >>>>>>
                >>>>>> ' Get the caption again and display it
                >>>>>> Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(af,
                >>>>>> Nothing).ToStri ng())
                >>>>>>
                >>>>>> Console.ReadLin e()
                >>>>>>
                >>>>>> Return 0
                >>>>>>
                >>>>>> End Function
                >>>>>> End Class
                >>>>>> </code>
                >>>>>>
                >>>>>> TIA
                >>>>>>
                >>>>>> Charles
                >>>>>>
                >>>>>>
                >>>>>
                >>>>>
                >>>>
                >>>>
                >>>
                >>>[/color]
                >>
                >>[/color]
                >
                >[/color]


                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: Cannot get PropertyInfo SetValue to Work for a Structure

                  Charles,
                  You can basically use the VB.NET code you started with:

                  Something like:

                  In a C# class library:

                  ' don't want other inheriting from this class
                  public sealed class PropertyHelper
                  {

                  ' Don't want others instantiating this class
                  private PropertyHelper( )
                  {
                  }

                  public static object SetValue(object o, string name, object value)
                  {
                  System.Reflecti on.PropertyInfo info =
                  o.GetType().Get Property(name);
                  info.SetValue(o , value, null);
                  return o;
                  }

                  public static object SetValue(object o, string name, object value,
                  object[] index)
                  {
                  System.Reflecti on.PropertyInfo info =
                  o.GetType().Get Property(name);
                  info.SetValue(o , value, index);
                  return o;
                  }

                  }

                  Then in VB.NET when you want to use it you can:

                  Imports ClassLibrary1 ' whatever you called the above class library.
                  Dim af As MyStructure

                  af = DirectCast(Prop ertyHelper.SetV alue(af, "Caption", "This caption has
                  been changed."), MyStructure)

                  Hope this helps
                  Jay



                  "Charles Law" <blank@nowhere. com> wrote in message
                  news:upntBMcxEH A.4028@TK2MSFTN GP15.phx.gbl...[color=blue]
                  > Jay
                  >
                  > I don't use C# - would it be too much to ask for a skeleton that I could
                  > use?
                  >
                  > Charles
                  >
                  >[/color]
                  <<snip>>


                  Comment

                  • Charles Law

                    #10
                    Re: Cannot get PropertyInfo SetValue to Work for a Structure

                    Jay, I love you, and want to have your babies. It does exactly what I want.

                    Thanks.

                    Charles


                    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                    news:%23KGZTTmx EHA.3024@TK2MSF TNGP14.phx.gbl. ..[color=blue]
                    > Charles,
                    > You can basically use the VB.NET code you started with:
                    >
                    > Something like:
                    >
                    > In a C# class library:
                    >
                    > ' don't want other inheriting from this class
                    > public sealed class PropertyHelper
                    > {
                    >
                    > ' Don't want others instantiating this class
                    > private PropertyHelper( )
                    > {
                    > }
                    >
                    > public static object SetValue(object o, string name, object value)
                    > {
                    > System.Reflecti on.PropertyInfo info =
                    > o.GetType().Get Property(name);
                    > info.SetValue(o , value, null);
                    > return o;
                    > }
                    >
                    > public static object SetValue(object o, string name, object value,
                    > object[] index)
                    > {
                    > System.Reflecti on.PropertyInfo info =
                    > o.GetType().Get Property(name);
                    > info.SetValue(o , value, index);
                    > return o;
                    > }
                    >
                    > }
                    >
                    > Then in VB.NET when you want to use it you can:
                    >
                    > Imports ClassLibrary1 ' whatever you called the above class library.
                    > Dim af As MyStructure
                    >
                    > af = DirectCast(Prop ertyHelper.SetV alue(af, "Caption", "This caption
                    > has been changed."), MyStructure)
                    >
                    > Hope this helps
                    > Jay
                    >
                    >
                    >
                    > "Charles Law" <blank@nowhere. com> wrote in message
                    > news:upntBMcxEH A.4028@TK2MSFTN GP15.phx.gbl...[color=green]
                    >> Jay
                    >>
                    >> I don't use C# - would it be too much to ask for a skeleton that I could
                    >> use?
                    >>
                    >> Charles
                    >>
                    >>[/color]
                    > <<snip>>
                    >[/color]


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: Cannot get PropertyInfo SetValue to Work for a Structure

                      Charles,
                      This just in!

                      If you assign your structure to a System.ValueTyp e variable instead of a
                      System.Object variable you can call the methods via reflection.

                      For example (from your original post):

                      Public Shared Function Main() As Integer

                      Console.WriteLi ne("Reflection. PropertyInfo")

                      ''Dim af As New MyProperty ' original
                      Dim af As MyStructure

                      af.Caption = "Default caption"

                      Dim vaf As ValueType = af

                      ' Get the type and PropertyInfo
                      Dim MyType As Type = vaf.GetType
                      Dim Mypropertyinfo As PropertyInfo = MyType.GetPrope rty("Caption")

                      ' Get and display the GetValue method
                      Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                      Nothing).ToStri ng())

                      ' Use the SetValue method to change the caption
                      Mypropertyinfo. SetValue(vaf, "This caption has been changed.",
                      Nothing)

                      ' Get the caption again and display it
                      Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                      Nothing).ToStri ng())

                      Console.ReadLin e()

                      Return 0

                      End Function

                      Notice that I define a vaf variable that has type System.ValueTyp e, that
                      this new variable is used when calling Reflection.

                      This tip courtesy of Mattias Sjögren in the thread "Difference between
                      boxing a primitive type and boxing a custom structure" in the
                      microsoft.publi c.dotnet.framew ork newsgroup.

                      Hope this helps
                      Jay


                      "Charles Law" <blank@nowhere. com> wrote in message
                      news:%23t96Z%23 mxEHA.3108@TK2M SFTNGP14.phx.gb l...[color=blue]
                      > Jay, I love you, and want to have your babies. It does exactly what I
                      > want.
                      >
                      > Thanks.
                      >
                      > Charles
                      >
                      >
                      > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                      > news:%23KGZTTmx EHA.3024@TK2MSF TNGP14.phx.gbl. ..[color=green]
                      >> Charles,
                      >> You can basically use the VB.NET code you started with:
                      >>
                      >> Something like:
                      >>
                      >> In a C# class library:
                      >>
                      >> ' don't want other inheriting from this class
                      >> public sealed class PropertyHelper
                      >> {
                      >>
                      >> ' Don't want others instantiating this class
                      >> private PropertyHelper( )
                      >> {
                      >> }
                      >>
                      >> public static object SetValue(object o, string name, object value)
                      >> {
                      >> System.Reflecti on.PropertyInfo info =
                      >> o.GetType().Get Property(name);
                      >> info.SetValue(o , value, null);
                      >> return o;
                      >> }
                      >>
                      >> public static object SetValue(object o, string name, object value,
                      >> object[] index)
                      >> {
                      >> System.Reflecti on.PropertyInfo info =
                      >> o.GetType().Get Property(name);
                      >> info.SetValue(o , value, index);
                      >> return o;
                      >> }
                      >>
                      >> }
                      >>
                      >> Then in VB.NET when you want to use it you can:
                      >>
                      >> Imports ClassLibrary1 ' whatever you called the above class library.
                      >> Dim af As MyStructure
                      >>
                      >> af = DirectCast(Prop ertyHelper.SetV alue(af, "Caption", "This caption
                      >> has been changed."), MyStructure)
                      >>
                      >> Hope this helps
                      >> Jay
                      >>
                      >>
                      >>
                      >> "Charles Law" <blank@nowhere. com> wrote in message
                      >> news:upntBMcxEH A.4028@TK2MSFTN GP15.phx.gbl...[color=darkred]
                      >>> Jay
                      >>>
                      >>> I don't use C# - would it be too much to ask for a skeleton that I could
                      >>> use?
                      >>>
                      >>> Charles
                      >>>
                      >>>[/color]
                      >> <<snip>>
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: Cannot get PropertyInfo SetValue to Work for a Structure

                        Charles,
                        This just in!

                        If you assign your structure to a System.ValueTyp e variable instead of a
                        System.Object variable you can call the methods via reflection.

                        For example (from your original post):

                        Public Shared Function Main() As Integer

                        Console.WriteLi ne("Reflection. PropertyInfo")

                        ''Dim af As New MyProperty ' original
                        Dim af As MyStructure

                        af.Caption = "Default caption"

                        Dim vaf As ValueType = af

                        ' Get the type and PropertyInfo
                        Dim MyType As Type = vaf.GetType
                        Dim Mypropertyinfo As PropertyInfo = MyType.GetPrope rty("Caption")

                        ' Get and display the GetValue method
                        Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                        Nothing).ToStri ng())

                        ' Use the SetValue method to change the caption
                        Mypropertyinfo. SetValue(vaf, "This caption has been changed.",
                        Nothing)

                        ' Get the caption again and display it
                        Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                        Nothing).ToStri ng())

                        Console.ReadLin e()

                        Return 0

                        End Function

                        Notice that I define a vaf variable that has type System.ValueTyp e, that
                        this new variable is used when calling Reflection.

                        This tip courtesy of Mattias Sjögren in the thread "Difference between
                        boxing a primitive type and boxing a custom structure" in the
                        microsoft.publi c.dotnet.framew ork newsgroup.

                        Hope this helps
                        Jay


                        "Charles Law" <blank@nowhere. com> wrote in message
                        news:%23t96Z%23 mxEHA.3108@TK2M SFTNGP14.phx.gb l...[color=blue]
                        > Jay, I love you, and want to have your babies. It does exactly what I
                        > want.
                        >
                        > Thanks.
                        >
                        > Charles
                        >
                        >
                        > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                        > news:%23KGZTTmx EHA.3024@TK2MSF TNGP14.phx.gbl. ..[color=green]
                        >> Charles,
                        >> You can basically use the VB.NET code you started with:
                        >>
                        >> Something like:
                        >>
                        >> In a C# class library:
                        >>
                        >> ' don't want other inheriting from this class
                        >> public sealed class PropertyHelper
                        >> {
                        >>
                        >> ' Don't want others instantiating this class
                        >> private PropertyHelper( )
                        >> {
                        >> }
                        >>
                        >> public static object SetValue(object o, string name, object value)
                        >> {
                        >> System.Reflecti on.PropertyInfo info =
                        >> o.GetType().Get Property(name);
                        >> info.SetValue(o , value, null);
                        >> return o;
                        >> }
                        >>
                        >> public static object SetValue(object o, string name, object value,
                        >> object[] index)
                        >> {
                        >> System.Reflecti on.PropertyInfo info =
                        >> o.GetType().Get Property(name);
                        >> info.SetValue(o , value, index);
                        >> return o;
                        >> }
                        >>
                        >> }
                        >>
                        >> Then in VB.NET when you want to use it you can:
                        >>
                        >> Imports ClassLibrary1 ' whatever you called the above class library.
                        >> Dim af As MyStructure
                        >>
                        >> af = DirectCast(Prop ertyHelper.SetV alue(af, "Caption", "This caption
                        >> has been changed."), MyStructure)
                        >>
                        >> Hope this helps
                        >> Jay
                        >>
                        >>
                        >>
                        >> "Charles Law" <blank@nowhere. com> wrote in message
                        >> news:upntBMcxEH A.4028@TK2MSFTN GP15.phx.gbl...[color=darkred]
                        >>> Jay
                        >>>
                        >>> I don't use C# - would it be too much to ask for a skeleton that I could
                        >>> use?
                        >>>
                        >>> Charles
                        >>>
                        >>>[/color]
                        >> <<snip>>
                        >>[/color]
                        >
                        >[/color]


                        Comment

                        • Charles Law

                          #13
                          Re: Cannot get PropertyInfo SetValue to Work for a Structure

                          Top tip! Thanks Jay (and Mattias)

                          And casting vaf back to af after calling SetValue gives me the updated
                          structure in af as a MyStructure again.

                          Cheers.

                          Charles


                          "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                          news:%23uatZnTy EHA.3676@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                          > Charles,
                          > This just in!
                          >
                          > If you assign your structure to a System.ValueTyp e variable instead of a
                          > System.Object variable you can call the methods via reflection.
                          >
                          > For example (from your original post):
                          >
                          > Public Shared Function Main() As Integer
                          >
                          > Console.WriteLi ne("Reflection. PropertyInfo")
                          >
                          > ''Dim af As New MyProperty ' original
                          > Dim af As MyStructure
                          >
                          > af.Caption = "Default caption"
                          >
                          > Dim vaf As ValueType = af
                          >
                          > ' Get the type and PropertyInfo
                          > Dim MyType As Type = vaf.GetType
                          > Dim Mypropertyinfo As PropertyInfo = MyType.GetPrope rty("Caption")
                          >
                          > ' Get and display the GetValue method
                          > Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                          > Nothing).ToStri ng())
                          >
                          > ' Use the SetValue method to change the caption
                          > Mypropertyinfo. SetValue(vaf, "This caption has been changed.",
                          > Nothing)
                          >
                          > ' Get the caption again and display it
                          > Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                          > Nothing).ToStri ng())
                          >
                          > Console.ReadLin e()
                          >
                          > Return 0
                          >
                          > End Function
                          >
                          > Notice that I define a vaf variable that has type System.ValueTyp e, that
                          > this new variable is used when calling Reflection.
                          >
                          > This tip courtesy of Mattias Sjögren in the thread "Difference between
                          > boxing a primitive type and boxing a custom structure" in the
                          > microsoft.publi c.dotnet.framew ork newsgroup.
                          >
                          > Hope this helps
                          > Jay
                          >
                          >
                          > "Charles Law" <blank@nowhere. com> wrote in message
                          > news:%23t96Z%23 mxEHA.3108@TK2M SFTNGP14.phx.gb l...[color=green]
                          >> Jay, I love you, and want to have your babies. It does exactly what I
                          >> want.
                          >>
                          >> Thanks.
                          >>
                          >> Charles
                          >>
                          >>
                          >> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                          >> news:%23KGZTTmx EHA.3024@TK2MSF TNGP14.phx.gbl. ..[color=darkred]
                          >>> Charles,
                          >>> You can basically use the VB.NET code you started with:
                          >>>
                          >>> Something like:
                          >>>
                          >>> In a C# class library:
                          >>>
                          >>> ' don't want other inheriting from this class
                          >>> public sealed class PropertyHelper
                          >>> {
                          >>>
                          >>> ' Don't want others instantiating this class
                          >>> private PropertyHelper( )
                          >>> {
                          >>> }
                          >>>
                          >>> public static object SetValue(object o, string name, object
                          >>> value)
                          >>> {
                          >>> System.Reflecti on.PropertyInfo info =
                          >>> o.GetType().Get Property(name);
                          >>> info.SetValue(o , value, null);
                          >>> return o;
                          >>> }
                          >>>
                          >>> public static object SetValue(object o, string name, object
                          >>> value, object[] index)
                          >>> {
                          >>> System.Reflecti on.PropertyInfo info =
                          >>> o.GetType().Get Property(name);
                          >>> info.SetValue(o , value, index);
                          >>> return o;
                          >>> }
                          >>>
                          >>> }
                          >>>
                          >>> Then in VB.NET when you want to use it you can:
                          >>>
                          >>> Imports ClassLibrary1 ' whatever you called the above class library.
                          >>> Dim af As MyStructure
                          >>>
                          >>> af = DirectCast(Prop ertyHelper.SetV alue(af, "Caption", "This caption
                          >>> has been changed."), MyStructure)
                          >>>
                          >>> Hope this helps
                          >>> Jay
                          >>>
                          >>>
                          >>>
                          >>> "Charles Law" <blank@nowhere. com> wrote in message
                          >>> news:upntBMcxEH A.4028@TK2MSFTN GP15.phx.gbl...
                          >>>> Jay
                          >>>>
                          >>>> I don't use C# - would it be too much to ask for a skeleton that I
                          >>>> could use?
                          >>>>
                          >>>> Charles
                          >>>>
                          >>>>
                          >>> <<snip>>
                          >>>[/color]
                          >>
                          >>[/color]
                          >
                          >[/color]


                          Comment

                          • Charles Law

                            #14
                            Re: Cannot get PropertyInfo SetValue to Work for a Structure

                            Top tip! Thanks Jay (and Mattias)

                            And casting vaf back to af after calling SetValue gives me the updated
                            structure in af as a MyStructure again.

                            Cheers.

                            Charles


                            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                            news:%23uatZnTy EHA.3676@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                            > Charles,
                            > This just in!
                            >
                            > If you assign your structure to a System.ValueTyp e variable instead of a
                            > System.Object variable you can call the methods via reflection.
                            >
                            > For example (from your original post):
                            >
                            > Public Shared Function Main() As Integer
                            >
                            > Console.WriteLi ne("Reflection. PropertyInfo")
                            >
                            > ''Dim af As New MyProperty ' original
                            > Dim af As MyStructure
                            >
                            > af.Caption = "Default caption"
                            >
                            > Dim vaf As ValueType = af
                            >
                            > ' Get the type and PropertyInfo
                            > Dim MyType As Type = vaf.GetType
                            > Dim Mypropertyinfo As PropertyInfo = MyType.GetPrope rty("Caption")
                            >
                            > ' Get and display the GetValue method
                            > Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                            > Nothing).ToStri ng())
                            >
                            > ' Use the SetValue method to change the caption
                            > Mypropertyinfo. SetValue(vaf, "This caption has been changed.",
                            > Nothing)
                            >
                            > ' Get the caption again and display it
                            > Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                            > Nothing).ToStri ng())
                            >
                            > Console.ReadLin e()
                            >
                            > Return 0
                            >
                            > End Function
                            >
                            > Notice that I define a vaf variable that has type System.ValueTyp e, that
                            > this new variable is used when calling Reflection.
                            >
                            > This tip courtesy of Mattias Sjögren in the thread "Difference between
                            > boxing a primitive type and boxing a custom structure" in the
                            > microsoft.publi c.dotnet.framew ork newsgroup.
                            >
                            > Hope this helps
                            > Jay
                            >
                            >
                            > "Charles Law" <blank@nowhere. com> wrote in message
                            > news:%23t96Z%23 mxEHA.3108@TK2M SFTNGP14.phx.gb l...[color=green]
                            >> Jay, I love you, and want to have your babies. It does exactly what I
                            >> want.
                            >>
                            >> Thanks.
                            >>
                            >> Charles
                            >>
                            >>
                            >> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                            >> news:%23KGZTTmx EHA.3024@TK2MSF TNGP14.phx.gbl. ..[color=darkred]
                            >>> Charles,
                            >>> You can basically use the VB.NET code you started with:
                            >>>
                            >>> Something like:
                            >>>
                            >>> In a C# class library:
                            >>>
                            >>> ' don't want other inheriting from this class
                            >>> public sealed class PropertyHelper
                            >>> {
                            >>>
                            >>> ' Don't want others instantiating this class
                            >>> private PropertyHelper( )
                            >>> {
                            >>> }
                            >>>
                            >>> public static object SetValue(object o, string name, object
                            >>> value)
                            >>> {
                            >>> System.Reflecti on.PropertyInfo info =
                            >>> o.GetType().Get Property(name);
                            >>> info.SetValue(o , value, null);
                            >>> return o;
                            >>> }
                            >>>
                            >>> public static object SetValue(object o, string name, object
                            >>> value, object[] index)
                            >>> {
                            >>> System.Reflecti on.PropertyInfo info =
                            >>> o.GetType().Get Property(name);
                            >>> info.SetValue(o , value, index);
                            >>> return o;
                            >>> }
                            >>>
                            >>> }
                            >>>
                            >>> Then in VB.NET when you want to use it you can:
                            >>>
                            >>> Imports ClassLibrary1 ' whatever you called the above class library.
                            >>> Dim af As MyStructure
                            >>>
                            >>> af = DirectCast(Prop ertyHelper.SetV alue(af, "Caption", "This caption
                            >>> has been changed."), MyStructure)
                            >>>
                            >>> Hope this helps
                            >>> Jay
                            >>>
                            >>>
                            >>>
                            >>> "Charles Law" <blank@nowhere. com> wrote in message
                            >>> news:upntBMcxEH A.4028@TK2MSFTN GP15.phx.gbl...
                            >>>> Jay
                            >>>>
                            >>>> I don't use C# - would it be too much to ask for a skeleton that I
                            >>>> could use?
                            >>>>
                            >>>> Charles
                            >>>>
                            >>>>
                            >>> <<snip>>
                            >>>[/color]
                            >>
                            >>[/color]
                            >
                            >[/color]


                            Comment

                            • Imran Koradia

                              #15
                              Re: Cannot get PropertyInfo SetValue to Work for a Structure

                              Wow - nice one, Jay. I was a bit puzzled why this wouldn't work. Of course,
                              after looking at the solution it seems pretty obvious :) Thanks for this tip
                              (and thanks to Mattias Sjögren as well).

                              Imran.

                              "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                              news:%23uatZnTy EHA.3676@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                              > Charles,
                              > This just in!
                              >
                              > If you assign your structure to a System.ValueTyp e variable instead of a
                              > System.Object variable you can call the methods via reflection.
                              >
                              > For example (from your original post):
                              >
                              > Public Shared Function Main() As Integer
                              >
                              > Console.WriteLi ne("Reflection. PropertyInfo")
                              >
                              > ''Dim af As New MyProperty ' original
                              > Dim af As MyStructure
                              >
                              > af.Caption = "Default caption"
                              >
                              > Dim vaf As ValueType = af
                              >
                              > ' Get the type and PropertyInfo
                              > Dim MyType As Type = vaf.GetType
                              > Dim Mypropertyinfo As PropertyInfo = MyType.GetPrope rty("Caption")
                              >
                              > ' Get and display the GetValue method
                              > Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                              > Nothing).ToStri ng())
                              >
                              > ' Use the SetValue method to change the caption
                              > Mypropertyinfo. SetValue(vaf, "This caption has been changed.",
                              > Nothing)
                              >
                              > ' Get the caption again and display it
                              > Console.WriteLi ne("GetValue - " & Mypropertyinfo. GetValue(vaf,
                              > Nothing).ToStri ng())
                              >
                              > Console.ReadLin e()
                              >
                              > Return 0
                              >
                              > End Function
                              >
                              > Notice that I define a vaf variable that has type System.ValueTyp e, that
                              > this new variable is used when calling Reflection.
                              >
                              > This tip courtesy of Mattias Sjögren in the thread "Difference between
                              > boxing a primitive type and boxing a custom structure" in the
                              > microsoft.publi c.dotnet.framew ork newsgroup.
                              >
                              > Hope this helps
                              > Jay
                              >
                              >
                              > "Charles Law" <blank@nowhere. com> wrote in message
                              > news:%23t96Z%23 mxEHA.3108@TK2M SFTNGP14.phx.gb l...[color=green]
                              >> Jay, I love you, and want to have your babies. It does exactly what I
                              >> want.
                              >>
                              >> Thanks.
                              >>
                              >> Charles
                              >>
                              >>
                              >> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                              >> news:%23KGZTTmx EHA.3024@TK2MSF TNGP14.phx.gbl. ..[color=darkred]
                              >>> Charles,
                              >>> You can basically use the VB.NET code you started with:
                              >>>
                              >>> Something like:
                              >>>
                              >>> In a C# class library:
                              >>>
                              >>> ' don't want other inheriting from this class
                              >>> public sealed class PropertyHelper
                              >>> {
                              >>>
                              >>> ' Don't want others instantiating this class
                              >>> private PropertyHelper( )
                              >>> {
                              >>> }
                              >>>
                              >>> public static object SetValue(object o, string name, object
                              >>> value)
                              >>> {
                              >>> System.Reflecti on.PropertyInfo info =
                              >>> o.GetType().Get Property(name);
                              >>> info.SetValue(o , value, null);
                              >>> return o;
                              >>> }
                              >>>
                              >>> public static object SetValue(object o, string name, object
                              >>> value, object[] index)
                              >>> {
                              >>> System.Reflecti on.PropertyInfo info =
                              >>> o.GetType().Get Property(name);
                              >>> info.SetValue(o , value, index);
                              >>> return o;
                              >>> }
                              >>>
                              >>> }
                              >>>
                              >>> Then in VB.NET when you want to use it you can:
                              >>>
                              >>> Imports ClassLibrary1 ' whatever you called the above class library.
                              >>> Dim af As MyStructure
                              >>>
                              >>> af = DirectCast(Prop ertyHelper.SetV alue(af, "Caption", "This caption
                              >>> has been changed."), MyStructure)
                              >>>
                              >>> Hope this helps
                              >>> Jay
                              >>>
                              >>>
                              >>>
                              >>> "Charles Law" <blank@nowhere. com> wrote in message
                              >>> news:upntBMcxEH A.4028@TK2MSFTN GP15.phx.gbl...
                              >>>> Jay
                              >>>>
                              >>>> I don't use C# - would it be too much to ask for a skeleton that I
                              >>>> could use?
                              >>>>
                              >>>> Charles
                              >>>>
                              >>>>
                              >>> <<snip>>
                              >>>[/color]
                              >>
                              >>[/color]
                              >
                              >[/color]


                              Comment

                              Working...