using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.Windows.Forms; using System.Threading; using System.Collections.Specialized; namespace Test123
{
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } public class TestAttri : Attribute
{
public string JJ { get; set; }
} [TestAttri(JJ = "")]
public class ClassTest2 : ClassTest { } public abstract class ClassTest { } private void button3_Click(object sender, EventArgs e)
{
ClassTest obj = new ClassTest2();
Type type = obj.GetType();
TestAttri att = GetCustomAttribute<TestAttri>(type, false);
//(TestAttri)type.GetCustomAttributes(typeof(TestAttri), false).GetValue(0);
//或者 (TestAttri)obj.GetType().GetCustomAttributes(typeof(TestAttri), false)[0];
att.JJ = "rich"; //需要特别注意的地方:
//GetCustomAttributes 方法每次都返回新的实例
//每次获取 io 的 CustomAttributes 都是一个新的副本 MessageBox.Show(att.JJ); ((TestAttri)obj.GetType().GetCustomAttributes(typeof(TestAttri), false)[]).JJ = "jigjig";
MessageBox.Show(((TestAttri)obj.GetType().GetCustomAttributes(typeof(TestAttri), false)[]).JJ);
} private static Dictionary<MemberInfo, Object> _dicCache1 = new Dictionary<MemberInfo, Object>();
private static Dictionary<MemberInfo, Object> _dicCache2 = new Dictionary<MemberInfo, Object>(); public static TAttribute[] GetCustomAttributes<TAttribute>(MemberInfo member, Boolean inherit)
{
if (member == null)
{
return new TAttribute[];
} // 根据是否可继承,分属两个缓存集合
var cache = inherit ? _dicCache1 : _dicCache2; Object obj = null;
if (cache.TryGetValue(member, out obj))
{
return (TAttribute[])obj;
}
lock (cache)
{
if (cache.TryGetValue(member, out obj))
{
return (TAttribute[])obj;
}
var atts = member.GetCustomAttributes(typeof(TAttribute), inherit) as TAttribute[];
var att = atts == null ? new TAttribute[] : atts;
cache[member] = att;
return att;
}
} public static TAttribute GetCustomAttribute<TAttribute>(MemberInfo member, Boolean inherit)
{
var atts = member.GetCustomAttributes<TAttribute>(inherit);
if (atts == null || atts.Length < )
{
return default(TAttribute);
}
return atts[];
} } }

obj.GetType().GetCustomAttributes的更多相关文章

  1. Enum.GetUnderlyingType(obj.GetType())

    Enum.GetUnderlyingType(obj.GetType())获取保存枚举值的数据类型:

  2. MEF核心笔记(6)让 MEF 拥抱 AOP

    场景: 最近推荐同事在项目中使用起了 MEF,用其构建一个插件式的多人开发框架,因为该框架不是让我去设计了,所以对于 MEF 和 IOC 等概念不是很了解的同事,便会出现各种问题.接入 AOP 便是其 ...

  3. XML文件的读取、序列化和反序列化操作

    public class XmlHelper { //从xml中获取MsgType public static string XMLSelect(string XML) { XmlDocument x ...

  4. Unity在WPF中的应用

    1. 本文的实现类继承于IRepository using System; using System.Linq; using System.Linq.Expressions; using Zhang. ...

  5. C# GetType与typeof

    在反射和泛型中经常会使用到Type类,获取Type的最常用的方法是 obj.GetType(),和typeof(T).在获取泛型的type时有些小坑. public static void Main( ...

  6. WCF XmlSerializer GetType 反射类型 报错 解决方案

    为图省事,用现有的EF,和web一起使用一个Model类 当进行到 XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); 会报 ...

  7. default(T) 和 typeof 和 GetType()

    一.default(T) 在泛型编成中如果不限制T类型参数是值类型或引用类型的话 你程序内部可能会出现错误,因为值类型不允许NULL.所以default用来获取一个类型的默认值,对于值类型得到new ...

  8. C# 反射(GetType) 获取动态Json对象属性值的方法

    之前在开发一个程序,希望能够通过属性名称读取出属性值,但是由于那时候不熟悉反射,所以并没有找到合适的方法,做了不少的重复性工作啊! 然后今天我再上网找了找,被我找到了,跟大家分享一下. 其实原理并不复 ...

  9. C#/ASP.NET完善的DBHelper,配套Model生成器

    支持Oracle.MSSQL.MySQL.SQLite四种数据库,支持事务,支持对象关系映射:已在多个项目中实际使用. 没有语法糖,学习成本几乎为0,拿来即用. DBHelper类完整代码: usin ...

随机推荐

  1. 最小球覆盖——模拟退火&&三分套三分套三分

    题目 给出 $N(1 \leq N \leq 100)$ 个点的坐标 $x_i,y_i,z_i$($-100000 \leq x_i,y_i,z_i \leq 100000$),求包围全部点的最小的球 ...

  2. 第5章 Spring的事务管理

    5.1 Spring事务管理概述 5.11 事务管理的核心接口 在Spring的所有jar包中,有一个名spring-tx-4.3.6RELEAS的jar包,是提供事务管理的依赖包.在该包的org.s ...

  3. FitNesseRoot/ErrorLogs目录下可查看fitnesse输出日志

    调试fitnesse用例时,通过测试页面的输出信息不是很好定位问题出在哪里 这时可以在写代码过程中,增加一些输出信息,比如说java的话,可以用log4j.注意要把日志输出弄成utf-8编码,不然会中 ...

  4. MongoDB db.stats()&&db.db.serverStatus()

    db.stats()   示例图 参数解释: "db" : "test" ,表示当前是针对"test"这个数据库的描述.想要查看其他数据库, ...

  5. 05_配置交换机SSH服务(数通华为)

    1. 网络拓扑: 2. SW1配置:2.1 配置为Access口,vlan 10:[SW1]vlan 10[SW1-GigabitEthernet0/0/1]port link-type access ...

  6. Numpy | 08 切片和索引

    ndarray对象的内容可以通过索引或切片来访问和修改,与 Python 中 list 的切片操作一样. (1)ndarray 数组索引可以基于 0 - n 的下标进行: (2)切片对象可以通过内置的 ...

  7. smashing 三方widgets 使用

    smashing 有一套自己的约定,包括widgets 以及dashboard,还有就是关于数据的处理 约定如下 三方widgets 统一在widgets 目录下,一般包含了基于coffee 的js ...

  8. IIS服务器简单搭建

     概况 系统:WIN10企业版 开发工具:VS2013 - VS2017 IIS版本:IIS6.0 安装 安装步骤如图: 这里需要注意一点信息服务器下默认web管理工具只有iis控制台勾上了,也就意味 ...

  9. 《Java虚拟机JVM故障诊断与性能优化》读书笔记(未完待续)

    前言: 对于JVM学习用处的理解:我们程序员写的代码,虽说是放在服务器(linux)系统上的.但是很多时候,受JVM的影响,其实程序并没有发挥出服务器的最大性能.这时候,JVM就成为了瓶颈了.有瓶颈就 ...

  10. JVM参数笔记

    Java启动参数共分为三类: 其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容: 其二是非标准参数(-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现都满足, ...