TypeConverter.IsValid Método
Definição
Retorna se o objeto de valor especificado é válido para esse tipo.Returns whether the given value object is valid for this type.
Sobrecargas
| IsValid(ITypeDescriptorContext, Object) |
Retorna se o objeto de valor fornecido é válido para esse tipo e para o contexto especificado.Returns whether the given value object is valid for this type and for the specified context. |
| IsValid(Object) |
Retorna se o objeto de valor especificado é válido para esse tipo.Returns whether the given value object is valid for this type. |
IsValid(ITypeDescriptorContext, Object)
Retorna se o objeto de valor fornecido é válido para esse tipo e para o contexto especificado.Returns whether the given value object is valid for this type and for the specified context.
public:
virtual bool IsValid(System::ComponentModel::ITypeDescriptorContext ^ context, System::Object ^ value);
public virtual bool IsValid (System.ComponentModel.ITypeDescriptorContext context, object value);
abstract member IsValid : System.ComponentModel.ITypeDescriptorContext * obj -> bool
override this.IsValid : System.ComponentModel.ITypeDescriptorContext * obj -> bool
Public Overridable Function IsValid (context As ITypeDescriptorContext, value As Object) As Boolean
Parâmetros
- context
- ITypeDescriptorContext
Um ITypeDescriptorContext que fornece um contexto de formato.An ITypeDescriptorContext that provides a format context.
Retornos
true se o valor especificado for válido para esse objeto; caso contrário, false.true if the specified value is valid for this object; otherwise, false.
Exemplos
Para obter um exemplo dessa função, consulte a TypeConverter classe.For an example of this function, see the TypeConverter class.
Comentários
Use o context parâmetro para extrair informações adicionais sobre o ambiente do qual esse conversor é invocado.Use the context parameter to extract additional information about the environment from which this converter is invoked. Esse parâmetro pode ser null , portanto, sempre verifique.This parameter can be null, so always check it. Além disso, as propriedades no objeto de contexto podem retornar null.Also, properties on the context object can return null.
A partir do .NET Framework 4, o IsValid método captura exceções dos CanConvertFrom métodos e ConvertFrom .Starting in .NET Framework 4, the IsValid method catches exceptions from the CanConvertFrom and ConvertFrom methods. Se o tipo de valor de entrada causar CanConvertFrom retorno false , ou se o valor de entrada fizer com que o ConvertFrom gere uma exceção, o IsValid método retornará false .If the input value type causes CanConvertFrom to return false, or if the input value causes ConvertFrom to raise an exception, the IsValid method returns false.
Para habilitar o comportamento herdado, insira as linhas a seguir no arquivo de configuração do aplicativo, por exemplo, application1.exe.config.To enable the legacy behavior, insert the following lines into the configuration file of the application, for example, application1.exe.config.
<configuration>
<appSettings>
<add key="UseCompatibleTypeConverterBehavior" value="true" />
</appSettings>
</configuration>
Notas aos Herdeiros
Substitua esse método se o tipo que você deseja converter der suporte a valores padrão que podem ser validados.Override this method if the type you want to convert supports standard values that can be validated.
O IsValid(ITypeDescriptorContext, Object) método é usado para validar um valor dentro do tipo em vez de determinar se value pode ser convertido para o tipo fornecido.The IsValid(ITypeDescriptorContext, Object) method is used to validate a value within the type rather than to determine if value can be converted to the given type. Por exemplo, IsValid(ITypeDescriptorContext, Object) pode ser usado para determinar se um determinado valor é válido para um tipo de enumeração.For example, IsValid(ITypeDescriptorContext, Object) can be used to determine if a given value is valid for an enumeration type. Para ver um exemplo, consulte EnumConverter.For an example, see EnumConverter.
Confira também
Aplica-se a
IsValid(Object)
Retorna se o objeto de valor especificado é válido para esse tipo.Returns whether the given value object is valid for this type.
public:
bool IsValid(System::Object ^ value);
public bool IsValid (object value);
member this.IsValid : obj -> bool
Public Function IsValid (value As Object) As Boolean
Parâmetros
- value
- Object
O objeto a ser testado quanto à validade.The object to test for validity.
Retornos
true se o valor especificado for válido para esse objeto; caso contrário, false.true if the specified value is valid for this object; otherwise, false.
Comentários
A partir do .NET Framework 4, o IsValid método captura exceções dos CanConvertFrom métodos e ConvertFrom .Starting in .NET Framework 4, the IsValid method catches exceptions from the CanConvertFrom and ConvertFrom methods. Se o tipo de valor de entrada causar CanConvertFrom retorno false , ou se o valor de entrada fizer com que o ConvertFrom gere uma exceção, o IsValid método retornará false .If the input value type causes CanConvertFrom to return false, or if the input value causes ConvertFrom to raise an exception, the IsValid method returns false.
Para habilitar o comportamento herdado, insira as linhas a seguir no arquivo de configuração do aplicativo, por exemplo, application1.exe.config.To enable the legacy behavior, insert the following lines into the configuration file of the application, for example, application1.exe.config.
<configuration>
<appSettings>
<add key="UseCompatibleTypeConverterBehavior" value="true" />
</appSettings>
</configuration>
Notas aos Herdeiros
Substitua esse método se o tipo que você deseja converter der suporte a valores padrão que podem ser validados.Override this method if the type you want to convert supports standard values that can be validated.
O IsValid(Object) método é usado para validar um valor dentro do tipo em vez de determinar se value pode ser convertido para o tipo fornecido.The IsValid(Object) method is used to validate a value within the type rather than to determine if value can be converted to the given type. Por exemplo, IsValid(Object) pode ser usado para determinar se um determinado valor é válido para um tipo de enumeração.For example, IsValid(Object) can be used to determine if a given value is valid for an enumeration type. Para ver um exemplo, consulte EnumConverter.For an example, see EnumConverter.
Você pode escrever seu próprio método WillConvertSucceed encapsulando os ConvertTo ConvertFrom métodos e nos blocos de exceção.You can write your own WillConvertSucceed method by wrapping the ConvertTo and ConvertFrom methods in exception blocks.