Results 1 to 5 of 5

Thread: [RESOLVED] Set property with unknown type using reflection

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Resolved [RESOLVED] Set property with unknown type using reflection

    Hey all,

    I have a string data packet with this structure...

    Code:
    deviceInstanceID.PropertyName|propertyValue
    In reality, it could look like this "DD2.isOn|False", or this, "RD0.Value|2.21", etc.

    When I get this packet, I parse it to get the class instance and am trying to use reflection to set the instance's property to the value. The problem I am having is getting the value cast into the correct type for the property.

    This is what I am trying....

    Code:
     Dim parts() As String = line.Split(SELECTION_LIST_SEPARATER)
                            If parts.Count = 2 Then
                                Dim subParts() As String = parts(0).Split("."c)
                                Dim pandID As String = subParts(0)
                                Dim propertyName As String = subParts(1)
                                Dim value As String = parts(1)
    
                                'get the device
                                Dim device As BaseDeviceClass = devices.getDevice(pandID)
                                If device IsNot Nothing Then
                                    'use reflection to assign the value to the property...
                                    Dim dType = device.GetType()
                                    Dim pI As Reflection.PropertyInfo = dType.GetProperty(propertyName)
                                    pI.SetValue(device, value, Nothing) <---this is no good
    
                                End If
                            End If
    However, when I make the call to pI.SetValue I get an ArgumentException because value is of type String, but the property type is not (it could be any type).

    I have tried this,

    Code:
    Dim t As Type = pI.PropertyType
    Dim _value As Object = CType(value, t)
    pI.SetValue(device, _value, Nothing)
    but it does not compile with a message "Type 't' is undefined"

    Does anyone know how to do what I am trying to do? I do have control over the packet contents and can modify it if needed.
    Thanks
    Kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,959

    Re: Set property with unknown type using reflection

    Yeah, I know exactly what you're trying to do, and I went down the same logical road too... but it does't work that way (of course, it doesn't why would it, that's the logical way it should work).

    I think once you get the "Type" you need to use the .Name Property along with GetType, pass it to DirectCast to coerce it into the right type. Maybe... I forget exactly how it did it... and it has to be the FQN for the type name too....

    I want to say it goes like this:
    Code:
    Dim t As Type = pI.PropertyType
    'Dim _value As Object = CType(value, t)
    pI.SetValue(device, directcast(value, GetType(t.name))
    It doesn't quite look right, which means it's probably closer to working than it should be. I don't have a way of checking it either.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: Set property with unknown type using reflection

    thanks tg.
    your post made me dive deep into the bowels of google and I found the answer.... Conversion.CTypeDynamic

    Code:
    Dim dType = device.GetType()
    Dim pI As Reflection.PropertyInfo = dType.GetProperty(propertyName)
    pI.SetValue(device, Conversion.CTypeDynamic(value, pI.PropertyType))
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,959

    Re: Set property with unknown type using reflection

    Sweet!

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Set property with unknown type using reflection

    Quote Originally Posted by kebo View Post
    ... I found the answer.... Conversion.CTypeDynamic
    Nice find Kevin.

    If you ever need to work pre-Framework 4.0, you can do it using default TypeConverter as well.
    Code:
    Public Function FromString(sourceString As String, toType As Type) As Object
       Dim ret As Object = Nothing
       Dim tc As TypeConverter = TypeDescriptor.GetConverter(toType)
       If tc IsNot Nothing Then
          If tc.CanConvertFrom(GetType(System.String)) Then
             ' this will throw an exception if sourceString cannot be converted to toType
             ret = tc.ConvertFrom(sourceString)
          End If
       End If
       Return ret
    End Function
    It appears that Conversion.CTypeDynamic is bit less strict on what it will convert than the TypeConverter. "12.3" converts to 12 (as integer) with Conversion.CTypeDynamic but throws an error with the TypeConverter.
    Last edited by TnTinMN; Apr 15th, 2015 at 05:12 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width