Home All Groups Group Topic Archive Search About

Reflection and SetValue .arrrrggggggghhhhh

Author
18 Dec 2006 4:52 AM
cwertman
Ok, Im missing something simple here.

I have an object o its a person , I want to "blank" out any properties
that are a type of a collection

The follwoing code will loop through and tell me if it is or not.

BUT I cannot figure out how to Set the Value

Dim myProperties() As PropertyInfo =
o.GetType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
            Dim PropertyItem As PropertyInfo
            Dim MyString As String

            For Each PropertyItem In myProperties

                With PropertyItem

                    If
GetType(ICollection).IsAssignableFrom(.PropertyType) Then
'This works to tell if its a collection
                        MyString = MyString & "Collection" &
..ReflectedType.ToString & " " & .PropertyType.Name & vbCrLf

'THIS IS WHAT DOES NOT WORK
                        PropertyItem.SetValue(o, .GetType, Nothing)

                    End If


                End With

            Next


Any Help would be super, I know its got to be just a syntax issue,
steroids, or lack of sleep :)

Thanks

Chris

Author
18 Dec 2006 5:13 AM
cwertman
Ok, got it.........

The typo was.....
PropertyItem.SetValue(o, .GetType, Nothing)

Should have been

PropertyItem.SetValue(o, .GetType.ReflectedType, Nothing)

Next Problem is how do I get the Object for that Property ?

I want to DEAL with it seperatley.

So say the collection contains addresses, I want to get the collection
as an Object and do something else with it.

GetValue ? but another typo ?????

Help on that would be....errr...helpful....

Sad lack of Reflection and Generics Documentation in VB.Net....or im
too tired to find it even though looking


cwert***@gmail.com wrote:
Show quoteHide quote
> Ok, Im missing something simple here.
>
> I have an object o its a person , I want to "blank" out any properties
> that are a type of a collection
>
> The follwoing code will loop through and tell me if it is or not.
>
> BUT I cannot figure out how to Set the Value
>
>  Dim myProperties() As PropertyInfo =
> o.GetType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
>             Dim PropertyItem As PropertyInfo
>             Dim MyString As String
>
>             For Each PropertyItem In myProperties
>
>                 With PropertyItem
>
>                     If
> GetType(ICollection).IsAssignableFrom(.PropertyType) Then
> 'This works to tell if its a collection
>                         MyString = MyString & "Collection" &
> .ReflectedType.ToString & " " & .PropertyType.Name & vbCrLf
>
> 'THIS IS WHAT DOES NOT WORK
>                         PropertyItem.SetValue(o, .GetType, Nothing)
>
>                     End If
>
>
>                 End With
>
>             Next
>
>
> Any Help would be super, I know its got to be just a syntax issue,
> steroids, or lack of sleep :)
>
> Thanks
>
> Chris
Author
18 Dec 2006 9:14 AM
Fabio Z
<cwert***@gmail.com> ha scritto nel messaggio

> Should have been
>
> PropertyItem.SetValue(o, .GetType.ReflectedType, Nothing)

mmm... I don't know what you are trying to do but I think this should be

PropertyItem.SetValue(o, Nothing, Nothing)


> Next Problem is how do I get the Object for that Property ?
>
> I want to DEAL with it seperatley.

Please describe a lillte clearly the scenario :)