C# TypeDescriptor初瞭解

Whpitot發表於2018-01-28

說明
Provides information about the characteristics for a component, such as its attributes, properties, and events. This class cannot be inherited.
提供了一些關於元件特徵的一些資訊,例如:屬性(attributes),屬性(properties),事件,該類不能被整合。

GetProperties(Object)
Returns the collection of properties for a specified component. 針對某一特定元件,返回該元件的所有屬性集合。

TypeDescriptor is an extensible inspection mechanism for components: those classes that implement the IComponent interface. Unlike reflection, it does not inspect for methods. TypeDescriptor can be dynamically extended by several services available through the target component's Site. The following table shows these services.

得到特定屬性(Attribute)的值

PropertyDescriporCollection myPropertyDescriporCollection = TypeDescripor.GetProperties(this);
for(PropertyDescripor myPropertyDescripor in myPropertyDescriporCollection)
{
    FieldAttribute myFieldAttribute = myPropertyDescripor.Attributes(typeof(FieldAttribute)) as FunctionParameterAttribute
    
    string fieldName = TableAttribute.FieldName;//User
    DbType fileDbType = TableAttribute.DbType;// DbType.String
}

public Class User
{
    [Filed("User",DbType.String)]
    public string User { get; set; }
}

public Class FieldAttrubute:Attribute
{
    public FieldAttrubute(string name,DbType dbType)
    {
        Name = name;
        FieldDbType = dbType;
    }
    public string Name{ get; set; }
    public DbType FieldDbType { get; set; }
}
複製程式碼

相關文章