Conversion.CTypeDynamic Metodo
Definizione
Overload
CTypeDynamic(Object, Type) |
Converte un oggetto nel tipo specificato.Converts an object to the specified type. |
CTypeDynamic<TargetType>(Object) |
Converte un oggetto nel tipo generico specificato.Converts an object to the specified generic type. |
CTypeDynamic(Object, Type)
Converte un oggetto nel tipo specificato.Converts an object to the specified type.
public:
static System::Object ^ CTypeDynamic(System::Object ^ Expression, Type ^ TargetType);
public static object CTypeDynamic (object? Expression, Type TargetType);
public static object CTypeDynamic (object Expression, Type TargetType);
static member CTypeDynamic : obj * Type -> obj
Public Function CTypeDynamic (Expression As Object, TargetType As Type) As Object
Parametri
- Expression
- Object
Oggetto da convertire.The object to convert.
- TargetType
- Type
Tipo in cui convertire l'oggetto.The type to which to convert the object.
Restituisce
Oggetto il cui tipo in fase di esecuzione è il tipo di destinazione richiesto.An object whose type at run time is the requested target type.
Esempio
Nell'esempio seguente viene usato il CTypeDynamic
metodo per convertire un oggetto dinamico in una stringa usando la conversione definita dall'oggetto dinamico.The following example uses the CTypeDynamic
method to convert a dynamic object to a string by using the conversion defined by the dynamic object.
Imports System.Dynamic
Module Module1
Sub Main()
Dim dyn As Object = New SampleDynamicObject
Dim sampleString = CTypeDynamic(dyn, GetType(String))
Console.WriteLine(sampleString)
End Sub
End Module
Class SampleDynamicObject
Inherits DynamicObject
Public Overrides Function TryConvert(ByVal binder As ConvertBinder,
ByRef result As Object) As Boolean
If binder.Type = GetType(String) Then
result = "Sample String"
Return True
End If
Return False
End Function
End Class
Commenti
Il CTypeDynamic
metodo converte l'oggetto passato come Expression
parametro nel tipo specificato dal TargetType
parametro.The CTypeDynamic
method converts the object passed as the Expression
parameter to the type specified by the TargetType
parameter. Se l'oggetto è un oggetto dinamico, il CTypeDynamic
metodo applica le conversioni dinamiche disponibili.If the object is a dynamic object, the CTypeDynamic
method applies available dynamic conversions.
Il CTypeDynamic
metodo applica le conversioni dinamiche in base alla semantica di conversione definita dall'oggetto stesso.The CTypeDynamic
method applies dynamic conversions in accordance with the conversion semantics defined by the object itself. Se un oggetto dinamico eredita da DynamicObject , il CTypeDynamic
metodo tenta innanzitutto di eseguire la conversione utilizzando una conversione statica definita dall'utente.If a dynamic object inherits from DynamicObject, the CTypeDynamic
method first attempts to perform the conversion by using a user-defined, static conversion. Se la conversione statica definita dall'utente non riesce, il CTypeDynamic
metodo tenta di eseguire la conversione usando le conversioni dinamiche.If the user-defined, static conversion fails, the CTypeDynamic
method attempts to perform the conversion by using dynamic conversions. Se un oggetto dinamico implementa IDynamicMetaObjectProvider , il CTypeDynamic
metodo fornisce la precedenza alle conversioni dinamiche su conversioni statiche definite dall'utente.If a dynamic object implements IDynamicMetaObjectProvider, the CTypeDynamic
method gives precedence to dynamic conversions over user-defined, static conversions.
Vedi anche
- TryConvert(ConvertBinder, Object)
- BindConvert(ConvertBinder)
- CTypeDynamic<TargetType>(Object)
- Utilizzo di oggetti dinamici (Visual Basic)Working with Dynamic Objects (Visual Basic)
- Procedura dettagliata: Creazione e uso di oggetti dinamici (C# e Visual Basic)Walkthrough: Creating and Using Dynamic Objects (C# and Visual Basic)
Si applica a
CTypeDynamic<TargetType>(Object)
Converte un oggetto nel tipo generico specificato.Converts an object to the specified generic type.
public:
generic <typename TargetType>
static TargetType CTypeDynamic(System::Object ^ Expression);
public static TargetType? CTypeDynamic<TargetType> (object Expression);
public static TargetType CTypeDynamic<TargetType> (object Expression);
static member CTypeDynamic : obj -> 'argetType
Public Function CTypeDynamic(Of TargetType) (Expression As Object) As TargetType
Parametri di tipo
- TargetType
Tipo in cui convertire l'oggetto.The type to which to convert the object.
Parametri
- Expression
- Object
Oggetto da convertire.The object to convert.
Restituisce
- TargetType
Oggetto tipizzato in modo statico come tipo generico richiesto.An object statically typed as the requested generic type.
Esempio
Nell'esempio seguente viene usato il CTypeDynamic
metodo per convertire un oggetto dinamico in una stringa usando la conversione definita dall'oggetto dinamico.The following example uses the CTypeDynamic
method to convert a dynamic object to a string by using the conversion defined by the dynamic object.
Imports System.Dynamic
Module Module1
Sub Main()
Dim dyn As Object = New SampleDynamicObject
Dim str = CTypeDynamic(Of String)(dyn)
Console.WriteLine(str)
End Sub
End Module
Class SampleDynamicObject
Inherits DynamicObject
Public Overrides Function TryConvert(ByVal binder As ConvertBinder,
ByRef result As Object) As Boolean
If binder.Type = GetType(String) Then
result = "Sample String"
Return True
End If
Return False
End Function
End Class
Commenti
Il CTypeDynamic
metodo converte l'oggetto passato come Expression
parametro nel tipo specificato dal tipo del parametro generico.The CTypeDynamic
method converts the object passed as the Expression
parameter to the type specified by the type of the generic parameter. Se l'oggetto è un oggetto dinamico, il CTypeDynamic
metodo applica le conversioni dinamiche disponibili.If the object is a dynamic object, the CTypeDynamic
method applies available dynamic conversions.
Il CTypeDynamic
metodo applica le conversioni dinamiche in base alla semantica di conversione definita dall'oggetto stesso.The CTypeDynamic
method applies dynamic conversions in accordance with the conversion semantics defined by the object itself. Se un oggetto dinamico eredita da DynamicObject , il CTypeDynamic
metodo tenta innanzitutto di eseguire la conversione utilizzando una conversione statica definita dall'utente.If a dynamic object inherits from DynamicObject, the CTypeDynamic
method first attempts to perform the conversion by using a user-defined, static conversion. Se la conversione statica definita dall'utente non riesce, il CTypeDynamic
metodo tenta di eseguire la conversione usando le conversioni dinamiche.If the user-defined, static conversion fails, the CTypeDynamic
method attempts to perform the conversion by using dynamic conversions. Se un oggetto dinamico implementa IDynamicMetaObjectProvider , il CTypeDynamic
metodo fornisce la precedenza alle conversioni dinamiche su conversioni statiche definite dall'utente.If a dynamic object implements IDynamicMetaObjectProvider, the CTypeDynamic
method gives precedence to dynamic conversions over user-defined, static conversions.
Vedi anche
- TryConvert(ConvertBinder, Object)
- BindConvert(ConvertBinder)
- CTypeDynamic(Object, Type)
- Utilizzo di oggetti dinamici (Visual Basic)Working with Dynamic Objects (Visual Basic)
- Procedura dettagliata: Creazione e uso di oggetti dinamici (C# e Visual Basic)Walkthrough: Creating and Using Dynamic Objects (C# and Visual Basic)