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

提供格式內容的 ITypeDescriptorContextAn 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 ConvertFrom , IsValid 方法會從和方法中CanConvertFrom捕捉例外狀況。Starting in .NET Framework 4.NET Framework 4, the IsValid method catches exceptions from the CanConvertFrom and ConvertFrom methods. 如果輸入值CanConvertFrom類型導致false傳回, 或輸入值導致引發例外狀況, ConvertFromIsValid方法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.

若要啟用舊版行為, 請將下列幾行插入應用程式的設定檔案, 例如 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 ConvertFrom , IsValid 方法會從和方法中CanConvertFrom捕捉例外狀況。Starting in .NET Framework 4.NET Framework 4, the IsValid method catches exceptions from the CanConvertFrom and ConvertFrom methods. 如果輸入值CanConvertFrom類型導致false傳回, 或輸入值導致引發例外狀況, ConvertFromIsValid方法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.

若要啟用舊版行為, 請將下列幾行插入應用程式的設定檔案, 例如 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.

另請參閱

適用於