源文 : http://www.cnblogs.com/bicker/p/3326763.html

给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overflow看了不少文章,算是最终有了答案。

先是有这样的一段解释

Attributes are static metadata. Assemblies, modules, types, members, parameters, and return values aren't first-class objects in C# (e.g., the System.Type class is merely a reflected representation of a type). You can get an instance of an attribute for a type and change the properties if they're writable but that won't affect the attribute as it is applied to the type.

从这里看,想要实现动态添加Attribute就是不可能做到的。哈哈,被标题坑了吧。

好吧,但是我还是找到了一个东西System.ComponentModel.TypeDescriptor,这个号称可以添加Attribute到类、对象中去的类。TypeDescriptor里有方法:AddAttributes,可以把Attribute添加到类上。但是只有一个问题,添加上去的Attribute,只能通过TypeDescriptor来取到。

           /*
* 现在需要给simpleObject添加attribute
*/
TypeDescriptor.AddAttributes(typeof(targetObject), new simpleAttribute(new targetObject()));
AttributeCollection collection = TypeDescriptor.GetAttributes(typeof(targetObject));
simpleAttribute attr = ((simpleAttribute)collection[typeof(simpleAttribute)]);
if (attr != null)
{
MessageBox.Show(attr.ToString());
}

使用TypeDescriptor给类动态添加Attribute【转】的更多相关文章

  1. 使用TypeDescriptor给类动态添加Attribute

    给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overflow看了不少文章,算是最终有了答案. 先是有这样的一段解释 Attributes are stat ...

  2. python装饰器、继承、元类、mixin,四种給类动态添加类属性和方法的方式(一)

    介绍装饰器.继承.元类.mixin,四种給类动态添加类属性和方法的方式 有时候需要給类添加额外的东西,有些东西很频繁,每个类都需要,如果不想反复的复制粘贴到每个类,可以动态添加. # coding=u ...

  3. 细说python类2——类动态添加方法和slots(转)

    先说一下类添加属性方法和实例添加属性和方法的区别, 类添加属性属于加了一个以类为全局的属性(据说叫静态属性),那么以后类的每一个实例化,都具有这个属性.给类加一个方法也如此,以后类的每一个实例化都具备 ...

  4. 使用runtime给类动态添加方法并调用 - class_addMethod

    上手开发 iOS 一段时间后,我发现并不能只着眼于完成需求,利用闲暇之余多研究其他的开发技巧,才能在有限时间内提升自己水平.当然,“其他开发技巧”这个命题对于任何一个开发领域都感觉不找边际,而对于我来 ...

  5. C# 类动态添加属性、方法(Z)

      问题: 需要动态为WPF中的DataGrid添加列,并动态绑定相应数据.(此处仅实现动态属性的添加和使用,关于动态方法的添加和使用详见推荐阅读) 实现关键点: 目标类继承DynamicObject ...

  6. C# 类动态添加属性、方法

    问题: 需要动态为WPF中的DataGrid添加列,并动态绑定相应数据.(此处仅实现动态属性的添加和使用,关于动态方法的添加和使用详见推荐阅读) 实现关键点: 目标类继承DynamicObject,添 ...

  7. 给python类动态添加方法(method)

    群里有人问如何做到 def foo(): pass class Bar(object): pass Bar.set_instance_method(foo) b = Bar() b.foo() 这个其 ...

  8. php里面用魔术方法和匿名函数闭包函数动态的给类里面添加方法

    1.认识  __set  (在给不可访问属性赋值时,__set() 会被调用) 也就是说你再访问一个类里面没有的属性,会出发这个方法 class A{ private $aa = '11'; publ ...

  9. Python __slots__限制动态添加变量

    Python是一种非常灵活的动态语言,有时感觉太灵活以至于不知道遵循什么样的规则去驾驭.不过Python已经是非常完备的语言,想实现什么样的功能都是有方法的,而且也很容易,比如限制一个类动态添加成员变 ...

随机推荐

  1. clr(Windows 运行时和公共语言运行时)

    Windows 运行时   编译器使用 COM 引用计数机制来确定对象是否不再使用并可以删除. 因为从 Windows 运行时接口派生的对象实际上是 COM 对象,所以这是可行的. 在创建或复制对象时 ...

  2. luogu2455 [SDOI2006]线性方程组 高斯消元法

    #include <iostream> #include <cstdio> #include <cmath> using namespace std; int n, ...

  3. Selenium WebDriver- 操作JavaScript的Alert弹窗

    弹层和弹框是有区别的,弹框是那种完全没样式的框子:弹层是可以直接看到html的,有样式 #encoding=utf-8 import unittest import time from seleniu ...

  4. day03_06 变量详解

    print ("hello world") print("alex") print("jinxing") print("3乘以4= ...

  5. 让boostrap的图片轮播支持滑动效果

    因为最近开发的项目涉及到移动设备上的 HTML5 开发,其中需要实现轮播效果. 然后最快捷的方式,你知道的(Bootstrap),然后原生的 Bootstrap 的 carousel.js 插件并没有 ...

  6. CSS3中的border-radius兼容IE低版本解决方法

    ie-css3.htc先说道说道这斯是弄啥嘞ie-css3.htc是一个可以让IE浏览器支持部份CSS3属性的htc文件,不只是box-shadow,它还可以让你的IE浏览器支持圆角属性border- ...

  7. Python socket粘包问题(初级解决办法)

    server端配置: import socket,subprocess,struct from socket import * server=socket(AF_INET,SOCK_STREAM) s ...

  8. poj3255 Roadblocks 次短路

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10098   Accepted: 3620 Descr ...

  9. 【CTSC2010】产品销售(bzoj1920)

    数据结构优化网络流…… 重新定义一下题目的各种条件: 第 $i$ 天能生产 $a_i$ 个物品: 第 $i$ 天有 $b_i$ 个物品的需求: 每存储一天物品(把订单提前一天)需要 $c_i$ 的花费 ...

  10. py-faster-rcnn 的makefile.config 注意事项

    在配置py-faster-rcnn的过程中,我遇到一些问题,记录如下 py-faster-rcnn文件夹下面有一个caffe-fast-rcnn文件夹 这个过程中,我们需要编译caffe, rbgir ...