CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
SmartDateConverter.cs
Go to the documentation of this file.
1#if !NETFX_CORE
2//-----------------------------------------------------------------------
3// <copyright file="SmartDateConverter.cs" company="Marimer LLC">
4// Copyright (c) Marimer LLC. All rights reserved.
5// Website: https://cslanet.com
6// </copyright>
7// <summary>Converts values to and from a SmartDate.</summary>
8//-----------------------------------------------------------------------
9using System;
10using System.ComponentModel;
11
13{
17 public class SmartDateConverter : TypeConverter
18 {
27 public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
28 {
29 if (sourceType == typeof(string))
30 return true;
31 else if (sourceType == typeof(DateTime))
32 return true;
33 else if (sourceType == typeof(DateTimeOffset))
34 return true;
35 else if (sourceType == typeof(System.DateTime?))
36 return true;
37 return base.CanConvertFrom(context, sourceType);
38 }
39
48 public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
49 {
50 if (value is string)
51 return new SmartDate(System.Convert.ToString(value));
52 else if (value is DateTime)
53 return new SmartDate(System.Convert.ToDateTime(value));
54 else if (value == null)
55 return new SmartDate();
56 else if (value is System.DateTime?)
57 return new SmartDate((System.DateTime?)value);
58 else if (value is DateTimeOffset)
59 return new SmartDate(((DateTimeOffset)value).DateTime);
60 return base.ConvertFrom(context, culture, value);
61 }
62
71 public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType)
72 {
73 if (destinationType == typeof(string))
74 return true;
75 else if (destinationType == typeof(DateTime))
76 return true;
77 else if (destinationType == typeof(DateTimeOffset))
78 return true;
79 else if (destinationType == typeof(System.DateTime?))
80 return true;
81 return base.CanConvertTo(context, destinationType);
82 }
83
93 public override object ConvertTo(
94 System.ComponentModel.ITypeDescriptorContext context,
95 System.Globalization.CultureInfo culture, object value, System.Type destinationType)
96 {
97 SmartDate sd = (SmartDate)value;
98 if (destinationType == typeof(string))
99 return sd.Text;
100 else if (destinationType == typeof(DateTime))
101 return sd.Date;
102 else if (destinationType == typeof(DateTimeOffset))
103 return new DateTimeOffset(sd.Date);
104 else if (destinationType == typeof(System.DateTime?))
105 return new System.DateTime?(sd.Date);
106 return base.ConvertTo(context, culture, value, destinationType);
107 }
108 }
109}
110#endif
Converts values to and from a SmartDate.
override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType)
Determines if a SmartDate can be convert to a value.
override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
Converts values to a SmartDate.
override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
Converts a SmartDate to a value.
override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
Determines if a value can be converted to a SmartDate.
Provides a date data type that understands the concept of an empty date value.
Definition: SmartDate.cs:32
DateTime? Date
Gets or sets the date value.
Definition: SmartDate.cs:413
string Text
Gets or sets the date value.
Definition: SmartDate.cs:400