Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Best I've come up with so far:</p> <p>Step 1: Create TypeConverter's so that the Noda classes are editable</p> <pre><code>public class ToAndFromStringTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) return true; else return base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) return true; else return base.CanConvertTo(context, destinationType); } } public class LocalDateTypeConverter : ToAndFromStringTypeConverter { public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) { DateTime parsed; if (!DateTime.TryParse((string)value, out parsed)) throw new ArgumentException("Cannot convert '" + (string)value + "' to LocalDate."); else return new LocalDate(parsed.Year, parsed.Month, parsed.Day); } else { return base.ConvertFrom(context, culture, value); } } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { var tvalue = (LocalDate)value; try { var x = tvalue.ToString("yyyy-MM-dd"); return x; } catch (NullReferenceException) { return "1900-1-1"; } catch { throw new ArgumentException("Could not convert '" + value.ToString() + "' to LocalDate."); } } else return base.ConvertTo(context, culture, value, destinationType); } public class InstantTypeConverter : ToAndFromStringTypeConverter { public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) { try { DateTime parsed = DateTime.Parse((string)value); LocalDateTime dt = LocalDateTime.FromDateTime(parsed); Instant i = dt.InZoneLeniently(DateTimeZoneProviders.Default.GetSystemDefault()).ToInstant(); return i; } catch { throw new ArgumentException("Cannot convert '" + (string)value + "' to Instant."); } } else { return base.ConvertFrom(context, culture, value); } } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { try { Instant tvalue = (Instant)value; LocalDateTime local = tvalue.InZone(DateTimeZoneProviders.Default.GetSystemDefault()).LocalDateTime; string output = LocalDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss.FFFFFF").Format(local); return output; } catch { throw new ArgumentException("Could not convert '" + value.ToString() + "' to LocalDate."); } } else return base.ConvertTo(context, culture, value, destinationType); } } </code></pre> <p>Step 2: Register TypeConverters</p> <p>Put this code at the top of your app:</p> <pre><code>TypeDescriptor.AddAttributes(typeof(LocalDate), new TypeConverterAttribute(typeof(LocalDateTypeConverter))); TypeDescriptor.AddAttributes(typeof(Instant), new TypeConverterAttribute(typeof(InstantTypeConverter))); </code></pre> <p>Step 3: Use custom collection editor to handle things like List</p> <pre><code>public class NodaCollectionEditor : System.ComponentModel.Design.CollectionEditor { public NodaCollectionEditor(Type collection_type) : base(collection_type) { } protected override object CreateInstance(Type itemType) { if (itemType == typeof(LocalDate)) return LocalDateHelper.MinValue; else return base.CreateInstance(itemType); } } </code></pre> <p>This can be registered by adding this attribute to any appropriate properties: </p> <pre><code>[System.ComponentModel.Editor(typeof(NodaCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))] </code></pre>
    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. 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.
    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