TypeDescriptor.GetProperties 方法

定義

傳回元件或類型之屬性的集合。

多載

GetProperties(Object, Attribute[], Boolean)

使用指定的屬性陣列做為篩選條件並使用自訂類型描述元,傳回指定元件的屬性集合。

GetProperties(Object, Boolean)

傳回使用預設類型描述元之指定元件的屬性集合。

GetProperties(Type, Attribute[])

使用指定的屬性陣列做為篩選條件,傳回元件指定類型的屬性集合。

GetProperties(Type)

傳回元件指定類型的屬性集合。

GetProperties(Object)

傳回指定元件的屬性集合。

GetProperties(Object, Attribute[])

使用指定的屬性陣列做為篩選條件,傳回指定元件的屬性集合。

GetProperties(Object, Attribute[], Boolean)

來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs

使用指定的屬性陣列做為篩選條件並使用自訂類型描述元,傳回指定元件的屬性集合。

C#
public static System.ComponentModel.PropertyDescriptorCollection GetProperties (object component, Attribute[] attributes, bool noCustomTypeDesc);
C#
public static System.ComponentModel.PropertyDescriptorCollection GetProperties (object component, Attribute[]? attributes, bool noCustomTypeDesc);

參數

component
Object

要為其取得屬性的元件。

attributes
Attribute[]

做為篩選條件使用的 Attribute 類型陣列。

noCustomTypeDesc
Boolean

true 表示不考慮自訂類型描述資訊,否則為 false

傳回

PropertyDescriptorCollection,其事件符合指定元件的指定屬性。

例外狀況

component 是跨處理序的遠端物件。

備註

的屬性 component 可能會與類別的屬性不同,因為如果 已月臺,月臺可以新增或移除屬性 component

參數 attributes 數位是用來篩選陣列。 篩選是由下列規則所定義:

  • 如果屬性沒有 Attribute 相同類別的 ,則屬性不會包含在傳回的陣列中。

  • 如果屬性是 類別的 Attribute 實例,則 屬性必須是完全相符的,或未包含在傳回的數位中。

  • Attribute如果指定實例且它是預設屬性,則即使 屬性中沒有的Attribute實例,它也會包含在傳回的數位中。

  • 如果 attributes 具有默認屬性, GetProperties 則方法會比對屬性未套用屬性時的案例。

component如果 參數為 null,則會傳回空的集合。

傳回集合的順序不保證在呼叫之間相同,因此一律先排序它再使用。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperties(Object, Boolean)

來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs

傳回使用預設類型描述元之指定元件的屬性集合。

C#
public static System.ComponentModel.PropertyDescriptorCollection GetProperties (object component, bool noCustomTypeDesc);

參數

component
Object

要為其取得屬性的元件。

noCustomTypeDesc
Boolean

true 表示不考慮自訂類型描述資訊,否則為 false

傳回

具有指定元件之屬性的 PropertyDescriptorCollection

例外狀況

component 是跨處理序的遠端物件。

備註

參數的屬性 component 可能與類別的屬性不同,因為如果參數已月臺,月臺可以新增或移除屬性 component

如果 為 componentnull,則會傳回空集合。

傳回集合的順序不保證在呼叫之間相同,因此一律先排序它再使用。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperties(Type, Attribute[])

來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs

使用指定的屬性陣列做為篩選條件,傳回元件指定類型的屬性集合。

C#
public static System.ComponentModel.PropertyDescriptorCollection GetProperties (Type componentType, Attribute[] attributes);
C#
public static System.ComponentModel.PropertyDescriptorCollection GetProperties (Type componentType, Attribute[]? attributes);

參數

componentType
Type

目標元件的 Type

attributes
Attribute[]

做為篩選條件使用的 Attribute 類型陣列。

傳回

PropertyDescriptorCollection,其屬性 (Property) 符合此類型元件的指定屬性 (Attribute)。

範例

下列程式代碼範例示範如何實作 GetProperties 方法。 此程式代碼範例是提供給 類別之較大範例的 PropertyTab 一部分。

C#
// Returns the properties of the specified component extended with 
// a CategoryAttribute reflecting the name of the type of the property.
public override System.ComponentModel.PropertyDescriptorCollection GetProperties(object component, System.Attribute[] attributes)
{
    PropertyDescriptorCollection props;
    if( attributes == null )
        props = TypeDescriptor.GetProperties(component);    
    else
        props = TypeDescriptor.GetProperties(component, attributes);    
    
    PropertyDescriptor[] propArray = new PropertyDescriptor[props.Count];            
    for(int i=0; i<props.Count; i++)           
    {                
        // Create a new PropertyDescriptor from the old one, with 
        // a CategoryAttribute matching the name of the type.
        propArray[i] = TypeDescriptor.CreateProperty(props[i].ComponentType, props[i], new CategoryAttribute(props[i].PropertyType.Name));
    }
    return new PropertyDescriptorCollection( propArray );
}

public override System.ComponentModel.PropertyDescriptorCollection GetProperties(object component)
{                     
    return this.GetProperties(component, null);
}

備註

只有在您沒有 對象的實例時,才呼叫這個版本的這個方法。

參數 attributes 數位是用來篩選陣列。 篩選是由下列規則所定義:

  • 如果屬性沒有 Attribute 相同類別的 ,則屬性不會包含在傳回的陣列中。

  • 如果屬性是 類別的 Attribute 實例,則 屬性必須是完全相符的,或未包含在傳回的數位中。

  • Attribute如果指定實例且它是預設屬性,則即使 屬性中沒有的Attribute實例,它也會包含在傳回的數位中。

  • 如果 attributes 具有默認屬性, GetProperties 則方法會比對屬性未套用屬性時的案例。

componentType如果 參數為 null,則會傳回空的集合。

傳回集合的順序不保證在呼叫之間相同,因此一律先排序它再使用。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperties(Type)

來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs

傳回元件指定類型的屬性集合。

C#
public static System.ComponentModel.PropertyDescriptorCollection GetProperties (Type componentType);

參數

componentType
Type

表示要取得屬性之元件的 Type

傳回

有指定元件類型屬性的 PropertyDescriptorCollection

備註

只有在您沒有 對象的實例時,才呼叫這個版本的這個方法。

componentType如果 參數為 null,則會傳回空的集合。

傳回集合的順序不保證在呼叫之間相同,因此一律先排序它再使用。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperties(Object)

來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs

傳回指定元件的屬性集合。

C#
public static System.ComponentModel.PropertyDescriptorCollection GetProperties (object component);

參數

component
Object

要為其取得屬性的元件。

傳回

具有指定元件之屬性的 PropertyDescriptorCollection

例外狀況

component 是跨處理序的遠端物件。

範例

下列程式代碼範例示範如何使用 GetProperties 方法來存取 控件的屬性。 此程式代碼範例是提供給 類別之較大範例的 ComponentDesigner 一部分。

C#
// This is the shadowed property on the designer.
// This value will be serialized instead of the 
// value of the control's property.
public Color BackColor
{
    get
    {
        return (Color)ShadowProperties["BackColor"];
    }
    set
    {
        if (this.changeService != null)
        {
            PropertyDescriptor backColorDesc =
                TypeDescriptor.GetProperties(this.Control)["BackColor"];

            this.changeService.OnComponentChanging(
                this.Control,
                backColorDesc);

            this.ShadowProperties["BackColor"] = value;

            this.changeService.OnComponentChanged(
                this.Control,
                backColorDesc,
                null,
                null);
        }
    }
}

備註

元件的屬性可能與類別的屬性不同,因為如果月臺已月臺,月臺可以新增或移除屬性。

component如果 參數為 null,則會傳回空的集合。

傳回集合的順序不保證在呼叫之間相同,因此一律先排序它再使用。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperties(Object, Attribute[])

來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs
來源:
TypeDescriptor.cs

使用指定的屬性陣列做為篩選條件,傳回指定元件的屬性集合。

C#
public static System.ComponentModel.PropertyDescriptorCollection GetProperties (object component, Attribute[] attributes);
C#
public static System.ComponentModel.PropertyDescriptorCollection GetProperties (object component, Attribute[]? attributes);

參數

component
Object

要為其取得屬性的元件。

attributes
Attribute[]

做為篩選條件使用的 Attribute 類型陣列。

傳回

PropertyDescriptorCollection,其屬性 (Property) 符合指定元件的指定屬性 (Attribute)。

例外狀況

component 是跨處理序的遠端物件。

範例

下列程式代碼範例示範如何實作 GetProperties 方法。 此程式代碼範例是提供給 類別之較大範例的 PropertyTab 一部分。

C#
// Returns the properties of the specified component extended with 
// a CategoryAttribute reflecting the name of the type of the property.
public override System.ComponentModel.PropertyDescriptorCollection GetProperties(object component, System.Attribute[] attributes)
{
    PropertyDescriptorCollection props;
    if( attributes == null )
        props = TypeDescriptor.GetProperties(component);    
    else
        props = TypeDescriptor.GetProperties(component, attributes);    
    
    PropertyDescriptor[] propArray = new PropertyDescriptor[props.Count];            
    for(int i=0; i<props.Count; i++)           
    {                
        // Create a new PropertyDescriptor from the old one, with 
        // a CategoryAttribute matching the name of the type.
        propArray[i] = TypeDescriptor.CreateProperty(props[i].ComponentType, props[i], new CategoryAttribute(props[i].PropertyType.Name));
    }
    return new PropertyDescriptorCollection( propArray );
}

public override System.ComponentModel.PropertyDescriptorCollection GetProperties(object component)
{                     
    return this.GetProperties(component, null);
}

備註

參數的屬性 component 可能與類別的屬性不同,因為如果參數已月臺,月臺可以新增或移除屬性 component

參數 attributes 數位是用來篩選陣列。 篩選是由下列規則所定義:

  • 如果屬性沒有 Attribute 相同類別的 ,則屬性不會包含在傳回的陣列中。

  • 如果屬性是 類別的 Attribute 實例,則 屬性必須是完全相符的,或未包含在傳回的數位中。

  • Attribute如果指定實例且它是預設屬性,則即使 屬性中沒有的Attribute實例,它也會包含在傳回的數位中。

  • 如果 attributes 具有默認屬性, GetProperties 則方法會比對屬性未套用屬性時的案例。

如果 為 componentnull,則會傳回空集合。

傳回集合的順序不保證在呼叫之間相同,因此一律先排序它再使用。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1