Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's the code used in one of our existing apps... not sure if it's ideal but it works well for us.</p> <pre><code>/// &lt;summary&gt; /// Attempts to convert a value using any customer TypeConverters applied to the member /// &lt;/summary&gt; /// &lt;param name="value"&gt;Object containing the value to be assigned&lt;/param&gt; /// &lt;param name="member"&gt;Member to be assigned to&lt;/param&gt; /// &lt;returns&gt;&lt;paramref name="value"/&gt; converted to the appropriate type&lt;/returns&gt; public static Object CustomTypeConversion ( object value, MemberInfo member ) { if ( value == null || value == DBNull.Value ) return value; if ( member == null ) throw new ArgumentNullException ( ); List&lt;TypeConverter&gt; converters = GetCustomTypeConverters ( member ); foreach ( TypeConverter c in converters ) { if ( c.CanConvertFrom ( value.GetType ( ) ) ) return c.ConvertFrom ( value ); } if ( member is PropertyInfo ) { PropertyInfo prop = member as PropertyInfo; return ConvertToNative( value , prop.PropertyType ); } return ConvertToNative ( value, member.MemberType.GetType ( ) ); } /// &lt;summary&gt; /// Extracts and instantiates any customer type converters assigned to a /// derivitive of the &lt;see cref="System.Reflection.MemberInfo"/&gt; property /// &lt;/summary&gt; /// &lt;param name="member"&gt;Any class deriving from MemberInfo&lt;/param&gt; /// &lt;returns&gt;A list of customer type converters, empty if none found&lt;/returns&gt; public static List&lt;TypeConverter&gt; GetCustomTypeConverters ( System.Reflection.MemberInfo member ) { List&lt;TypeConverter&gt; result = new List&lt;TypeConverter&gt;(); try { foreach ( TypeConverterAttribute a in member.GetCustomAttributes( typeof( TypeConverterAttribute ) , true ) ) { TypeConverter converter = Activator.CreateInstance( Type.GetType( a.ConverterTypeName ) ) as TypeConverter; if ( converter != null ) result.Add( converter ); } } catch { // Let it go, there were no custom converters } return result; } /// &lt;summary&gt; /// Attempts to cast the incoming database field to the property type /// &lt;/summary&gt; /// &lt;param name="value"&gt;Database value to cast&lt;/param&gt; /// &lt;param name="castTo"&gt;Type to cast to&lt;/param&gt; /// &lt;returns&gt;The converted value, if conversion failed the original value will be returned&lt;/returns&gt; public static object ConvertToNative ( object value , Type castTo ) { try { return Convert.ChangeType( value , castTo , System.Threading.Thread.CurrentThread.CurrentCulture ); } catch { return value; } } </code></pre> <p>Just call the CustomTypeConversion method and away you go... probably a <em>little</em> more than you need but being thorough isn't a crime (or is it?).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload