expando 修飾子

クラスのインスタンスが expando プロパティをサポートすること、またはメソッドが expando オブジェクト コンストラクターであることを宣言します。

expando statement

引数

  • statement
    必ず指定します。 クラスまたはメソッドの定義。

解説

expando 修飾子は、クラスを動的に拡張できる (expando プロパティをサポートする) ことを示します。 expando クラス インスタンスの expando プロパティは、[] 表記を使ってアクセスする必要があります。ドット演算子ではアクセスできません。 expando 修飾子は、メソッドが expando オブジェクト コンストラクターであることを示します。

expando 修飾子は、クラスとクラスのメソッドに指定できます。 フィールド、プロパティ、インターフェイス、およびインターフェイスのメンバーには、expando 修飾子を使用できません。

expando クラスには Item という名前の隠ぺいされたプライベート プロパティがあり、Object パラメーターを 1 つ受け取り、Object を返します。 expando クラスでは、このシグネチャを持つプロパティは定義できません。

例 1

次のコードは、クラスに対する expando 修飾子の使用例です。 expando クラスは JScript の Object に似ていますが、次に示すような違いがあります。

expando class CExpandoExample {
   var x : int = 10;
}

// New expando class-based object.
var testClass : CExpandoExample = new CExpandoExample;
// New JScript Object.
var testObject : Object = new Object;

// Add expando properties to both objects.
testClass["x"] = "ten";
testObject["x"] = "twelve";

// Access the field of the class-based object.
print(testClass.x);      // Prints 10.
// Access the expando property.
print(testClass["x"]);   // Prints ten.

// Access the property of the class-based object.
print(testObject.x);     // Prints twelve.
// Access the same property using the [] operator.
print(testObject["x"]);  // Prints twelve.

このコードの出力は、次のようになります。

10
ten
twelve
twelve

例 2

次のコードは、メソッドに対する expando 修飾子の使用例です。 expando メソッドを通常の方法で呼び出すと、メソッドはフィールド x にアクセスします。 new 演算子を使って、メソッドを明示的なコンストラクターとして使用すると、新しいオブジェクトに expando プロパティが追加されます。

class CExpandoExample {
   var x : int;
   expando function constructor(val : int) {
      this.x = val;
      return "Method called as a function.";
   }
}

var test : CExpandoExample = new CExpandoExample;
// Call the expando method as a function.
var str = test.constructor(123);
print(str);        // The return value is a string.
print(test.x);     // The value of x has changed to 123.

// Call the expando method as a constructor.
var obj = new test.constructor(456);
// The return value is an object, not a string.
print(obj.x);      // The x property of the new object is 456.
print(test.x);     // The x property of the original object is still 123.

このコードの出力は、次のようになります。

Method called as a function.
123
456
123

必要条件

バージョン .NET

参照

参照

static 修飾子

var ステートメント

function ステートメント

class ステートメント

概念

変数と定数のスコープ

型の注釈

その他の技術情報

修飾子