[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Setting object property by reflection

Omikron

8/24/2008 8:27:00 AM

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
4 Answers

Jeroen Mostert

8/24/2008 11:19:00 AM

0

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.

Omikron

8/24/2008 12:38:00 PM

0

> 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 ?

Omikron

8/24/2008 12:45:00 PM

0

Ok, I think I find a solution. I need to check if the type implements
Parse method, if so I can invoke it.

Jeroen Mostert

8/24/2008 4:22:00 PM

0

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.