TypeConverter.IsValid 方法

定义

返回给定值对象对于此类型是否有效。Returns whether the given value object is valid for this type.

重载

IsValid(ITypeDescriptorContext, Object)

返回给定值对象对于此类型和此指定的上下文是否有效。Returns whether the given value object is valid for this type and for the specified context.

IsValid(Object)

返回给定值对象对于此类型是否有效。Returns whether the given value object is valid for this type.

IsValid(ITypeDescriptorContext, Object)

返回给定值对象对于此类型和此指定的上下文是否有效。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

参数

context
ITypeDescriptorContext

一个 ITypeDescriptorContext,用于提供格式上下文。An ITypeDescriptorContext that provides a format context.

value
Object

要测试其有效性的 ObjectThe Object to test for validity.

返回

如果指定值对于该对象有效,则为 true;否则为 falsetrue if the specified value is valid for this object; otherwise, false.

示例

有关此函数的示例, 请参见TypeConverter类。For an example of this function, see the TypeConverter class.

注解

使用 context 参数提取有关调用此转换器的环境的附加信息。Use the context parameter to extract additional information about the environment from which this converter is invoked. 此参数可以是null, 因此请始终选中此参数。This parameter can be null, so always check it. 同样,上下文对象的属性可以返回 nullAlso, properties on the context object can return null.

从开始.NET Framework 4.NET Framework 4 IsValid ,CanConvertFrom方法从和ConvertFrom方法中捕获异常。Starting in .NET Framework 4.NET Framework 4, the IsValid method catches exceptions from the CanConvertFrom and ConvertFrom methods. 如果输入值类型导致CanConvertFrom返回false, 或者输入值导致ConvertFrom引发异常, 则该方法将IsValid返回falseIf 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.

若要启用旧行为, 请将以下行插入到应用程序的配置文件中, 例如, application1。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>  

继承者说明

如果要转换的类型支持可验证的标准值, 请重写此方法。Override this method if the type you want to convert supports standard values that can be validated.

方法用于验证类型中的值, 而不是确定是否value可以转换为给定类型。 IsValid(ITypeDescriptorContext, Object)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. 例如, IsValid(ITypeDescriptorContext, Object)可用于确定给定值对于枚举类型是否有效。For example, IsValid(ITypeDescriptorContext, Object) can be used to determine if a given value is valid for an enumeration type. 有关示例,请参见 EnumConverterFor an example, see EnumConverter.

另请参阅

IsValid(Object)

返回给定值对象对于此类型是否有效。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

参数

value
Object

要测试其有效性的对象。The object to test for validity.

返回

如果指定值对于该对象有效,则为 true;否则为 falsetrue if the specified value is valid for this object; otherwise, false.

注解

从开始.NET Framework 4.NET Framework 4 IsValid ,CanConvertFrom方法从和ConvertFrom方法中捕获异常。Starting in .NET Framework 4.NET Framework 4, the IsValid method catches exceptions from the CanConvertFrom and ConvertFrom methods. 如果输入值类型导致CanConvertFrom返回false, 或者输入值导致ConvertFrom引发异常, 则该方法将IsValid返回falseIf 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.

若要启用旧行为, 请将以下行插入到应用程序的配置文件中, 例如, application1。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>  

继承者说明

如果要转换的类型支持可验证的标准值, 请重写此方法。Override this method if the type you want to convert supports standard values that can be validated.

方法用于验证类型中的值, 而不是确定是否value可以转换为给定类型。 IsValid(Object)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. 例如, IsValid(Object)可用于确定给定值对于枚举类型是否有效。For example, IsValid(Object) can be used to determine if a given value is valid for an enumeration type. 有关示例,请参见 EnumConverterFor an example, see EnumConverter.

可以通过在异常块中包装ConvertToConvertFrom方法来编写自己的 WillConvertSucceed 方法。You can write your own WillConvertSucceed method by wrapping the ConvertTo and ConvertFrom methods in exception blocks.

另请参阅

适用于