expando 한정자

클래스의 인스턴스가 expando 속성을 지원하거나 메서드가 expando 개체 생성자임을 선언합니다.

expando statement

인수

  • statement
    필수적 요소로서, 클래스 또는 메서드 정의입니다.

설명

expando 한정자는 클래스를 동적 확장이 가능한, 즉 expando 속성을 지원하는 클래스로 표시하는 용도로 사용됩니다. expando 클래스 인스턴스의 expando 속성에는 [] 표기법을 사용하여 액세스해야 합니다. 도트 연산자로는 액세스할 수 없습니다. expando 한정자는 메서드를 expando 개체 생성자로 표시하기도 합니다.

클래스와 클래스의 메서드는 expando 한정자로 표시할 수 있으나 필드, 속성, 인터페이스 및 인터페이스의 멤버는 expando 한정자를 사용할 수 없습니다.

expando 클래스에는 Item이라는 숨겨진 private 속성이 있는데, 이 속성은 하나의 Object 매개 변수를 사용하고 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 문

개념

변수 및 상수의 범위

형식 주석

기타 리소스

한정자