PropertyInfo.SetValue and custom type

  • Thread starter Thread starter Vladimir Semenov
  • Start date Start date
V

Vladimir Semenov

Hi all,

I have a problem with PropertyInfo.SetValue().

Task:
There is a generic code updating object properties using reflection. I don't
want to change this code.
One of property is of a custom type which is fully controlled by me.

Problem:
When the generic code tries to pass integer type to SetValue an exception is
thrown.

Question:
What should I do with custom type to ensure that SetValue automatically
converts integer to my custom type?
1) Declare constructor,
2) Declare TypeConverter
3) Implement conversion operator
4) ????

Thanks advance,

Vladimir
 
The implementation of the SetValue method of the ReflectPropertyDescriptor
class, derived from the abstract PropertyDescriptor, retrieves the "Set"
method of the object class that takes your type as parameter and calls it
using the Invoke method passing the value. AFAIK, converters are not
involved here (they are related to UI components) so try with a conversion
operator. If that fails, the calling code should be modified to use the
IConvertible interface that you should implement in your type.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Back
Top