public SonClass:FatherClass

{

定义属性

。。。。

}

Type thisType = typeof(SonClass);
方法一:

  1. PropertyInfo[] pis = thisType.BaseType.GetProperties();//thisType.BaseType就是FatherClass
  2. foreach (PropertyInfo p in pps)
  3. {
  4. properties.Remove(p.Name);
  5. }

方法二:

  1. PropertyInfo[] pis = thisType.GetProperties();
  2. foreach (PropertyInfo p in pps)
  3. {
  4. if (p.DeclaringType != thisType)
  5. properties.Remove(p.Name);
  6. }
  7. }

方法三:

  1. /// <summary>
  2. /// 支持展现属性的类型转换器
  3. /// </summary>
  4. /// <remarks></remarks>
  5. [System.Runtime.InteropServices.ComVisible(false)]
  6. [System.Reflection.Obfuscation(Exclude = true, ApplyToMembers = true)]
  7. public class TypeConverterSupportProperties : System.ComponentModel.TypeConverter
  8. {
  9. /// <summary>
  10. /// 支持获得属性
  11. /// </summary>
  12. /// <param name="context"></param>
  13. /// <returns></returns>
  14. public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  15. {
  16. return true;
  17. }
  18. /// <summary>
  19. /// 获得属性
  20. /// </summary>
  21. /// <param name="context"></param>
  22. /// <param name="value"></param>
  23. /// <param name="attributes"></param>
  24. /// <returns></returns>
  25. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
  26. {
  27. PropertyDescriptorCollection ps = TypeDescriptor.GetProperties(value, attributes);
  28. return ps;
  29. }
  30. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  31. {
  32. if (destinationType == typeof(string))
  33. {
  34. return false;
  35. }
  36. return base.CanConvertTo(context, destinationType);
  37. }
  38. }

方法四:直接把下面的代码添加到需要的类里面即可(本代码演示了ClassAAA 继承了 Label, ICustomTypeDescriptor(接口),紫色的代码即为要添加的)

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.ComponentModel.Design;
  5. using System.Windows.Forms;
  6. using System.Reflection;
  7. namespace WindowsFormsApplication2
  8. {
  9. public class ClassAAA : Label, ICustomTypeDescriptor
  10. {
  11. private bool me = false;
  12. public bool IsMe
  13. {
  14. get
  15. {
  16. return me;
  17. }
  18. set
  19. {
  20. this.me = value;
  21. }
  22. }
  23. public int Code
  24. {
  25. get
  26. {
  27. return this.GetHashCode();
  28. }
  29. }
  30. #region ICustomTypeDescriptor 显式接口定义
  31. AttributeCollection ICustomTypeDescriptor.GetAttributes()
  32. {
  33. return TypeDescriptor.GetAttributes(this, true);
  34. }
  35. string ICustomTypeDescriptor.GetClassName()
  36. {
  37. return TypeDescriptor.GetClassName(this, true);
  38. }
  39. string ICustomTypeDescriptor.GetComponentName()
  40. {
  41. return TypeDescriptor.GetComponentName(this, true);
  42. }
  43. TypeConverter ICustomTypeDescriptor.GetConverter()
  44. {
  45. return TypeDescriptor.GetConverter(this, true);
  46. }
  47. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
  48. {
  49. return TypeDescriptor.GetDefaultEvent(this, true);
  50. }
  51. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
  52. {
  53. return null;
  54. }
  55. object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
  56. {
  57. return TypeDescriptor.GetEditor(this, editorBaseType, true);
  58. }
  59. EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
  60. {
  61. return TypeDescriptor.GetEvents(this, true);
  62. }
  63. EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
  64. {
  65. return TypeDescriptor.GetEvents(this, attributes, true);
  66. }
  67. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
  68. {
  69. return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
  70. }
  71. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
  72. {
  73. ArrayList props = new ArrayList();
  74. Type thisType = typeof(OneClass);
  75. PropertyInfo[] pis = thisType.GetProperties();
  76. foreach (PropertyInfo p in pis)
  77. {
  78. if (p.DeclaringType == thisType)
  79. {
  80. PropertyStub psd = new PropertyStub(p, attributes);
  81. props.Add(psd);
  82. }
  83. }
  84. PropertyDescriptor[] propArray =
  85. (PropertyDescriptor[])props.ToArray(typeof(PropertyDescriptor));
  86. return new PropertyDescriptorCollection(propArray);
  87. }
  88. object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
  89. {
  90. return this;
  91. }
  92. #endregion
  93. #region PropertyStub 定义
  94. private class PropertyStub : PropertyDescriptor
  95. {
  96. PropertyInfo info;
  97. public PropertyStub(PropertyInfo propertyInfo, Attribute[] attrs) :
  98. base(propertyInfo.Name, attrs)
  99. {
  100. this.info = propertyInfo;
  101. }
  102. public override Type ComponentType
  103. {
  104. get { return this.info.ReflectedType; }
  105. }
  106. public override bool IsReadOnly
  107. {
  108. get { return this.info.CanWrite == false; }
  109. }
  110. public override Type PropertyType
  111. {
  112. get { return this.info.PropertyType; }
  113. }
  114. public override bool CanResetValue(object component)
  115. {
  116. return false;
  117. }
  118. public override object GetValue(object component)
  119. {
  120. Console.WriteLine("GetValue: " + component.GetHashCode());
  121. return this.info.GetValue(component, null);
  122. }
  123. public override void ResetValue(object component)
  124. {
  125. }
  126. public override void SetValue(object component, object value)
  127. {
  128. Console.WriteLine("SetValue: " + component.GetHashCode());
  129. this.info.SetValue(component, value, null);
  130. }
  131. public override bool ShouldSerializeValue(object component)
  132. {
  133. return false;
  134. }
  135. }
  136. #endregion
  137. }
  138. }

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使用的更多相关文章

  1. 基类中定义的虚函数在派生类中重新定义时,其函数原型,包括返回类型、函数名、参数个数、参数类型及参数的先后顺序,都必须与基类中的原型完全相同 but------> 可以返回派生类对象的引用或指针

      您查询的关键词是:c++primer习题15.25 以下是该网页在北京时间 2016年07月15日 02:57:08 的快照: 如果打开速度慢,可以尝试快速版:如果想更新或删除快照,可以投诉快照. ...

  2. C++继承 派生类中的内存布局(单继承、多继承、虚拟继承)

    今天在网上看到了一篇写得非常好的文章,是有关c++类继承内存布局的.看了之后获益良多,现在转在我自己的博客里面,作为以后复习之用. ——谈VC++对象模型(美)简.格雷程化    译 译者前言 一个C ...

  3. C++派生类中如何初始化基类对象(五段代码)

    今天收到盛大的面试,问我一个问题,关于派生类中如何初始化基类对象,我在想派生类对于构造函数不都是先构造基类对象,然后在构造子类对象,但是如果我们在成员初始化列表先初始化派生类的私有成员,在函数内去调用 ...

  4. Abstract和Virtual和interface , 派生类中重写 override / new关键字

    http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html C#中Abstract和Virtual 在C#的学习中,容易混淆virtua ...

  5. c++中基类与派生类中隐含的this指针的分析

    先不要看结果,看一下你是否真正了解了this指针? #include<iostream> using namespace std; class Parent{ public: int x; ...

  6. C++:调整基类成员在派生类中的访问属性的其他方法(同名成员和访问声明)

    4.3 调整基类成员在派生类中的访问属性的其他方法 4.3.1 同名函数 在定义派生类的时候,C++语言允许在派生类中说明的成员与基类中的成员名字相同,也就是 说,派生类可以重新说明与基类成员同名的成 ...

  7. 06 - 从Algorithm 算法派生类中删除ExecuteInformation() 和ExecuteData() VTK 6.0 迁移

    在先前的vtk中,如vtkPointSetAlgorithm 等算法派生类中定义了虚方法:ExecuteInformation() 和 ExecuteData().这些方法的定义是为了平稳的从VTK4 ...

  8. 3.3 C++改变基类成员在派生类中的访问属性

    参考:http://www.weixueyuan.net/view/6360.html 总结: 使用using声明可以改变基类成员在派生类中的访问属性. private: using book::se ...

  9. C++——派生类中的访问——可见性问题

    C++中派生类对基类成员的访问形式主要有以下两种: 1.内部访问:由派生类中新增成员对基类继承来的成员的访问. 2.对象访问:在派生类外部,通过派生类的对象对从基类继承来的成员的访问.今天给大家介绍在 ...

随机推荐

  1. Linux 下添加普通用户,登陆并删除

    adduser 命令.LINUX创建用户的命令useradd -g test -d /home/test1 -s /etc/bash -m test1注解:-g 所属组 -d 家目录 -s 所用的SH ...

  2. selector的理解

    对于nio这块最近几年一直就有关注,知道非阻塞,线程池,缓冲池,io的模式select,poll,epoll,甚至epoll中的et,lt. 但是最近才有时间实际看了看netty的源码,才发现原来se ...

  3. C# 实现HTML5服务器推送事件

    为什么需要服务器推送事件: 因为如果需要保持前台数据的实时更新例如,IM聊天,股票信息, 1.可以在客户端不断地调用服务端的方法来获得新数据,但是这样会很消耗服务器资源,导致系统变慢! 2 html5 ...

  4. winfrom 水晶按钮

    闲来无事,从网上找了不少自定义控件,然后整理了一下,做了一个水晶按钮 /// <summary> /// 表示 Windows 的按钮控 /// </summary> [Des ...

  5. 使用VS时点右键卡住—不响应的问题

    以前因为论坛,后来因为工作,发现已经好久没有来百度空间了.也好久没人留言或发表评论了,今天自己更新一下吧. 关于使用VS时点右键卡住或不响应的问题,我是在VS 2008中遇到的,不知道其它的版本有没有 ...

  6. aliexpress 上传图

    首先,图片转化为字节流 public byte[] ImagefileToByte(string srcImagePath) { System.IO.MemoryStream m = new Syst ...

  7. Kakfa揭秘 Day3 Kafka源码概述

    Kakfa揭秘 Day3 Kafka源码概述 今天开始进入Kafka的源码,本次学习基于最新的0.10.0版本进行.由于之前在学习Spark过程中积累了很多的经验和思想,这些在kafka上是通用的. ...

  8. [Git]代码管理工具简单使用

    1 Git简介 Git是分布式的版本控制系统,是Linux内核开发者林纳斯·托瓦兹(Linus Torvalds)为更好地管理Linux内核开发而设计.与CVS.Subversion一类的集中式版本控 ...

  9. 转几篇WPF文章

    How to view word document in WPF application (CSVSTOViewWordInWPF) WPF 浏览PDF 文件 如何保存RichTextBox的文本到数 ...

  10. Node.js的process模块

    process模块用来与当前进程互动,可以通过全局变量process访问,不必使用require命令加载.它是一个EventEmitter对象的实例. 属性 process对象提供一系列属性,用于返回 ...