C#如何在派生类中不显示父类的一些属性以及TypeDescriptor使用
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 方法来更新缓存。
C#如何在派生类中不显示父类的一些属性以及TypeDescriptor使用的更多相关文章
- 基类中定义的虚函数在派生类中重新定义时,其函数原型,包括返回类型、函数名、参数个数、参数类型及参数的先后顺序,都必须与基类中的原型完全相同 but------> 可以返回派生类对象的引用或指针
您查询的关键词是:c++primer习题15.25 以下是该网页在北京时间 2016年07月15日 02:57:08 的快照: 如果打开速度慢,可以尝试快速版:如果想更新或删除快照,可以投诉快照. ...
- C++继承 派生类中的内存布局(单继承、多继承、虚拟继承)
今天在网上看到了一篇写得非常好的文章,是有关c++类继承内存布局的.看了之后获益良多,现在转在我自己的博客里面,作为以后复习之用. ——谈VC++对象模型(美)简.格雷程化 译 译者前言 一个C ...
- C++派生类中如何初始化基类对象(五段代码)
今天收到盛大的面试,问我一个问题,关于派生类中如何初始化基类对象,我在想派生类对于构造函数不都是先构造基类对象,然后在构造子类对象,但是如果我们在成员初始化列表先初始化派生类的私有成员,在函数内去调用 ...
- Abstract和Virtual和interface , 派生类中重写 override / new关键字
http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html C#中Abstract和Virtual 在C#的学习中,容易混淆virtua ...
- c++中基类与派生类中隐含的this指针的分析
先不要看结果,看一下你是否真正了解了this指针? #include<iostream> using namespace std; class Parent{ public: int x; ...
- C++:调整基类成员在派生类中的访问属性的其他方法(同名成员和访问声明)
4.3 调整基类成员在派生类中的访问属性的其他方法 4.3.1 同名函数 在定义派生类的时候,C++语言允许在派生类中说明的成员与基类中的成员名字相同,也就是 说,派生类可以重新说明与基类成员同名的成 ...
- 06 - 从Algorithm 算法派生类中删除ExecuteInformation() 和ExecuteData() VTK 6.0 迁移
在先前的vtk中,如vtkPointSetAlgorithm 等算法派生类中定义了虚方法:ExecuteInformation() 和 ExecuteData().这些方法的定义是为了平稳的从VTK4 ...
- 3.3 C++改变基类成员在派生类中的访问属性
参考:http://www.weixueyuan.net/view/6360.html 总结: 使用using声明可以改变基类成员在派生类中的访问属性. private: using book::se ...
- C++——派生类中的访问——可见性问题
C++中派生类对基类成员的访问形式主要有以下两种: 1.内部访问:由派生类中新增成员对基类继承来的成员的访问. 2.对象访问:在派生类外部,通过派生类的对象对从基类继承来的成员的访问.今天给大家介绍在 ...
随机推荐
- Kail安装Parallels tools
今天在安装虚拟机里面安装kail,在安装虚拟机提供的tools时候提示没有权限,如图: 后面经过自己证实首先tools是挂载的cdrom,在这样挂载下是没有写权限的(类似于windows下面读取光盘光 ...
- JavaScript jQuery 入门回顾 2
JQuery 滑动 利用jQuery可以在元素上创建滑动效果. slideDown() 向下滑动元素. slideUp() 向上滑动元素. slideToggle() 在 slideDown() 与 ...
- IOS 四种保存数据的方式
在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: 1.NSKeyed ...
- php入门变量之变量的间接引用、连接字符串和连接赋值运算符
[1]变量的间接引用: <?php $a = 'b'; $$a = '123'; echo $b; ?> 上面的输出结果是123 我们可以看到在第二行代码中多了一个$,并通过指定的名称访问 ...
- C语言创建一个窗口提示
打开Vs2012[我的是2012] /* X下面这些东西并没有什么用... 就不改了用2013 2015都一样 当然 devC++ 还有最原始的那个vc6.0也都是可以的. 编译环境遇到了相关问题网上 ...
- Python for 循环 失效
昨天发现一个负责处理观察者模式的基类工作失败,默认的N个观察者负责处理 发送的一些东西, 其中提供一个内置接口移除观察者: def removeObserver(self, observer): if ...
- 一道简单的IOS面试题-b
题目: (参考:陈曦 包子的iOS开发)我在code review的时候,发现了某个viewController中有这样一段代码,觉得很不妥当,请尝试找出代码中的任何问题,或者可以优化的部分. -(i ...
- phpd读取txt文件(自动解析换行)
<form id="form" method="post" action="whois.php"> <?php $newf ...
- 团体程序设计天梯赛-练习集L1-023. 输出GPLT
L1-023. 输出GPLT 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个长度不超过10000的.仅由英文字母构成的 ...
- jsp request.getParameterValues获取数组值代码示例
tt.jsp <form action="tt2.jsp" method="POST"> <select name="two&quo ...