Modificatore expando

Dichiara che le istanze di una classe supportano le proprietà expando o che un metodo è un costruttore di oggetti expando.

expando statement

Argomenti

  • statement
    Obbligatorio. Definizione di una classe o di un metodo.

Note

Il modificatore expando viene utilizzato per contrassegnare una classe come estensibile in modo dinamico, quindi in grado di supportare proprietà expando. Per accedere alle proprietà expando su istanze di classi expando è necessario utilizzare la notazione []. Non è possibile accedere ad esse mediante l'operatore punto. Il modificatore expando, inoltre, contrassegna un metodo come costruttore di oggetti expando.

Le classi e i metodi in esse specificati possono essere contrassegnati con il modificatore expando. I campi, le proprietà, le interfacce e i membri delle interfacce non possono essere contrassegnati con il modificatore expando.

Una classe expando dispone di una proprietà nascosta e privata denominata Item che accetta un parametro Object e restituisce un Object. Non è consentito definire una proprietà con questa firma su una classe expando.

Esempio 1

Nell'esempio riportato di seguito viene illustrato un utilizzo del modificatore expando su una classe. La classe expando si comporta come un Object JScript, ad eccezione di alcune differenze illustrate di seguito.

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.

L'output di questo codice è il seguente:

10
ten
twelve
twelve

Esempio 2

Nell'esempio riportato di seguito viene illustrato un utilizzo del modificatore expando su un metodo. Quando viene chiamato come di consueto, il metodo expando accede al campo x. Quando viene utilizzato come costruttore esplicito mediante l'operatore new, il metodo aggiunge una proprietà expando a un nuovo oggetto.

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.

L'output di questo codice è il seguente:

Method called as a function.
123
456
123

Requisiti

Versione .NET

Vedere anche

Riferimenti

Modificatore static

Istruzione var

Istruzione function

Istruzione class

Concetti

Ambito di variabili e costanti

Annotazione di tipi

Altre risorse

Modificatori