obj.GetType().GetCustomAttributes
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的更多相关文章
- Enum.GetUnderlyingType(obj.GetType())
Enum.GetUnderlyingType(obj.GetType())获取保存枚举值的数据类型:
- MEF核心笔记(6)让 MEF 拥抱 AOP
场景: 最近推荐同事在项目中使用起了 MEF,用其构建一个插件式的多人开发框架,因为该框架不是让我去设计了,所以对于 MEF 和 IOC 等概念不是很了解的同事,便会出现各种问题.接入 AOP 便是其 ...
- XML文件的读取、序列化和反序列化操作
public class XmlHelper { //从xml中获取MsgType public static string XMLSelect(string XML) { XmlDocument x ...
- Unity在WPF中的应用
1. 本文的实现类继承于IRepository using System; using System.Linq; using System.Linq.Expressions; using Zhang. ...
- C# GetType与typeof
在反射和泛型中经常会使用到Type类,获取Type的最常用的方法是 obj.GetType(),和typeof(T).在获取泛型的type时有些小坑. public static void Main( ...
- WCF XmlSerializer GetType 反射类型 报错 解决方案
为图省事,用现有的EF,和web一起使用一个Model类 当进行到 XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); 会报 ...
- default(T) 和 typeof 和 GetType()
一.default(T) 在泛型编成中如果不限制T类型参数是值类型或引用类型的话 你程序内部可能会出现错误,因为值类型不允许NULL.所以default用来获取一个类型的默认值,对于值类型得到new ...
- C# 反射(GetType) 获取动态Json对象属性值的方法
之前在开发一个程序,希望能够通过属性名称读取出属性值,但是由于那时候不熟悉反射,所以并没有找到合适的方法,做了不少的重复性工作啊! 然后今天我再上网找了找,被我找到了,跟大家分享一下. 其实原理并不复 ...
- C#/ASP.NET完善的DBHelper,配套Model生成器
支持Oracle.MSSQL.MySQL.SQLite四种数据库,支持事务,支持对象关系映射:已在多个项目中实际使用. 没有语法糖,学习成本几乎为0,拿来即用. DBHelper类完整代码: usin ...
随机推荐
- python列表各种切片姿势
顺着切,反着切,想怎么切就怎么切,但是别被坑. mylist = [1,2,3,4,5,6,7,8,9] print(mylist[2:7:2]) # [3, 5, 7] print(mylist[: ...
- ios Aspects面向切面沉思录—面向结构编程—面向修改记录编程—面向运行时结构编程—元编程?
1.将主功能看成一个巨大的结构: 2.将切面注入的交叉业务看成是一组结构修改的注册:目标对象+方法是修改的键值: 3.Aspects引擎是修改的执行者.记录者.和维护者: 4.函数和方法是它操作和面对 ...
- What is react-native link?
What is react-native link? or Should you just use react-native link when linking any dependency or s ...
- LeetCode 934. Shortest Bridge
原题链接在这里:https://leetcode.com/problems/shortest-bridge/ 题目: In a given 2D binary array A, there are t ...
- Hibernate中对象的三种状态即save(),update(),saveOrUpdate()的使用【转】
当new一个user的时候,是处于瞬时状态 当调用session.save(user)的时候,user对象会放到session里,此时处于持久状态 当提交事务的时候,user对象才会存到DB中 当调用 ...
- MongoDB的安装、基本操作
此说明文档针对的community版本是v4.2.0(1)下载下载官网,此时的community版本是v4.2.0https://www.mongodb.com/download-center/com ...
- Nginx 优化配置
基本配置优化(优化后配置样例,可以改后直接上生产) 1 #头部配置 2 user nginx nginx; #定义nginx的启动用户,不建议使用root 3 worker_processes 4; ...
- 插入排序;至少要比较N(N-1)/2次;N表示元素个数
<script type="text/javascript"> //冒泡排序:至少要比较N(N-1)/2次:N表示元素个数 function get(){ var nu ...
- 洛谷 p1968 美元汇率 题解
传送门 美元由马克转化,马克由美元转化 求最大美元 每一天只有2种选择 ①:不转化另一货币 ②:转化另一货币 典型01背包 可以开一个二维数组f[100][3] F[i][1]表示前i天获得最大美元 ...
- 洛谷 P1621 集合
目录 题目 思路 \(Code\) 题目 P1621 集合 思路 并查集+埃氏筛,一开始连通块的个数是\(b-a+1\)个,在筛素数的过程中只要当前素数大于\(p\)就对该素数筛出来的数进行判断,如果 ...