Share via


expando Modifier

Declares that instances of a class support expando properties or that a method is an expando object constructor.

expando statement

Arguments

  • statement
    Required. A class or method definition.

Remarks

The expando modifier is used to mark a class as dynamically extensible (one that supports expando properties). Expando properties on expando class instances must be accessed using the [] notation; they are not accessible with the dot operator. The expando modifier also marks a method as an expando object constructor.

Classes and methods in classes can be marked with the expando modifier. Fields, properties, interfaces, and members of interfaces cannot take the expando modifier.

An expando class has a hidden, private property named Item that takes one Object parameter and returns an Object. You are not allowed to define a property with this signature on an expando class.

Example 1

The following example illustrates a use of the expando modifier on a class. The expando class is like a JScript Object, but there are some differences that are illustrated here.

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.

The output of this code is

10
ten
twelve
twelve

Example 2

The following example illustrates a use of the expando modifier on a method. When the expando method is called in the usual way, it accesses the field x. When the method is used as an explicit constructor with the new operator, it adds an expando property to a new object.

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.

The output of this code is

Method called as a function.
123
456
123

Requirements

Version .NET

See Also

Concepts

Scope of Variables and Constants

Type Annotation

Reference

static Modifier

var Statement

function Statement

class Statement

Other Resources

Modifiers