DynamicObject.TrySetIndex(SetIndexBinder, Object[], Object) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Fornisce l'implementazione per le operazioni che impostano un valore per indice. Le classi derivate dalla classe possono eseguire l'override DynamicObject di questo metodo per specificare il comportamento dinamico per le operazioni che accedono agli oggetti da un indice specificato.
public:
virtual bool TrySetIndex(System::Dynamic::SetIndexBinder ^ binder, cli::array <System::Object ^> ^ indexes, System::Object ^ value);
public virtual bool TrySetIndex(System.Dynamic.SetIndexBinder binder, object[] indexes, object value);
abstract member TrySetIndex : System.Dynamic.SetIndexBinder * obj[] * obj -> bool
override this.TrySetIndex : System.Dynamic.SetIndexBinder * obj[] * obj -> bool
Public Overridable Function TrySetIndex (binder As SetIndexBinder, indexes As Object(), value As Object) As Boolean
Parametri
- binder
- SetIndexBinder
Fornisce informazioni sull'operazione.
- indexes
- Object[]
Indici utilizzati nell'operazione. Ad esempio, per l'operazione sampleObject[3] = 10 in C# (sampleObject(3) = 10 in Visual Basic), dove sampleObject è derivato dalla classe DynamicObject, indexes[0] è uguale a 3.
- value
- Object
Valore da impostare sull'oggetto con l'indice specificato. Ad esempio, per l'operazione sampleObject[3] = 10 in C# (sampleObject(3) = 10 in Visual Basic), dove sampleObject deriva dalla classe DynamicObject, value è uguale a 10.
Valori restituiti
true se l'operazione riesce; in caso contrario, false. Se questo metodo restituisce false, il binder di runtime del linguaggio determina il comportamento. Nella maggior parte dei casi viene generata un'eccezione di runtime specifica del linguaggio.
Esempio
Si supponga di voler creare un oggetto in cui è possibile accedere alle proprietà in base a nomi quali Property0, Property1 e così via o per indice, in modo che, ad esempio, sampleObject.Property0 equivale a sampleObject[0] in C# o sampleObject(0) in Visual Basic.
Nell'esempio di codice seguente viene illustrata la SampleDynamicObject classe , derivata dalla DynamicObject classe . La classe SampleDynamicObject esegue l'override dei TrySetIndex metodi e TryGetIndex per abilitare l'accesso in base all'indice. Esegue l'override dei TrySetMember metodi e TryGetMember per abilitare l'accesso in base al nome della proprietà.
// The class derived from DynamicObject.
public class SampleDynamicObject : DynamicObject
{
// The inner dictionary to store field names and values.
Dictionary<string, object> dictionary
= new Dictionary<string, object>();
// Get the property value.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
return dictionary.TryGetValue(binder.Name, out result);
}
// Set the property value.
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
dictionary[binder.Name] = value;
return true;
}
// Set the property value by index.
public override bool TrySetIndex(
SetIndexBinder binder, object[] indexes, object value)
{
int index = (int)indexes[0];
// If a corresponding property already exists, set the value.
if (dictionary.ContainsKey("Property" + index))
dictionary["Property" + index] = value;
else
// If a corresponding property does not exist, create it.
dictionary.Add("Property" + index, value);
return true;
}
// Get the property value by index.
public override bool TryGetIndex(
GetIndexBinder binder, object[] indexes, out object result)
{
int index = (int)indexes[0];
return dictionary.TryGetValue("Property" + index, out result);
}
}
class Program
{
static void Test(string[] args)
{
// Creating a dynamic object.
dynamic sampleObject = new SampleDynamicObject();
// Creating Property0.
// The TrySetMember method is called.
sampleObject.Property0 = "Zero";
// Getting the value by index.
// The TryGetIndex method is called.
Console.WriteLine(sampleObject[0]);
// Setting the property value by index.
// The TrySetIndex method is called.
// (This method also creates Property1.)
sampleObject[1] = 1;
// Getting the Property1 value.
// The TryGetMember method is called.
Console.WriteLine(sampleObject.Property1);
// The following statement produces a run-time exception
// because there is no corresponding property.
//Console.WriteLine(sampleObject[2]);
}
}
// This code example produces the following output:
// Zero
// 1
' The class derived from DynamicObject.
Public Class SampleDynamicObject
Inherits DynamicObject
' The inner dictionary to store field names and values.
Dim dictionary As New Dictionary(Of String, Object)
' Get the property value.
Public Overrides Function TryGetMember(
ByVal binder As System.Dynamic.GetMemberBinder,
ByRef result As Object) As Boolean
Return dictionary.TryGetValue(binder.Name, result)
End Function
' Set the property value.
Public Overrides Function TrySetMember(
ByVal binder As System.Dynamic.SetMemberBinder,
ByVal value As Object) As Boolean
dictionary(binder.Name) = value
Return True
End Function
' Set the property value by index.
Public Overrides Function TrySetIndex(
ByVal binder As System.Dynamic.SetIndexBinder,
ByVal indexes() As Object, ByVal value As Object) As Boolean
Dim index As Integer = CInt(indexes(0))
' If a corresponding property already exists, set the value.
If (dictionary.ContainsKey("Property" & index)) Then
dictionary("Property" & index) = value
Else
' If a property does not exist, create it.
dictionary.Add("Property" & index, value)
End If
Return True
End Function
' Get the property value by index.
Public Overrides Function TryGetIndex(
ByVal binder As System.Dynamic.GetIndexBinder,
ByVal indexes() As Object, ByRef result As Object) As Boolean
Dim index = CInt(indexes(0))
Return dictionary.TryGetValue("Property" & index, result)
End Function
End Class
Sub Test()
' Creating a dynamic object.
Dim sampleObject As Object = New SampleDynamicObject()
' Creating Property0.
' The TrySetMember method is called.
sampleObject.Property0 = "Zero"
' Getting the value by index.
' The TryGetIndex method is called.
Console.WriteLine(sampleObject(0))
' Setting the property value by index.
' The TrySetIndex method is called.
' (This method also creates Property1.)
sampleObject(1) = 1
' Getting the Property1 value.
' The TryGetMember method is called.
Console.WriteLine(sampleObject.Property1)
' The following statement produces a run-time exception
' because there is no corresponding property.
' Console.WriteLine(sampleObject(2))
End Sub
' This code example produces the following output:
' Zero
' 1
Commenti
Le classi derivate dalla classe possono eseguire l'override DynamicObject di questo metodo per specificare come devono essere eseguite le operazioni che accedono a un oggetto in base all'indice per un oggetto dinamico. Quando il metodo non viene sottoposto a override, il binder di runtime del linguaggio determina il comportamento. Nella maggior parte dei casi viene generata un'eccezione di runtime specifica del linguaggio.
Se questo metodo viene sottoposto a override, viene richiamato automaticamente quando si dispone di un'operazione come sampleObject[3] = 10 in C# o sampleObject(3) = 10 in Visual Basic, dove sampleObject deriva dalla classe DynamicObject.