messagetoselfconverter.cs

来自「中文名:Windows Forms 程序设计 英文名:Windows Form」· CS 代码 · 共 48 行

CS
48
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;
using System.Text;

namespace AlarmClockControlLibrary {
  internal class MessageToSelfConverter : ExpandableObjectConverter {
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
      if( destinationType == typeof(InstanceDescriptor) ) return true;
      return this.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {

      // If source value is an MessageToSelf type
      if( value is MessageToSelf ) {
        // Convert to string
        if( (destinationType == typeof(string)) ) {
          MessageToSelf messageToSelf = (MessageToSelf)value;
          return string.Format("{0}, {1}", messageToSelf.Occurence.ToString(), messageToSelf.Message);
        }

        // Convert to InstanceDescriptor
        if( (destinationType == typeof(InstanceDescriptor)) ) {
          MessageToSelf messageToSelf = (MessageToSelf)value;
          object[] properties = new object[2];
          Type[] types = new Type[2];
          // Color
          properties[0] = messageToSelf.Occurence;
          types[0] = typeof(DateTime);
          // Width
          properties[1] = messageToSelf.Message;
          types[1] = typeof(string);
          // Build constructor
          ConstructorInfo ci = typeof(MessageToSelf).GetConstructor(types);
          return new InstanceDescriptor(ci, properties);
        }
      }

      // Base ConvertTo if neither string or InstanceDescriptor required
      return base.ConvertTo(context, culture, value, destinationType);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?