次のコードは、70-536 Microsoft .NET 試験のメジャーアップ テストから取得したものです。
private void frmMain_Paint(object sender, PaintEventArgs e)
{
System.ComponentModel.TypeConverter rectConverter =
System.ComponentModel.TypeDescriptor.GetConverter(typeof(Rectangle));
Rectangle rect = (Rectangle)rectConverter.ConvertFromString("50,50,200,200"); //fails
e.Graphics.DrawRectangle(Pens.Black, rect);
rect.Inflate(-10, -10);
e.Graphics.DrawRectangle(Pens.Blue, rect);
}
しかし、ライン
Rectangle rect = (Rectangle)rectConverter.ConvertFromString("50,50,200,200");
「50,50,200,200」は Int32 の有効な値ではありません」で失敗します。私は TypeConverter クラスにまったく慣れていません。しかし、説明によると、これは座標 (50,50) に黒い四角形を描画することになっていました。サイズ (200,200). 次に、Inflate メソッドを使用して、黒い四角形の内側に新しい小さな青い四角形を描画します. しかし、ConvertFromString が失敗するのはなぜですか?
編集:
これは、Rectangle クラスに関する ConvertFromString メソッドの .NET 実装だと思います。