ExpandableObjectConverter 类

定义

提供一个类型转换器,用于将可扩展对象与各种其他表示形式相互转换。

public ref class ExpandableObjectConverter : System::ComponentModel::TypeConverter
public class ExpandableObjectConverter : System.ComponentModel.TypeConverter
type ExpandableObjectConverter = class
    inherit TypeConverter
Public Class ExpandableObjectConverter
Inherits TypeConverter
继承
ExpandableObjectConverter
派生

示例

下面的代码示例将类型的 Margins 变量转换为字符串变量。

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(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))

下面的代码示例演示如何使用 NotifyParentPropertyAttributeExpandableObjectConverter 类在自定义控件上创建可扩展属性。 该代码示例是 NotifyParentPropertyAttribute 类中的一个较大示例的一部分。

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

    [Browsable(true),
    NotifyParentProperty(true),
    EditorBrowsable(EditorBrowsableState.Always),
    DefaultValue(1)]
    public int BorderSize
    {
        get => 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 => 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 若要使一种类型的属性可PropertyGrid展开,请为标准实现GetPropertiesSupportedGetProperties指定此属性TypeConverter。 使用 > 标记子属性,以确保控件中的 正确行为。

注释

不应直接访问类型转换器。 请改用 <a0/a0> 调用相应的转换器。 有关详细信息,请参阅基类中的 TypeConverter 示例。

有关类型转换器的详细信息,请参阅 TypeConverter 基类。

构造函数

名称 说明
ExpandableObjectConverter()

初始化 ExpandableObjectConverter 类的新实例。

方法

名称 说明
CanConvertFrom(ITypeDescriptorContext, Type)

返回此转换器是否可以使用指定的上下文将给定类型的对象转换为此转换器的类型。

(继承自 TypeConverter)
CanConvertFrom(Type)

返回此转换器是否可以将给定类型的对象转换为此转换器的类型。

(继承自 TypeConverter)
CanConvertTo(ITypeDescriptorContext, Type)

返回此转换器是否可以使用指定的上下文将对象转换为指定类型。

(继承自 TypeConverter)
CanConvertTo(Type)

返回此转换器是否可以将对象转换为指定类型。

(继承自 TypeConverter)
ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

使用指定的上下文和区域性信息将给定对象转换为此转换器的类型。

(继承自 TypeConverter)
ConvertFrom(Object)

将给定值转换为此转换器的类型。

(继承自 TypeConverter)
ConvertFromInvariantString(ITypeDescriptorContext, String)

使用固定区域性和指定的上下文将给定字符串转换为此转换器的类型。

(继承自 TypeConverter)
ConvertFromInvariantString(String)

使用固定区域性将给定字符串转换为此转换器的类型。

(继承自 TypeConverter)
ConvertFromString(ITypeDescriptorContext, CultureInfo, String)

使用指定的上下文和区域性信息将给定文本转换为对象。

(继承自 TypeConverter)
ConvertFromString(ITypeDescriptorContext, String)

使用指定的上下文将给定文本转换为对象。

(继承自 TypeConverter)
ConvertFromString(String)

将指定的文本转换为对象。

(继承自 TypeConverter)
ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

使用指定的上下文和区域性信息将给定值对象转换为指定类型。

(继承自 TypeConverter)
ConvertTo(Object, Type)

使用参数将给定值对象转换为指定类型。

(继承自 TypeConverter)
ConvertToInvariantString(ITypeDescriptorContext, Object)

使用指定的上下文将指定值转换为区域性固定字符串表示形式。

(继承自 TypeConverter)
ConvertToInvariantString(Object)

将指定的值转换为区域性固定字符串表示形式。

(继承自 TypeConverter)
ConvertToString(ITypeDescriptorContext, CultureInfo, Object)

使用指定的上下文和区域性信息将给定值转换为字符串表示形式。

(继承自 TypeConverter)
ConvertToString(ITypeDescriptorContext, Object)

使用给定上下文将给定值转换为字符串表示形式。

(继承自 TypeConverter)
ConvertToString(Object)

将指定的值转换为字符串表示形式。

(继承自 TypeConverter)
CreateInstance(IDictionary)

为对象重新创建一 Object 组给定的属性值。

(继承自 TypeConverter)
CreateInstance(ITypeDescriptorContext, IDictionary)

使用指定的上下文创建一个类型实例,该实例 TypeConverter 使用指定的上下文,给定对象的一组属性值。

(继承自 TypeConverter)
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetConvertFromException(Object)

返回在无法执行转换时引发的异常。

(继承自 TypeConverter)
GetConvertToException(Object, Type)

返回在无法执行转换时引发的异常。

(继承自 TypeConverter)
GetCreateInstanceSupported()

返回更改此对象上的值是否需要调用 CreateInstance(IDictionary) 该方法来创建新值。

(继承自 TypeConverter)
GetCreateInstanceSupported(ITypeDescriptorContext)

返回是否更改此对象上的值需要调用 CreateInstance(IDictionary) 才能使用指定的上下文创建新值。

(继承自 TypeConverter)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetProperties(ITypeDescriptorContext, Object, Attribute[])

获取由值参数指定的对象的类型的属性集合。

GetProperties(ITypeDescriptorContext, Object)

使用指定的上下文返回由值参数指定的数组类型的属性集合。

(继承自 TypeConverter)
GetProperties(Object)

返回由值参数指定的数组类型的属性集合。

(继承自 TypeConverter)
GetPropertiesSupported()

返回此对象是否支持属性。

(继承自 TypeConverter)
GetPropertiesSupported(ITypeDescriptorContext)

获取一个值,该值指示此对象是否支持使用指定的上下文的属性。

GetStandardValues()

从此类型转换器设计为数据类型的默认上下文中返回标准值的集合。

(继承自 TypeConverter)
GetStandardValues(ITypeDescriptorContext)

返回此类型转换器在提供格式上下文时设计的数据类型的标准值的集合。

(继承自 TypeConverter)
GetStandardValuesExclusive()

返回的标准值的 GetStandardValues() 集合是否为独占列表。

(继承自 TypeConverter)
GetStandardValuesExclusive(ITypeDescriptorContext)

返回的标准值的 GetStandardValues() 集合是否是使用指定上下文的可能值的排他列表。

(继承自 TypeConverter)
GetStandardValuesSupported()

返回此对象是否支持可从列表中选择的标准值集。

(继承自 TypeConverter)
GetStandardValuesSupported(ITypeDescriptorContext)

返回此对象是否支持使用指定上下文从列表中选取的标准值集。

(继承自 TypeConverter)
GetType()

获取当前实例的 Type

(继承自 Object)
IsValid(ITypeDescriptorContext, Object)

返回给定值对象是否对此类型和指定的上下文有效。

(继承自 TypeConverter)
IsValid(Object)

返回给定值对象是否对此类型有效。

(继承自 TypeConverter)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
SortProperties(PropertyDescriptorCollection, String[])

对属性的集合进行排序。

(继承自 TypeConverter)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅