Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Sun, 24 Aug 2008 01:27:00 -0700 (PDT),    group: microsoft.public.dotnet.framework        back       


Setting object property by reflection   
Hi
I would like to set a property of an object (like Web UI control)
based on the property name to given value (as string.)

I have:


Dim obj_FinalProperty As System.Reflection.PropertyInfo =
obj_ref.GetType().GetProperty(PropertyName)

Dim obj_MethodInfo As MethodInfo =
obj_FinalProperty.GetSetMethod(True)
obj_MethodInfo.Invoke(obj_ref, Value)


obj_ref is the control reference. Now let's say I have a button and I
want to set its "Height" to "20" (provided as string). Height property
is System.Web.UI.WebControls.Unit, and I am getting exception when it
comes to Invoke call:

Object of type 'System.String' cannot be converted to type
'System.Web.UI.WebControls.Unit'. I don't know why this happens,
because when I write directly - button.Height = "20" everything is
fine. How can I overcome this problem ?

Many Thanks
Bartek
date: Sun, 24 Aug 2008 01:27:00 -0700 (PDT)   author:   Omikron

Re: Setting object property by reflection   
Omikron wrote:
> Hi
> I would like to set a property of an object (like Web UI control)
> based on the property name to given value (as string.)
> 
> I have:
> 
> 
> Dim obj_FinalProperty As System.Reflection.PropertyInfo =
> obj_ref.GetType().GetProperty(PropertyName)
> 
> Dim obj_MethodInfo As MethodInfo =
> obj_FinalProperty.GetSetMethod(True)
> obj_MethodInfo.Invoke(obj_ref, Value)
> 
There's no need to call .GetSetMethod(); you can call .SetValue() directly.

> obj_ref is the control reference. Now let's say I have a button and I
> want to set its "Height" to "20" (provided as string). Height property
> is System.Web.UI.WebControls.Unit, and I am getting exception when it
> comes to Invoke call:
> 
> Object of type 'System.String' cannot be converted to type
> 'System.Web.UI.WebControls.Unit'. I don't know why this happens,
> because when I write directly - button.Height = "20" everything is
> fine. How can I overcome this problem ?
> 
Visual Basic supports some convenient implicit conversions that reflected 
properties don't. You need to supply a value of the exact same type as the 
property, not merely one that's convertible to it. You can do this by 
calling Convert.ChangeType() and supplying the property's type.

-- 
J.
date: Sun, 24 Aug 2008 13:19:28 +0200   author:   Jeroen Mostert

Re: Setting object property by reflection   
> Visual Basic supports some convenient implicit conversions that reflected
> properties don't. You need to supply a value of the exact same type as the
> property, not merely one that's convertible to it. You can do this by
> calling Convert.ChangeType() and supplying the property's type.
>
> --
> J.

I tried this before as well. I had:

Dim obj_FinalProperty As System.Reflection.PropertyInfo =
obj_Control.GetType().GetProperty(PropertyName)

            If Not obj_FinalProperty Is Nothing AndAlso
obj_FinalProperty.CanWrite = True Then
                Try
                    Dim ValueType As Type =
System.Type.GetType(obj_FinalProperty.PropertyType.AssemblyQualifiedName.ToString(),
False, True)

                    If Not ValueType Is Nothing Then
                        obj_FinalProperty.SetValue(obj_Control,
System.Convert.ChangeType(Value, ValueType), Nothing)
                    End If

Still Convert.ChangeType returns exception about invalid conversion.
So I understand there is no way to apply string value to Unit type by
the reflection ?
date: Sun, 24 Aug 2008 05:38:02 -0700 (PDT)   author:   Omikron

Re: Setting object property by reflection   
Ok, I think I find a solution. I need to check if the type implements
Parse method, if so I can invoke it.
date: Sun, 24 Aug 2008 05:44:51 -0700 (PDT)   author:   Omikron

Re: Setting object property by reflection   
Omikron wrote:
>> Visual Basic supports some convenient implicit conversions that reflected
>> properties don't. You need to supply a value of the exact same type as the
>> property, not merely one that's convertible to it. You can do this by
>> calling Convert.ChangeType() and supplying the property's type.
>>
> 
> I tried this before as well. I had:
> 
> Dim obj_FinalProperty As System.Reflection.PropertyInfo =
> obj_Control.GetType().GetProperty(PropertyName)
> 
>             If Not obj_FinalProperty Is Nothing AndAlso
> obj_FinalProperty.CanWrite = True Then
>                 Try
>                     Dim ValueType As Type =
> System.Type.GetType(obj_FinalProperty.PropertyType.AssemblyQualifiedName.ToString(),
> False, True)
> 
What on earth is this supposed to achieve? Just stop at .PropertyType.

>                     If Not ValueType Is Nothing Then
>                         obj_FinalProperty.SetValue(obj_Control,
> System.Convert.ChangeType(Value, ValueType), Nothing)
>                     End If
> 
ValueType cannot possibly be Nothing. The property must have a type and it 
must be accessible, otherwise you wouldn't have been able to get it at all.

> Still Convert.ChangeType returns exception about invalid conversion.

Yes, I should have dug further. Convert only operates on system types. You 
want TypeConverter here.

> So I understand there is no way to apply string value to Unit type by
> the reflection ?

Yes, there is. Here's a full snippet:

       Dim PropertyInfo As PropertyInfo = 
obj_Control.GetType().GetProperty(PropertyName)
     If PropertyInfo Is Nothing Then Throw New 
InvalidOperationException(String.Format("Property '{0}' does not exist.", 
PropertyName))
     If Not PropertyInfo.CanWrite Then Throw New 
InvalidOperationException(String.Format("Property '{0}' is not writable.", 
PropertyName))
     PropertyInfo.SetValue(obj_Control, 
TypeDescriptor.GetConverter(PropertyInfo.PropertyType).ConvertFromString(Value), 
Nothing)

-- 
J.
date: Sun, 24 Aug 2008 18:21:40 +0200   author:   Jeroen Mostert

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us