C#如何在衍生類別中不顯示父類的一些屬性以及TypeDescriptor使用

來源:互聯網
上載者:User

標籤:

public SonClass:FatherClass

{

定義屬性

。。。。

}

Type thisType = typeof(SonClass);
方法一:
PropertyInfo[] pis = thisType.BaseType.GetProperties();//thisType.BaseType就是FatherClass
foreach (PropertyInfo p in pps)
{
properties.Remove(p.Name);
}
方法二:
PropertyInfo[] pis = thisType.GetProperties();
foreach (PropertyInfo p in pps)
{
if (p.DeclaringType != thisType)
properties.Remove(p.Name);
}
} 

方法三:

/// <summary>
/// 支援展現屬性的類型轉換器
/// </summary>
/// <remarks></remarks>
[System.Runtime.InteropServices.ComVisible(false)]
[System.Reflection.Obfuscation(Exclude = true, ApplyToMembers = true)]
public class TypeConverterSupportProperties : System.ComponentModel.TypeConverter
{
/// <summary>
/// 支援獲得屬性
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
/// <summary>
/// 獲得屬性
/// </summary>
/// <param name="context"></param>
/// <param name="value"></param>
/// <param name="attributes"></param>
/// <returns></returns>
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection ps = TypeDescriptor.GetProperties(value, attributes);
return ps;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return false;
}
return base.CanConvertTo(context, destinationType);
}
}

TypeDescriptor 類

提供有關組件屬性 (Attribute) 的資訊,如組件的屬性 (Attribute)、屬性 (Property) 和事件。無法繼承此類。

命名空間:System.ComponentModel
程式集:System(在 system.dll 中)

文法  C#
public sealed class TypeDescriptor
 備忘  .NET Framework 提供了兩種訪問某類型的中繼資料的方式:通過 System.Reflection 命名空間中提供的反射 API,以及通過 TypeDescriptor類。反射是可用於所有類型的通用機制,因為它是基於根 Object 類的 GetType 方法建立的。反射為某個類型返回的資訊不可擴充,因為編譯了目標類型後就不能對其進行修改。有關更多資訊,請參見 反射 中的主題。

相反,TypeDescriptor 是組件的可擴充檢查機制:即實現 IComponent 介面的那些類。與反射不同的是,它並不檢查方法。通過目標組件的Site 中提供的幾種服務,可以動態擴充 TypeDescriptor。下表顯示這些服務。

 

服務名

說明

IExtenderProvider

啟用其他類(如 ToolTip),以便為組件提供額外的屬性 (Property)。

ITypeDescriptorFilterService

啟用其他對象,以便修改由組件公開的標準中繼資料。

ICustomTypeDescriptor

啟用一個類,以便完全且動態地指定其自身的中繼資料,進而替換 TypeDescriptor 的標準檢查機制。

TypeDescriptor 提供的擴充性允許組件的設計時錶示形式不同於其實際的運行時表示形式,從而使 TypeDescriptor 對於產生設計時基礎結構十分有用。

TypeDescriptor 中的所有方法都是 static 的。不能建立此類的執行個體,也不能繼承此類。

可以通過兩種不同的方式設定屬性 (Property) 和事件值:在組件類中指定它們,或在設計時對它們變更。由於可以通過兩種方式設定這些值,因此,TypeDescriptor 的重載方法採用兩種不同類型的參數:類類型或對象執行個體。

當要訪問 TypeDescriptor 資訊並且您具有對象的執行個體時,應使用調用組件的方法。只有當您不具有對象的執行個體時,才能使用調用類類型的方法。

為了提高速度,將由 TypeDescriptor 緩衝屬性和事件。通常,它們在對象的生存期內保持不變。但是,擴充程式提供者和設計器可以更改對象的屬性集。如果是這種情況,則必須調用 Refresh 方法來更新緩衝。

C#如何在衍生類別中不顯示父類的一些屬性以及TypeDescriptor使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.