ExpandableObjectConverter ExpandableObjectConverter ExpandableObjectConverter ExpandableObjectConverter Class

定義

提供型別轉換子將可擴展的物件與其他各種表示相互轉換。Provides a type converter to convert expandable objects to and from various other representations.

public ref class ExpandableObjectConverter : System::ComponentModel::TypeConverter
public class ExpandableObjectConverter : System.ComponentModel.TypeConverter
type ExpandableObjectConverter = class
    inherit TypeConverter
Public Class ExpandableObjectConverter
Inherits TypeConverter
繼承
ExpandableObjectConverterExpandableObjectConverterExpandableObjectConverterExpandableObjectConverter
衍生

範例

下列程式碼範例會將類型Margins的變數轉換成字串變數。The following code example converts a variable of type Margins to a string variable.

String^ strM = "1,2,3,4";
System::Drawing::Printing::Margins^ m = gcnew System::Drawing::Printing::Margins( 1,2,3,4 );
Console::WriteLine( TypeDescriptor::GetConverter( strM )->CanConvertTo( System::Drawing::Printing::Margins::typeid ) );
Console::WriteLine( TypeDescriptor::GetConverter( m )->ConvertToString( m ) );
string strM="1,2,3,4";
System.Drawing.Printing.Margins  m= new System.Drawing.Printing.Margins(1,2,3,4);
Console.WriteLine(TypeDescriptor.GetConverter(strM).CanConvertTo(typeof(System.Drawing.Printing.Margins))); 
Console.WriteLine(TypeDescriptor.GetConverter(m).ConvertToString(m)); 
Dim strM As String
strM = "1,2,3,4"
            Dim m As New System.Drawing.Printing.Margins(1, 2, 3, 4)
Console.WriteLine(TypeDescriptor.GetConverter(strM).CanConvertTo(GetType(System.Drawing.Printing.Margins)))
Console.WriteLine(TypeDescriptor.GetConverter(m).ConvertToString(m))

下列程式碼範例示範如何使用NotifyParentPropertyAttribute ExpandableObjectConverter和類別, 在自訂控制項上建立可擴充的屬性。The following code example demonstrates how to use the NotifyParentPropertyAttribute and the ExpandableObjectConverter class to create an expandable property on a custom control. 這個程式碼範例是針對NotifyParentPropertyAttribute類別提供之較大範例的一部分。This code example is part of a larger example provided for the NotifyParentPropertyAttribute class.

[TypeConverter(typeof(BorderAppearanceConverter))]
public class BorderAppearance
{
    private int borderSizeValue = 1;
    private Color borderColorValue = Color.Empty;

    [Browsable(true),
    NotifyParentProperty(true),
    EditorBrowsable(EditorBrowsableState.Always),
    DefaultValue(1)]
    public int BorderSize
    {
        get
        {
            return borderSizeValue;
        }
        set
        {
            if (value < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "BorderSize",
                    value,
                    "must be >= 0");
            }

            if (borderSizeValue != value)
            {
                borderSizeValue = value;
            }
        }
    }

    [Browsable(true)]
    [NotifyParentProperty(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    [DefaultValue(typeof(Color), "")]
    public Color BorderColor
    {
        get
        {
            return borderColorValue;
        }
        set
        {
            if (value.Equals(Color.Transparent))
            {
                throw new NotSupportedException("Transparent colors are not supported.");
            }

            if (borderColorValue != value)
            {
                borderColorValue = value;
            }
        }
    }
}
<TypeConverter(GetType(BorderAppearanceConverter))>  _
Public Class BorderAppearance
    Private borderSizeValue As Integer = 1
    Private borderColorValue As Color = Color.Empty
    
    
    <Browsable(True), NotifyParentProperty(True), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(1)>  _
    Public Property BorderSize() As Integer 
        Get
            Return borderSizeValue
        End Get
        Set
            If value < 0 Then
                Throw New ArgumentOutOfRangeException("BorderSize", value, "must be >= 0")
            End If
            
            If borderSizeValue <> value Then
                borderSizeValue = value
            End If
        End Set
    End Property
    
    
    <Browsable(True), NotifyParentProperty(True), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(GetType(Color), "")>  _
    Public Property BorderColor() As Color 
        Get
            Return borderColorValue
        End Get
        Set
            If value.Equals(Color.Transparent) Then
                Throw New NotSupportedException("Transparent colors are not supported.")
            End If
            
            If borderColorValue <> value Then
                borderColorValue = value
            End If
        End Set
    End Property
End Class

備註

這個類別會將物件上屬性的支援加入至所提供TypeConverter的方法和屬性。This class adds support for properties on an object to the methods and properties provided by TypeConverter. 若要讓屬性類型可在中展開PropertyGrid, 請針對TypeConverterGetPropertiesGetPropertiesSupported標準執行指定此項。To make a type of property expandable in the PropertyGrid, specify this TypeConverter for standard implementations of GetPropertiesSupported and GetProperties. 使用NotifyParentPropertyAttribute標記子屬性以確保PropertyGrid控制項中的行為正確。Mark child properties with the NotifyParentPropertyAttribute to ensure correct behavior in a PropertyGrid control.

注意

您不應該直接存取類型轉換器。You should never access a type converter directly. 相反地, 請使用TypeDescriptor來呼叫適當的轉換器。Instead, call the appropriate converter by using TypeDescriptor. 如需詳細資訊, 請參閱TypeConverter基類中的範例。For more information, see the examples in the TypeConverter base class.

如需類型轉換器的詳細資訊, 請TypeConverter參閱基底類別和如何:執行類型轉換器For more information about type converters, see the TypeConverter base class and How to: Implement a Type Converter.

建構函式

ExpandableObjectConverter() ExpandableObjectConverter() ExpandableObjectConverter() ExpandableObjectConverter()

初始化 ExpandableObjectConverter 類別的新執行個體。Initializes a new instance of the ExpandableObjectConverter class.

方法

CanConvertFrom(ITypeDescriptorContext, Type) CanConvertFrom(ITypeDescriptorContext, Type) CanConvertFrom(ITypeDescriptorContext, Type) CanConvertFrom(ITypeDescriptorContext, Type)

會傳回這個轉換子是否可以使用指定內容,將指定型別的物件轉換為這個轉換子的型別。Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.

(Inherited from TypeConverter)
CanConvertFrom(Type) CanConvertFrom(Type) CanConvertFrom(Type) CanConvertFrom(Type)

傳回值,指出這個轉換子是否可將指定之型別的物件轉換為這個轉換子的型別。Returns whether this converter can convert an object of the given type to the type of this converter.

(Inherited from TypeConverter)
CanConvertTo(ITypeDescriptorContext, Type) CanConvertTo(ITypeDescriptorContext, Type) CanConvertTo(ITypeDescriptorContext, Type) CanConvertTo(ITypeDescriptorContext, Type)

傳回這個轉換器是否可以使用指定的內容,將物件轉換成指定的類型。Returns whether this converter can convert the object to the specified type, using the specified context.

(Inherited from TypeConverter)
CanConvertTo(Type) CanConvertTo(Type) CanConvertTo(Type) CanConvertTo(Type)

傳回值,指出這個轉換子是否可以將物件轉換成指定的型別。Returns whether this converter can convert the object to the specified type.

(Inherited from TypeConverter)
ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

使用指定的內容和文化特性資訊,將指定物件轉換為這個轉換子的類型。Converts the given object to the type of this converter, using the specified context and culture information.

(Inherited from TypeConverter)
ConvertFrom(Object) ConvertFrom(Object) ConvertFrom(Object) ConvertFrom(Object)

會將指定值轉換為這個轉換子的型別。Converts the given value to the type of this converter.

(Inherited from TypeConverter)
ConvertFromInvariantString(ITypeDescriptorContext, String) ConvertFromInvariantString(ITypeDescriptorContext, String) ConvertFromInvariantString(ITypeDescriptorContext, String) ConvertFromInvariantString(ITypeDescriptorContext, String)

會使用不區分的文化特性和指定的內容,將指定的字串轉換為這個轉換子的型別。Converts the given string to the type of this converter, using the invariant culture and the specified context.

(Inherited from TypeConverter)
ConvertFromInvariantString(String) ConvertFromInvariantString(String) ConvertFromInvariantString(String) ConvertFromInvariantString(String)

會使用不因文化特性而異,將指定字串轉換為這個轉換子的型別。Converts the given string to the type of this converter, using the invariant culture.

(Inherited from TypeConverter)
ConvertFromString(ITypeDescriptorContext, CultureInfo, String) ConvertFromString(ITypeDescriptorContext, CultureInfo, String) ConvertFromString(ITypeDescriptorContext, CultureInfo, String) ConvertFromString(ITypeDescriptorContext, CultureInfo, String)

會使用指定內容和文化特性資訊,將指定文字轉換為物件。Converts the given text to an object, using the specified context and culture information.

(Inherited from TypeConverter)
ConvertFromString(ITypeDescriptorContext, String) ConvertFromString(ITypeDescriptorContext, String) ConvertFromString(ITypeDescriptorContext, String) ConvertFromString(ITypeDescriptorContext, String)

會使用指定的內容,將指定文字轉換為物件。Converts the given text to an object, using the specified context.

(Inherited from TypeConverter)
ConvertFromString(String) ConvertFromString(String) ConvertFromString(String) ConvertFromString(String)

會將指定的文字轉換成物件。Converts the specified text to an object.

(Inherited from TypeConverter)
ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

使用指定的內容和文化特性資訊,將指定值物件轉換成指定類型。Converts the given value object to the specified type, using the specified context and culture information.

(Inherited from TypeConverter)
ConvertTo(Object, Type) ConvertTo(Object, Type) ConvertTo(Object, Type) ConvertTo(Object, Type)

會使用引數,將指定值物件轉換成指定型別。Converts the given value object to the specified type, using the arguments.

(Inherited from TypeConverter)
ConvertToInvariantString(ITypeDescriptorContext, Object) ConvertToInvariantString(ITypeDescriptorContext, Object) ConvertToInvariantString(ITypeDescriptorContext, Object) ConvertToInvariantString(ITypeDescriptorContext, Object)

會使用指定的內容,將指定值轉換成不因文化特性而異的字串表示。Converts the specified value to a culture-invariant string representation, using the specified context.

(Inherited from TypeConverter)
ConvertToInvariantString(Object) ConvertToInvariantString(Object) ConvertToInvariantString(Object) ConvertToInvariantString(Object)

會將指定值轉換成不因文化特性而異的字串表示。Converts the specified value to a culture-invariant string representation.

(Inherited from TypeConverter)
ConvertToString(ITypeDescriptorContext, CultureInfo, Object) ConvertToString(ITypeDescriptorContext, CultureInfo, Object) ConvertToString(ITypeDescriptorContext, CultureInfo, Object) ConvertToString(ITypeDescriptorContext, CultureInfo, Object)

會使用指定的內容和文化特性資訊,將指定值轉換為字串表示。Converts the given value to a string representation, using the specified context and culture information.

(Inherited from TypeConverter)
ConvertToString(ITypeDescriptorContext, Object) ConvertToString(ITypeDescriptorContext, Object) ConvertToString(ITypeDescriptorContext, Object) ConvertToString(ITypeDescriptorContext, Object)

會使用指定的內容,將指定值轉換為字串表示。Converts the given value to a string representation, using the given context.

(Inherited from TypeConverter)
ConvertToString(Object) ConvertToString(Object) ConvertToString(Object) ConvertToString(Object)

會將指定值轉換成字串表示。Converts the specified value to a string representation.

(Inherited from TypeConverter)
CreateInstance(IDictionary) CreateInstance(IDictionary) CreateInstance(IDictionary) CreateInstance(IDictionary)

重新建立 Object,需為物件提供一組屬性值。Re-creates an Object given a set of property values for the object.

(Inherited from TypeConverter)
CreateInstance(ITypeDescriptorContext, IDictionary) CreateInstance(ITypeDescriptorContext, IDictionary) CreateInstance(ITypeDescriptorContext, IDictionary) CreateInstance(ITypeDescriptorContext, IDictionary)

使用指定的內容,建立與這個 TypeConverter 相關聯之類型的執行個體,並為物件提供一組屬性值。Creates an instance of the type that this TypeConverter is associated with, using the specified context, given a set of property values for the object.

(Inherited from TypeConverter)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetConvertFromException(Object) GetConvertFromException(Object) GetConvertFromException(Object) GetConvertFromException(Object)

無法執行轉換時,會傳回要擲回的例外狀況。Returns an exception to throw when a conversion cannot be performed.

(Inherited from TypeConverter)
GetConvertToException(Object, Type) GetConvertToException(Object, Type) GetConvertToException(Object, Type) GetConvertToException(Object, Type)

無法執行轉換時,會傳回要擲回的例外狀況。Returns an exception to throw when a conversion cannot be performed.

(Inherited from TypeConverter)
GetCreateInstanceSupported() GetCreateInstanceSupported() GetCreateInstanceSupported() GetCreateInstanceSupported()

傳回值,指出在這個物件上變更值時是否需要呼叫 CreateInstance(IDictionary) 方法來建立新值。Returns whether changing a value on this object requires a call to the CreateInstance(IDictionary) method to create a new value.

(Inherited from TypeConverter)
GetCreateInstanceSupported(ITypeDescriptorContext) GetCreateInstanceSupported(ITypeDescriptorContext) GetCreateInstanceSupported(ITypeDescriptorContext) GetCreateInstanceSupported(ITypeDescriptorContext)

會使用指定的內容,傳回在這個物件上變更值是否需要呼叫 CreateInstance(IDictionary) 來建立新值。Returns whether changing a value on this object requires a call to CreateInstance(IDictionary) to create a new value, using the specified context.

(Inherited from TypeConverter)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetProperties(ITypeDescriptorContext, Object) GetProperties(ITypeDescriptorContext, Object) GetProperties(ITypeDescriptorContext, Object) GetProperties(ITypeDescriptorContext, Object)

會使用指定的內容,傳回由值參數所指定之陣列型別的屬性集合。Returns a collection of properties for the type of array specified by the value parameter, using the specified context.

(Inherited from TypeConverter)
GetProperties(ITypeDescriptorContext, Object, Attribute[]) GetProperties(ITypeDescriptorContext, Object, Attribute[]) GetProperties(ITypeDescriptorContext, Object, Attribute[]) GetProperties(ITypeDescriptorContext, Object, Attribute[])

取得由值參數所指定的物件型別的屬性集合。Gets a collection of properties for the type of object specified by the value parameter.

GetProperties(Object) GetProperties(Object) GetProperties(Object) GetProperties(Object)

傳回由值參數所指定之陣列型別的屬性集合。Returns a collection of properties for the type of array specified by the value parameter.

(Inherited from TypeConverter)
GetPropertiesSupported() GetPropertiesSupported() GetPropertiesSupported() GetPropertiesSupported()

傳回值,指出這個物件是否支援屬性。Returns whether this object supports properties.

(Inherited from TypeConverter)
GetPropertiesSupported(ITypeDescriptorContext) GetPropertiesSupported(ITypeDescriptorContext) GetPropertiesSupported(ITypeDescriptorContext) GetPropertiesSupported(ITypeDescriptorContext)

使用指定內容取得值,表示這個物件是否使用支援屬性。Gets a value indicating whether this object supports properties using the specified context.

GetStandardValues() GetStandardValues() GetStandardValues() GetStandardValues()

從為這個型別轉換子所設計的資料型別預設內容中,會傳回標準值的集合。Returns a collection of standard values from the default context for the data type this type converter is designed for.

(Inherited from TypeConverter)
GetStandardValues(ITypeDescriptorContext) GetStandardValues(ITypeDescriptorContext) GetStandardValues(ITypeDescriptorContext) GetStandardValues(ITypeDescriptorContext)

當有提供格式內容時,會傳回這個型別轉換子設計之資料型別的標準值集合。Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.

(Inherited from TypeConverter)
GetStandardValuesExclusive() GetStandardValuesExclusive() GetStandardValuesExclusive() GetStandardValuesExclusive()

傳回值,這個值表示從 GetStandardValues() 所傳回的標準值集合是否為獨佔清單。Returns whether the collection of standard values returned from GetStandardValues() is an exclusive list.

(Inherited from TypeConverter)
GetStandardValuesExclusive(ITypeDescriptorContext) GetStandardValuesExclusive(ITypeDescriptorContext) GetStandardValuesExclusive(ITypeDescriptorContext) GetStandardValuesExclusive(ITypeDescriptorContext)

會使用指定的內容,傳回從 GetStandardValues() 所傳回的標準值集合是否為可能值的獨佔清單。Returns whether the collection of standard values returned from GetStandardValues() is an exclusive list of possible values, using the specified context.

(Inherited from TypeConverter)
GetStandardValuesSupported() GetStandardValuesSupported() GetStandardValuesSupported() GetStandardValuesSupported()

會傳回這個物件是否支援可以從清單中挑選的標準值組。Returns whether this object supports a standard set of values that can be picked from a list.

(Inherited from TypeConverter)
GetStandardValuesSupported(ITypeDescriptorContext) GetStandardValuesSupported(ITypeDescriptorContext) GetStandardValuesSupported(ITypeDescriptorContext) GetStandardValuesSupported(ITypeDescriptorContext)

會使用指定的內容傳回,這個物件是否支援可從清單中挑選的標準值集合。Returns whether this object supports a standard set of values that can be picked from a list, using the specified context.

(Inherited from TypeConverter)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsValid(ITypeDescriptorContext, Object) IsValid(ITypeDescriptorContext, Object) IsValid(ITypeDescriptorContext, Object) IsValid(ITypeDescriptorContext, Object)

會傳回指定值物件是否對這個型別和指定的內容有效。Returns whether the given value object is valid for this type and for the specified context.

(Inherited from TypeConverter)
IsValid(Object) IsValid(Object) IsValid(Object) IsValid(Object)

會傳回指定值物件是否對這個型別有效。Returns whether the given value object is valid for this type.

(Inherited from TypeConverter)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
SortProperties(PropertyDescriptorCollection, String[]) SortProperties(PropertyDescriptorCollection, String[]) SortProperties(PropertyDescriptorCollection, String[]) SortProperties(PropertyDescriptorCollection, String[])

排序屬性的集合。Sorts a collection of properties.

(Inherited from TypeConverter)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱