标签 Attribute
在一个类上面放一个标签,可以用来表示一些特定规则,比如某个对象的某个属性不想被json化,那么我们在它头上放个标签就行了,或是做ORM时指定某个Class对应的table名字等。
最后标签是通过反射来调用的,一个类只要继承了微软的Attribute类就可以当标签来使用了.
[AttributeUsage(AttributeTargets.All)]//指定Attribute的使用范围,比如只能在class级别使用
public class Column : Attribute
{
public Column(string str)
{
this.str = str;
}
public string str { get; set; }
}
//第2个
[AttributeUsage(AttributeTargets.All)]
public class Table : Attribute
{
public string someData { get; set; } //公开属性还可以直接在标签上面写入
public Table(string str)
{
this.str = str;
}
public string str { get; set; }
} //2个属性
[Column("class")]
[Table("table class", someData = "ggc" )] //someData="ggc"是直接对attribute对象写入值!
public class Abc
{
public string x { get; set; }
[Column("attr")]
public Int32 y { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
//通过反射可以调用到这个类型头上的attr
Type type = typeof(Abc);
bool isGot = Attribute.IsDefined(type, typeof(Column)); //查看有没有某个Attribute
Attribute[] attrs = Attribute.GetCustomAttributes(type); //批量获取
string xy = attrs.First().GetType().Name; //可以查看它的名字来选择想要的,或是看有没有想要的.
Column column = (Column)Attribute.GetCustomAttribute(type, typeof(Column)); //强转
Column[] columns = (Column[])Attribute.GetCustomAttributes(type, typeof(Column));
column = columns[];
string x = column.str; //调用方法或是公开属性等等都可以 //实例化之后也是可以一样拿
Abc a = new Abc();
PropertyInfo info = a.GetType().GetProperty("y");
Column attribute2 = (Column)Attribute.GetCustomAttribute(info, typeof(Column));
string xyz = attribute2.str;
}
标签 Attribute的更多相关文章
- 有关C#标签Attribute的熟悉
Attribute 简单用法: 最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAt ...
- DOM对象属性(property)与HTML标签特性(attribute)
HTML中property与attribute是极易混淆的两个概念.大多数时候这两个单词都翻译为"属性",为了区分二者,一般将property翻译为"属性",a ...
- 转 使用IParameterInspector, IOperationBehavior,Attribute(参数检查器、操作行为接口和标签)扩展WCF操作行为
public class EntryIdInspector : IParameterInspector { public int intParamIndex { get; set; } string ...
- .Net Attribute详解(上)-Attribute本质以及一个简单示例
Attribute的直接翻译是属性,这和Property容易产生混淆,所以一般翻译成特性加以区分.Attribute常常的表现形式就是[AttributeName], 随意地添加在class, met ...
- .Net Attribute详解(下) - 使用Attribute武装枚举类型
接上文.Net Attribute详解(上)-Attribute本质以及一个简单示例,这篇文章介绍一个非常实用的例子,相信你一定能够用到你正在开发的项目中.枚举类型被常常用到项目中,如果要使用枚举To ...
- JavaEE自定义标签:标签类的创建、tld配置文件的创建(位置、如何创建)、Web-XML配置、JSP应用
1.标签 以类似于html标签的方式实现的java代码的封装. 第一:形成了开发标签的技术标准---自定义标签的技术标准. 第二:java标准标签库(sun之前自己开发的一系列的标签的集合)jstl, ...
- Java自定义简单标签
Java自定义简单标签可以方便的在页面输出信息,并且对于权限的控制,和对于Jsp标签和servlet代码的分离有着很好的作用. 下面将以权限的控制为例自定义一个标签: 一.标签类型 <wxt: ...
- .Net Attribute详解(一)
.Net Attribute详解(一) 2013-11-27 08:10 by JustRun, 1427 阅读, 14 评论, 收藏, 编辑 Attribute的直接翻译是属性,这和Property ...
- .NET Core CSharp 中级篇2-8 特性标签
.NET Core CSharp 中级篇2-8 本节内容为特性标签 简介 标签Attribute是一个非常重要的技术,你可以使用Attribute技术优化精简你的代码.特性标签可以运用在程序集,模块, ...
随机推荐
- 路由器的nat模式、路由模式和全模式
NAT模式.此模式下,由局域网向广域网发送的数据包默认经过NAT转换,但路由器对所有源地址与局域网接口不在同一网段的数据包均不进行处理.例如,路由器LAN口IP设置为192.168.1.1,子网掩码为 ...
- Linux操作系统Centos7.2版本搭建Apache+PHP+Mysql环境
对于在校大学生来说腾讯云1元主机很划算,所以就申请了一台,打算在上面学习下linux,使用版本为centos7.2版本.在服务器上比较推荐centos,此版本生命周期较长,而且网上有关centos的教 ...
- 705 - Slash Maze
By filling a rectangle with slashes (/) and backslashes ( ), you can generate nice little mazes. Her ...
- CI框架源代码阅读笔记5 基准測试 BenchMark.php
上一篇博客(CI框架源代码阅读笔记4 引导文件CodeIgniter.php)中.我们已经看到:CI中核心流程的核心功能都是由不同的组件来完毕的.这些组件类似于一个一个单独的模块,不同的模块完毕不同的 ...
- JAVA中JNI的简单使用
了解JNI:JAVA因其跨平台特性而受人们喜爱,也正因此,使得它和本机各种内部联系变得很少,所以JNI(Java Native Interface)就是用来解决JAVA本地操作的一种方式.JAVA通过 ...
- Android NumberPicker默认外观的修改
NumberPicker是Android3.0之后引入的一个控件,在以后的TimePicker和DatePicker时间控件里边都有引用,NumberPicker本身并没有提供接口给开发者修改其默认外 ...
- Tomcat: IllegalStateException: No output folder --reference
Today, I started to create a couple of JSP pages for the server-side part of my MSc thesis project i ...
- Chapter 4. Using the Gradle Command-Line 使用gradle命令行
This chapter introduces the basics of the Gradle command-line. You run a build using the gradle comm ...
- 在top命令下kill和renice进程
For common process management tasks, top is so great because it gives an overview of the most active ...
- i利用图片按钮 和 input type="image" 为背景提交表单
<img src="img/cancel.jpg" onclick="javascript:document.getElementByIdx_x('loginFor ...