<定義>
private ScheduleCollection _ScheduleArray ;
<初期化>
_ScheduleArray = new ScheduleCollection() ;
<公開プロパティ>
[Description("スケジュールコレクションです。"),Category("設定")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(true), Bindable(true)]
public ScheduleCollection ScheduleArray
{
get
{
return this._ScheduleArray ;
}
set
{
this._ScheduleArray = value ;
}
}
<スケジュールクラス>
[Serializable()]
[TypeConverter(typeof(ScheduleConverter))]
public class Schedule
{
private DateTime _start ;
private TimeSpan _span ;
private String _message ;
public Schedule()
{
_start = DateTime.MinValue ;
_span = new TimeSpan(0) ;
_message = "" ;
}
public Schedule(DateTime start, TimeSpan span, String message)
{
_start = start ;
_span = span ;
_message = message ;
}
[Description("スケジュール開始日時です。"),Category("設定")]
public DateTime StartTime
{
get
{
return this._start ;
}
set
{
this._start = value ;
}
}
[Description("スケジュール期間です。"),Category("設定")]
public TimeSpan TimeSpan
{
get
{
return this._span ;
}
set
{
this._span = value ;
}
}
[Description("スケジュール内容です。"),Category("設定")]
public String Message
{
get
{
return this._message ;
}
set
{
this._message = value ;
}
}
}
<スケジュールクラスタイプコンバーター>
internal class ScheduleConverter : ExpandableObjectConverter
{
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context,
object value,
Attribute[] filter)
{
return TypeDescriptor.GetProperties(value, filter);
}
public override bool GetPropertiesSupported(
ITypeDescriptorContext context)
{
return true;
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom( ITypeDescriptorContext context,
System.Globalization.CultureInfo info, object value)
{
if (value is string)
{
try
{
string[] v = ((string)value).Split(new char[] {','});
return new Schedule( DateTime.Parse(v[0]), TimeSpan.Parse(v[1]), v[2] ) ;
}
catch
{}
throw new ArgumentException("Can not convert '" + (string)value + "' to type Schedule") ;
}
return base.ConvertFrom(context, info, value);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type destType)
{
if (destType == typeof(string) && value is Schedule)
{
Schedule p = (Schedule)value;
return p.StartTime.ToString("yyyy/MM/dd HH:mm:ss") + "," +
p.TimeSpan.ToString() + "," + p.Message ;
}
if (destType == typeof(InstanceDescriptor) && value is Schedule)
{
Schedule sc = (Schedule)value;
System.Reflection.ConstructorInfo ctor = typeof(Schedule).GetConstructor(new Type[] {typeof(DateTime), typeof(TimeSpan), typeof(String)});
if (ctor != null)
{
return new InstanceDescriptor(ctor, new object[] {sc.StartTime,sc.TimeSpan,sc.Message});
}
}
return base.ConvertTo(context, culture, value, destType);
}
}
<ScheduleCollectionクラス>
[Serializable()]
public class ScheduleCollection : CollectionBase
{
public Schedule this[ int index ]
{
get
{
return( (Schedule) List[index] );
}
set
{
List[index] = value;
}
}
public int Add( Schedule value )
{
return( List.Add( value ) );
}
public int IndexOf( Schedule value )
{
return( List.IndexOf( value ) );
}
public void Insert( int index, Schedule value )
{
List.Insert( index, value );
}
public void Remove( Schedule value )
{
List.Remove( value );
}
public bool Contains( Schedule value )
{
// If value is not of type Schedule, this will return false.
return( List.Contains( value ) );
}
protected override void OnInsert( int index, Object value )
{
if ( value.GetType() != Type.GetType("WindowsControlLibrary1.Schedule") )
throw new ArgumentException( "value must be of type Schedule. (" + value.GetType().ToString() + ")" , "value" );
}
protected override void OnRemove( int index, Object value )
{
if ( value.GetType() != Type.GetType("WindowsControlLibrary1.Schedule") )
throw new ArgumentException( "value must be of type Schedule. (" + value.GetType().ToString() + ")" , "value" );
}
protected override void OnSet( int index, Object oldValue, Object newValue )
{
if ( newValue.GetType() != Type.GetType("WindowsControlLibrary1.Schedule") )
throw new ArgumentException( "newValue must be of type Schedule. (" + newValue.GetType().ToString() + ")", "newValue" );
}
protected override void OnValidate( Object value )
{
if ( value.GetType() != Type.GetType("WindowsControlLibrary1.Schedule") )
throw new ArgumentException( "value must be of type Schedule. (" + value.GetType().ToString() + ")" );
}
}
|