⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagetoselfconverter.cs

📁 中文名:Windows Forms 程序设计 英文名:Windows Forms Programming in c# 作者: Chris Sells 翻译: 荣耀 蒋贤哲 出版社:人民
💻 CS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -