现代英语,英文趣解

PCMCIA:People Can’t Memorize Computer Industry Acronyms(人们不能记住电脑产业)

IBM: I Blame Microsoft(我责备微软)

  在C#中,许多地方都用到了特征属性Attribute,它可以用来描述类型或方法,从而赋予它们一些额外的功能或特性。事实上,当你打开Visual Studio并创建一个Windows Forms Application应用程序时,在默认的Program.cs文件中,Main函数就被标记上了特征属性。

国外字库软件介绍 (载入中...)

ISDN:It Still does Nothing(它还什么都没做)

BASIC: Bill’’s Attempt to Seize Industry Control(比尔的企图:夺取工业控制)

[STAThread]
static void Main()
{
}

Font/DA Juggler Plus. Items Included: 51-page user manual. 33-page utilities manual. Customer Support: Telephone support 713-353-1510, 90-day repair or replacement of faulty manual or defective disk. Operating System(s) Required: Mac-DOS 5.0 or higher (512k). PC Processor: Macintosh 512KE or higher. Diskette $59.95 Network: Most AppleTalk including AppleShare, TOPS & MacServe. Font/Desk Accessory(DA)Fkey/Sound Utility. Greatly Extends Power of Macintosh by Giving User Access to Hundreds of Fonts, DAs, FKeys & Sounds Simultaneously. System Resources Remain in FonL/DA Mover or Fkei/Sound Mover Files & Are Opened by Font/DA Juggler Plus. Features 100% MultiFinder Compatibility, Font Compression, DA List, Font List. Sound List, User-Definabie Hot Keys & Much More. ALSoft, Inc.

APPLE:Arrogance Produces Profit-Losing Entity(傲慢出废品)

ISDN: It Still Does Nothing(它仍然什么都不做)

  特征属性是一组继承自Attribute的类,通常被用来修饰类型或方法,在类型或方法定义之前以方括号括起来。如我们可以给类的属性添加特征属性使其可以被序列化为XML:

Font Downloader & Editor. Operating System(s) Required: Apple DOS 3.3. PC Processor: Apple II+. lle, & compatibles; Franklin Ace. Diskette $39.00 General Requirements: Printer, parallel printer card. User Can Design Special Types of Custom Characters, Foreign Language Symbols, Math or Electronic Symbols. Fonts for ProWriter, Apple Dot- Matrix, & A@ple imagewriter Are Provided. Additional Fonts Are Available on Separate Font Library Disks. Micro-W Corp.

SCSI:System Can’t See It(系统不能发现它)

DOS: Defunct Operating System(已死亡的操作系统)

public class Movie
{
  [XmlElement("MovieName")]
  public string Title
  { get; set; }

Font Effects. Version: 1.2. PC Processor: IBM PC & compatibles. Diskette $95.00 Utility Which Enables Users to Process Bit-Mapped Fonts in the SoftCraft or HP SoftFonts Format, SoftCraft EDF File, or AidusIMicrosoft Windows TIFF Format. Users Can Add Special Effects Such As Shadows, Outlines, Contours, or Patterns. SoftCraft, Inc.

DOS:Defunct Operating System(不存在的操作系统)

MACINTOSH: Most Applications Crash; If Not, The Operating System Hangs(大部分功能崩溃,如果没有,则操作系统挂起)

  [XmlElement("MovieRating")]
  public float Rating
  { get; set; }

Font Factory GS. Version: 1.3. Wilfried Ricken. Jul. 1989. Items lnciuded: Extra font disk. Customer Support: 11-5 Mon-Fri EST 904-576-9415 or on-line through AppleLink. America Online, GEnie & CompuServe. Operating System(s) Required: GS/OS (768k). PC Processor: Macintosh. 3.5" Diskette $39.95 (Order #: 4030) A Font Editor That Can Be Used to Change Fonts or Create New Fonts, Symbols or Pictures !'hat Can Be Used With Word Processing, Desktop Publishing & Drawing Programs. Compatible Programs Can Run on Any Apple 11 as Long as They Support "Standard IIGS Fonts". Seven Hills Software Corp.

BASIC:Bill’s Attempt to Seize Industry Control(比尔抓取企业控制杆的企图)

WINDOWS: Will Install Needless Data On Whole System(将在所有系统中安装无用数据)

  [XmlElement("MovieReleaseDate")]
  public DateTime ReleaseDate
  { get; set; }
}

Font Garden. Items Included: 256 pg. book with each font clearly displayed & described. Customer Support:One year unconditional guarantee. PC Processor: UNIX; MacOS. Diskette Price not set. A comprehensive collection of 500 fonts in both

IBM:I’ll Bounce Microsoft(我要撵走微软)

MICROSOFT: Most Intelligent Customers Realize Our Software Only Fools Teenagers

  如同上面的代码片段,特征属性允许接收参数,这要取决于你使用了什么样的特征属性类型。当然,在.NET中我们也可以自定义特征属性类,但前提是自定义类必须继承自Attribute类。下面是一个例子:

TrueType & Type 1 PostScript. Comes with the user- friendly Font View program. FreeBSD Mall.

DEC:Dot’t expect Cuts(别指望降价)

(大部分聪明的用户认识到我们的软件仅仅欺骗年轻人)

class MyToken : Attribute
{
  public string DisplayName { get; set; }

Font Solution Pack. Version: 2.0. May 1988. Customer Support: Free technical support (phone toll only). Operating System(s) Required: PC- or MS-DOS 3.1 or higher (512k). PC Processor: IBM PC, AT or compatible. Diskette $495.00 General Requirements: Ventura Publisher, Aidus PageMaker, or Microsoft Windows: or Microsoft Word, WordPerfect, or OfficeWriter. Package to Provide. Create, Customize, Add Special Effects to, & Install Bitmap Soft Fonts for the Most Popular PC Desktop Publishing Packages & Word Processors & HP LaserJet Series il Compatibles. SoftCraft, Inc.

CD-ROM:Consumer Device, Rendered Obsolete in Months(消费型设备,被宣布在数月内废止)

  public MyToken(string dn)
  { DisplayName = dn; }
}

Font Special Effects Pack. Aug. 1988. Customer Support: Free tachnical support (phone toll only). Operating Sys em(s) Required: PC- or MS-DO 3.1 or higher (512K). PC Processor: IBM PC, AT or compatible. Diskette $295.00 General Requirements: Ventura Publisher Aidus PageMaker, or Microsoft Windows; or Microsoft Word, WordPerfect, or OfficeWriter. Package to Provide, Customize, Add Special Effects to, & Install Bitmap Soft Fonts for the Most Popular PC Desktop Publishing Packages & Word Processors & HP LaserJet Series II Compatibles. SoftCraft, lnc,

OS/2:Obsolete Soon, too(也将很快废止)

  然后,我们在代码中使用上面我们自定义的特征属性类:

Font WorKs for Macintosh. Items Included: Browser/Installer/launcher front end software. Customer Support: 90 day warranty, free call back technical support. Operating System(s) Required: System 6. PC Piocessor: Any Mac. Nonstandard peripherals Required:CD-ROM drive. CD-ROM $9.95 Network: Apple File Sharing. BeachWare.

WWW:World Wild Waite(全球等)

public static class Operations
{
  [MyToken("Operation 1")]
  public static void Operation1()
  {
    System.Windows.Forms.MessageBox.Show("Hey, I'm operation 1!");
  }

Fontagenix. PC Processor: Apple Macintosh. Fontagenix I, II, III (Valfonts Collection) or IV,ea. 3.5" Diskette $39.50 General Requirements: Image Writer. All four. 3.5" Diskette $139.50 Fontagenix I Is a Collection of 11 Ritmap (Dot Vlatrix) Fonts. Fontagenix II Is a Collection of 11 Bitmap Fonts. Fontagenix III (Vatfonts Collection) Is a Collection of 12 Biptmap Fonts. Fontagenix IV Is a Collection of 13 Bitmap Fonts.

MACINTOSH:ost Applications Crash; If Not, The Operating System Hangs(大多数应用程序瘫痪,要不然就是系统挂起)

  [MyToken("Operation 2")]
  public static void Operation2()
  {
    System.Windows.Forms.MessageBox.Show("Hey, I'm operation 2!");     
  }
}

*Fontastic Bundle. 2001. Operating System(s) Required: Macintosh. CD-ROM $199.95 Extensis Corp.

  那么我们在方法前使用我们自定义的特征属性有什么意义呢?来看看一个实际的例子,我们通过反射来找出所有被标记成我们自定义的特征属性的方法。

Fontastic Plus. Version: 2.0. Items Included: Disk, user guide. Customer Support: Yes, telephone & BBS. PC Processor: Apple Macintosh. 3.5' Diskette $99.95 Specialized Bitmap Font Editor Designed to Create New Fonts & Typefaces or Customize Different Character Sets. User Can Create Bitmapped Logos, Foreign Characters, Clip Art & Custom Fonts. Scaling (up to 127 Points High) Rotation, Styling, & Editing Capabilities Make Font & Graphic Desiqn Easier. Includes Such Features as Keming Pairs & Fractional Character Widths; Allows for Printing on OuickDraw Printers. Also Has Integrated Font Mover. Altsys Corp.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

FontFlddler: Kerning Editor. Jul. 1993. Items Included: Manual, registration card. Customer Support: Free support. Operating System(s) Required: Windows 3.1 PC Processor: IBM PC & compatibles (2Mb). Diskette $99.95 (ISBN: 1-879464-05-5) General Requirements: TrueType, PostScript Type 1. Optimal configuration: 486 33Mz with 4Mb RAM & a hard disk. Network: Ethernet. Ares Software Corp.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
澳门钻石官网,        public Form1()
        {
            InitializeComponent();
            PopulateComBox();
        }

FontGen V.1. 1993. Items Included:VS laser word processor tool kit, font selection, manuals. Customer Support: Telephone (no charge) FAX/Mail/BBS. Operating System(s) Required: MS-DOS 2.X or higher. PC Processor: IBM PC (500k). $295.00 General Requirements: Laser printer. Font editor/generator for high speed precision design of fonts, barcodes, special symbols & logos at 300 or 600 dpi resolution. Features on-line help, pop-up menus, WYSIWYG display, single keystroke commands, rubberbanded design tools, preview editing & global funciton. Supports mouse & several scanners. Supports seven bitmapped font formats. VS Software.

        private void PopulateComBox()
        {
            MethodInfo[] methods = typeof(Operations).GetMethods();

FontMonger. Version: 1.0.4. Apr. 1991. PC Processor: Macintosh & Windows 3.0. 3.5" Diskette $99.95 (Order #.. FM1000) Provides Type Format Conversion in Any Direction, Among PostScript Type 1, Type 3, & TrueType Fonts. It Converts Type to Adobe Illustrator or EPS Files, So User Can Combine Graphics & Type, Then Install the Results Back Into a Font. Type Modification Abilities Include Access to Unencoded Characters, Generating Custom Oblique Characters, Generating Superior & Inferior Characters, & Generating En & EM Fractions. Ares Software Corp.

            MyToken token = null;
            List<KeyValuePair<String, MethodInfo>> items = new List<KeyValuePair<string, MethodInfo>>();

FontMonger Windows. Apr. 1992. Items Included: Dual media - 3.5" & 5.25" disks included; manual. Customer Support: Technical support available. Operating System(s) Required: Microsoft Windows 3.0 or higher. PC Processor: IBM 286, 386, 486, & compatibles. Diskette $149.95 (ISBN: 1-879464-01-2) General Requirements: Windows, outline fonts in TrueType, Intellifont, PostScript Type 1, PostScript Type 3, or Nimbus Q format. Bridges Competing Font Technologies by Letting Desktop Publishers Convert Between Adobe PostScript Type 1 & Type 3, Intellifont, Nimbus Q & TrueType (for Windows 3.1). Converted Fonts Can Be Sav

            foreach (MethodInfo method in methods)
            {
                token = Attribute.GetCustomAttribute(method, typeof(MyToken), false) as MyToken;
                if (token == null)
                {
                    continue;
                }

                items.Add(new KeyValuePair<String, MethodInfo>(token.DisplayName, method));
            }

            this.comboBox1.DataSource = items;
            this.comboBox1.DisplayMember = "Key";
            this.comboBox1.ValueMember = "Value";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedIndex < 0)
            {
                return;
            }

            MethodInfo method = this.comboBox1.SelectedValue as MethodInfo;
            if (method != null)
            {
                method.Invoke(null, null);
            }
        }
    }

    public class MyToken : Attribute
    {
        public string DisplayName { get; set; }

        public MyToken(string dn)
        { DisplayName = dn; }
    }

    public static class Operations
    {
        [Obsolete]
        public static void Operation0()
        {
            System.Windows.Forms.MessageBox.Show("Hey, I'm operation 0!");
        }

        [MyToken("Operation 1")]
        public static void Operation1()
        {
            System.Windows.Forms.MessageBox.Show("Hey, I'm operation 1!");
        }

        [MyToken("Operation 2")]
        public static void Operation2()
        {
            System.Windows.Forms.MessageBox.Show("Hey, I'm operation 2!");
        }

        public static void Operation3()
        {
            System.Windows.Forms.MessageBox.Show("Hey, I'm operation 3!");
        }
    }
}

  上面是一个WinForm程序的完整代码,窗体上一共两个控件,一个Combox,一个Button。程序启动时调用PopulateComBox()方法,通过反射找出Operations类中所有被标记为MyToken特征属性的方法,添加到集合List中。这里有两个地方需要说明一下:

  1. 通过Attribute.GetCustomAttribute()方法尝试找到MethodInfo[]数组中所有被标记为MyToken特征属性的方法。注意GetCustomAttribute()方法的参数形式。typeof(Operations).GetMethods()用来返回类型Operations中所有public访问级别的方法。

  2. 集合items被定义为List泛型类型,该List泛型类型被指定为具有名值对的形式。实际上你也可以使用任何具有名值对的集合类型,如Hashtable。

  当点击按钮的时候,程序通过ComBox.SelectedValue属性找到当前选中的方法名称,然后使用Invoke()方法执行它,下面是程序执行时的效果截图。

澳门钻石官网 1

 

 

澳门钻石官网 2

  你应该注意到了在ComBox中只有两项:Operation1和Operation2。但是我们看到Operations类中一共有四个被定义为public访问级别的static方法,为什么反射只找到了两个方法呢?这是因为我们在反射中规定只找出被定义为MyToken特征属性的方法,方法Operation0被定义为特征属性Obsolete(该特征属性用于标识方法已过期并不再使用,你应该在一些早期.NET版本中发布的类型或方法中见过这个标识,或者在代码的智能提示中见过它)而非MyToken,方法Operation3不包含有特征属性。

  反射的最大好处就是可以动态地加载程序集,并获取程序集中的类型,以及类型中的属性和方法,并执行它们。在上例中,如果你想添加一个新的Operation方法并让它在ComBox中出现且能被执行,你只需要做一件事情,那就是在Operations类中增加一个方法并标注为MyToken特征属性,同时加上一些你期待执行的代码。

本文由澳门钻石唯一旗舰赌场发布于澳门钻石官网,转载请注明出处:现代英语,英文趣解

您可能还会对下面的文章感兴趣: