LoginSignup
2
2

More than 5 years have passed since last update.

Property属性のオプション

Last updated at Posted at 2013-08-26

Property属性

Getter/Setter の定義で便利な Boo.Lang.Extensions.PropertyAttribute には、小技的なオプション指定ができます。

設定値をガードする。

条件式は assert と一緒に、setter メソッドの先頭に展開されます。

class A:
    [property(IntValue, value > 0)]
    intValue as int

プロパティへの設定をフックする

Observable:true を入れると、「プロパティ名Changed」という event がクラスに追加されます。

class A:
    [property(MyName, Observable:true )]
    myName as string

# 「プロパティ名Changed」というeventが使える。
a = A()
a.MyNameChanged += { o as A | print "kawatta!" + o.MyName }

a.MyName = 'abc'   # kawatta!abc と表示されます。

プロパティを protected にする。

class A:
    [property(MyName, Protected:true )]
    myName as string

オプションの組み合わせ

null や長さ0の文字列を設定したらエラーなプロパティ。ついでに上記の Observable も設定します。

class A:
    [property(MyName, Observable:true, not string.IsNullOrEmpty(value) )]
    myName as string

その他関連情報

Property属性の中身

http://qiita.com/nistake/items/d448937eba504fe54d25 の例のような構文木書き換えする属性です。

Unity3D での注意

Unityの中の人があろうことか「Property」という名前を勝手にEditorのプロパティ拡張用として定義しちゃったので、普通に「property」とか書くとコンパイルエラーになります。

import Boo.Lang.Extensions.PropertyAttribute as GetterSetter

とかして回避しましょう。

Boo導入しておいてこういうことする中の人許すまじ。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2