シリアライズ

Booの予約語を見ながら言語を調査しているのですが、今回はtransientです。辞書を引くと

一時的な,つかのまの,はかない

とあるので、NonSerialized属性みたいなものかと予想したら、そのとおりでした。(^^;

import System.IO
import System.Runtime.Serialization.Formatters.Binary

# Booはデフォルトで[Serializable]が付く
class Person:
    [Property(Name)]
    name as string

    [Property(Age)]
    age as int

    # transientは[NonSerialized]に相当する
    [Property(Sex)]
    transient sex as string

    public override def ToString() as string:
        return "Name=${Name}, Age=${Age}, Sex=${Sex}"

# コンストラクタでは名前:でプロパティを初期化できる
man = Person(Name:"Taro", Age:20, Sex:"Male")
print man

# シリアライズを行う
# Name,Ageのみシリアライズされる
ms = MemoryStream()
bf = BinaryFormatter()
bf.Serialize(ms, man)
man = null

ms.Position = 0
man = bf.Deserialize(ms)
print man

/* 結果
Name=Taro, Age=20, Sex=Male
Name=Taro, Age=20, Sex=
 */

BooはSerializable属性をつけなくてもシリアライズ可能なんですね。