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);
- }
- }
方法四:直接把下面的代碼添加到需要的類里面即可(本代碼演示了ClassAAA 繼承了 Label, ICustomTypeDescriptor(接口),紫色的代碼即為要添加的)
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.ComponentModel.Design;
- using System.Windows.Forms;
- using System.Reflection;
- namespace WindowsFormsApplication2
- {
- public class ClassAAA : Label, ICustomTypeDescriptor
- {
- private bool me = false;
- public bool IsMe
- {
- get
- {
- return me;
- }
- set
- {
- this.me = value;
- }
- }
- public int Code
- {
- get
- {
- return this.GetHashCode();
- }
- }
- #region ICustomTypeDescriptor 顯式接口定義
- AttributeCollection ICustomTypeDescriptor.GetAttributes()
- {
- return TypeDescriptor.GetAttributes(this, true);
- }
- string ICustomTypeDescriptor.GetClassName()
- {
- return TypeDescriptor.GetClassName(this, true);
- }
- string ICustomTypeDescriptor.GetComponentName()
- {
- return TypeDescriptor.GetComponentName(this, true);
- }
- TypeConverter ICustomTypeDescriptor.GetConverter()
- {
- return TypeDescriptor.GetConverter(this, true);
- }
- EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
- {
- return TypeDescriptor.GetDefaultEvent(this, true);
- }
- PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
- {
- return null;
- }
- object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
- {
- return TypeDescriptor.GetEditor(this, editorBaseType, true);
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
- {
- return TypeDescriptor.GetEvents(this, true);
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
- {
- return TypeDescriptor.GetEvents(this, attributes, true);
- }
- PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
- {
- return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
- }
- PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
- {
- ArrayList props = new ArrayList();
- Type thisType = typeof(OneClass);
- PropertyInfo[] pis = thisType.GetProperties();
- foreach (PropertyInfo p in pis)
- {
- if (p.DeclaringType == thisType)
- {
- PropertyStub psd = new PropertyStub(p, attributes);
- props.Add(psd);
- }
- }
- PropertyDescriptor[] propArray =
- (PropertyDescriptor[])props.ToArray(typeof(PropertyDescriptor));
- return new PropertyDescriptorCollection(propArray);
- }
- object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
- {
- return this;
- }
- #endregion
- #region PropertyStub 定義
- private class PropertyStub : PropertyDescriptor
- {
- PropertyInfo info;
- public PropertyStub(PropertyInfo propertyInfo, Attribute[] attrs) :
- base(propertyInfo.Name, attrs)
- {
- this.info = propertyInfo;
- }
- public override Type ComponentType
- {
- get { return this.info.ReflectedType; }
- }
- public override bool IsReadOnly
- {
- get { return this.info.CanWrite == false; }
- }
- public override Type PropertyType
- {
- get { return this.info.PropertyType; }
- }
- public override bool CanResetValue(object component)
- {
- return false;
- }
- public override object GetValue(object component)
- {
- Console.WriteLine("GetValue: " + component.GetHashCode());
- return this.info.GetValue(component, null);
- }
- public override void ResetValue(object component)
- {
- }
- public override void SetValue(object component, object value)
- {
- Console.WriteLine("SetValue: " + component.GetHashCode());
- this.info.SetValue(component, value, null);
- }
- public override bool ShouldSerializeValue(object component)
- {
- return false;
- }
- }
- #endregion
- }
- }
TypeDescriptor 類
提供有關組件屬性 (Attribute) 的信息,如組件的屬性 (Attribute)、屬性 (Property) 和事件。無法繼承此類。
命名空間:System.ComponentModel
程序集:System(在 system.dll 中)
相反,TypeDescriptor 是組件的可擴展檢查機制:即實現 IComponent 接口的那些類。與反射不同的是,它並不檢查方法。通過目標組件的Site 中提供的幾種服務,可以動態擴展 TypeDescriptor。下表顯示這些服務。
服務名 |
說明 |
---|---|
啟用其他類(如 ToolTip),以便為組件提供額外的屬性 (Property)。 |
|
啟用其他對象,以便修改由組件公開的標准元數據。 |
|
啟用一個類,以便完全且動態地指定其自身的元數據,進而替換 TypeDescriptor 的標准檢查機制。 |
TypeDescriptor 提供的擴展性允許組件的設計時表示形式不同於其實際的運行時表示形式,從而使 TypeDescriptor 對於生成設計時基礎結構十分有用。
TypeDescriptor 中的所有方法都是 static 的。不能創建此類的實例,也不能繼承此類。
可以通過兩種不同的方式設置屬性 (Property) 和事件值:在組件類中指定它們,或在設計時對它們進行更改。由於可以通過兩種方式設置這些值,因此,TypeDescriptor 的重載方法采用兩種不同類型的參數:類類型或對象實例。
當要訪問 TypeDescriptor 信息並且您具有對象的實例時,應使用調用組件的方法。只有當您不具有對象的實例時,才能使用調用類類型的方法。
為了提高速度,將由 TypeDescriptor 緩存屬性和事件。通常,它們在對象的生存期內保持不變。但是,擴展程序提供程序和設計器可以更改對象的屬性集。如果是這種情況,則必須調用 Refresh 方法來更新緩存。