06-自定义Attribute标记案例
自定义Attribute:
1)Attribute都从System. Attribute类继承,类名一般以Attribute结尾
2) 标记类的用途—AttributeUsage标记(标记的标记):AttributeTargets:可以添加到哪些成员上。 AllowMultiple:是否允许在一个成员上标记这个Attribute的多个实例,默认false。标注在Attribute类的类名上方。
3)Attribute只是给类、成员做标记,本身没有什么意义,不会发生任何动作,就像小偷踩点在门上做标记(StealableAttribute)一样,具体这个标记会起到什么作用是由使用这个类、成员的代码决定的。
案例代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary; namespace _11动态创建一个对象的html表单页
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Person p1 = new Person();
p1.Name = "hzy";
p1.Age = ;
p1.Email = "hzy@sina.com";
// Car car = new Car() { Brand = "兰博基尼", LicenseNo = "京00000" };
CreateFormByObject(p1);
} private void CreateFormByObject(object obj)
{
Type type = obj.GetType();//获取这个对象的type
StringBuilder sbHtml = new StringBuilder();
PropertyInfo[] pinfo = type.GetProperties();//获取当前对象中的所有的属性
foreach (PropertyInfo item in pinfo)
{
//这句话的意思是,获取当前遍历的属性Brand上所标记的UserLanguageAttribute特性
object[] objs = item.GetCustomAttributes(typeof(UserLanguageAttribute), false); //获取所有标记的特性,不包含父类中继承下来的特性
//item.GetCustomAttributes(false)
//objs[0].GetType().Name
//表示当前属性标记了这个特性(UserLanguageAttribute)
if (objs.Length > )
{
UserLanguageAttribute ula = (UserLanguageAttribute)objs[];
sbHtml.Append(ula.DisplayLanguage + ":<input value=\"" + item.GetValue(obj, null) + "\"/><br/>");
}
else
{
sbHtml.Append(item.Name + ":<input value=\"" + item.GetValue(obj, null) + "\"/><br/>");
}
}
webBrowser1.DocumentText = sbHtml.ToString();
}
} public class Person
{
[UserLanguage("xing ming")]
public string Name
{
get;
set;
}
[UserLanguage("nian ling")]
public int Age
{
get;
set; }
//[UserLanguage("yi mei er")]
public string Email
{
get;
set;
}
} public class Car
{
[UserLanguage("品牌")]
public string Brand
{
get;
set;
}
[UserLanguage("车牌号码")]
public string LicenseNo
{
get;
set;
}
} public class UserLanguageAttribute : Attribute
{
public UserLanguageAttribute(string lan)
{
this.DisplayLanguage = lan;
}
public string DisplayLanguage
{
get;
set;
}
}
}
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbEAAADACAIAAAAFu8o2AAAUOUlEQVR4nO2dfWwc5Z3Hf2tLcZo7VK700hBVOqrS5nr9K9z1XkgQV/EmEayCQoRJAk0QJiGkydVYSM0dnIQV9SKa8CIiqAyC9uBUaEOELi2wUDRJr8ARSEwwIc0lxE7s2N71y/o1iWN7748ZzzP7zOzsi3d3dnY/H320mp155pm3nW+emYnnkQQAAMwiQa8AAEAZ4ZGJsuLjyC1tkVva5KbP5abP5eZjlrd2yMpOWd0ld/eYn5H1Mbm7T1b3yNo+ubtP1s7aEEfM0UHE3Lyztyg609CMwnkbxuqapi/bllz8aHLxo8lvbk8u2Zlc+rPk0qeTVz+ZvPrJ5DW7k9fsTl77TFrNAojZ6/NzQvS0SD9FcQZizV198zaM1WwermueuWxb0o7Fb+xIfmNHcsnO5HefUC592tI5EhEx1FqZKN//UG49LSuPRxrjkca4bBgwrdk8XLtlvHbrZF3zjDw4XduUrG1KmsN1zTP2SHPAHDnvxzN1TVZJRMQiGdk8UwwdmbjiY1l9Snl3j9zTZ+ZjzeZh09ot4+ms2Txcu3FM2TiMiBg6JZFIyFX75eYTsuJjWXnc0hGLdjJa3j+itEduGIg0xmXdoOXafsQc7UMsC61MvGq/fP/DyC1t6TLRVsvBSGNc7umzHkCbj55X98jqHmk4i4gYPhOJhFz5X16Z2OWZiXYOOqKwR+7usaLQTsNV3Yg5ePsZxNxc2VkUE4mELPq1XLVfrvmo5gY9EyPrY1b22cm4PqZGOhuGzrZh4DsLESveEmSi3PS5HYhG1nhc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9Y6MEh9dbZvj32pWeSY7IpE78BMyUTPPgX1DrLSXMt7pZdnoqXM7rX0zMsKH2QihIW8MtH9gEW7GLSuKFM7aE7f+7PeB0symUx3PzE1I9w55gyx1GZd6px2WukBlHpN63H57R5O+7DJ+z5DSlB6BXb67Q0vZCKEhcK1E9P1ReX1UEYliGe/VMlkMptM9LqOnu3KyvEebmuWDNfV3hvpn4kpmzb3diKZCBA8c8xEx/1Ev0zMovfnXDMx3ZNkMxSjjq4JZpeVehvQHuV1leqOwrTr4Hzkk/F+olkg3f1EMhEgeOaaiclk0grGjJno3/tzbpmoX8Gn5FF7a+rjWnth6tG19hBblfN97uzKKectg3bXHUq90ozPnclEgMCZeyY674hlzMT0vT/7ZGKuuHMkfaMy3RU4FBYyEcJCTpmYri9m678GWl+ijpZR1NkgcrSO9N6fHc296Fy7bPZsWlnL1sbrLUooFmQihIWc24klI5+zyDv59MmeF9xQVMhECAuVlYlQrnA0ISyQiVAKOJoQFso6E7GSDPoHBZAV5ZuJAAClh0wEAFCQiQAACjIRAEBhvz/xGJkIAEAmAgAoyEQAAAWZCACgIBMBABRkIgCAgkwEAFCQiQAACjIRAEBBJgIAKMhEAAAFmQgAoPh/TRuxeChT438AAAAASUVORK5CYII=" alt="" />
06-自定义Attribute标记案例的更多相关文章
- .net c#获取自定义Attribute
前言: 在c#开发中,有时候我们需要读取 Attribute中的信息(关于Attribute , 我自己把他理解成一个可以为类,属性标记的东西,这个标记可以为你提供一些关于类,方法,属性的额外信息) ...
- Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论
Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论 创建用户自定义的类加载器 要创建用户自定义的类加载器,只需要扩展java.lang.ClassLoader类,然后覆盖它的f ...
- XsdGen:通过自定义Attribute与反射自动生成XSD
前言 系统之间的数据交互往往需要事先定义一些契约,在WCF中我们需要先编写XSD文件,然后通过自动代码生成工具自动生成C#对象.对于刚刚接触契约的人来说,掌握XMLSpy之类的软件之后确实比手写XML ...
- 2.C#自定义Attribute
阅读目录 一:C#自定义Attribute 二:AttributeUsageAttribute中的3个属性(Property)中的AttributeTargets 三:Attribut ...
- 自定义Attribute 服务端校验 客户端校验
MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...
- C#自定义Attribute值的获取与优化
C#自定义Attribute值的获取是开发中会经常用到的,一般我们的做法也就是用反射进行获取的,代码也不是很复杂. 1.首先有如下自定义的Attribute [AttributeUsage(Attri ...
- CVS导出&&自定义Attribute的使用
1.cvs导出:List转为byte[] /// <summary> /// CvsExport帮助类 /// </summary> public static class C ...
- 转:C#制作ORM映射学习笔记一 自定义Attribute类
之前在做unity项目时发现只能用odbc连接数据库,感觉非常的麻烦,因为之前做web开发的时候用惯了ORM映射,所以我想在unity中也用一下ORM(虽然我知道出于性能的考虑这样做事不好的,不过自己 ...
- 【MVC 笔记】MVC 自定义 Attribute 属性中的猫腻
原想在 MVC Action 上加一个自定义 Attribute 来做一些控制操作,最先的做法是在自定 Attribute 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...
随机推荐
- ASP.NET中实现页面间数据传递的方法
说到页面间数据传递,很多人都会想到通过像Session这样的全局变量,但是向Session中添加的东西太多会增加服务器的压力,页面间数据传递,数据的作用范围越小越好. ASP.NET页面间数据传递 ...
- centos账户管理命令(root权限)
cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1 查看所有用户 userdel -r 用户名 -删除用户 ...
- Android学习----ADB
adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.a ...
- underscorejs-indexBy学习
2.19 indexBy 2.19.1 语法 _.indexBy(list, iteratee, [context]) 2.19.2 说明 给定一个list,和 一个用来返回一个在列表中的每个元素键 ...
- php cookie不刷新及时生效的实现代码
<?php /** * 不刷新 cookie及时生效 */ cookie("mycookie","cookievalue",time()+60); coo ...
- MySQL重置root密码的几种方法(windows+Linux)
重置root密码的方法: windows系统下:1.停止mysql服务:2.新建文件init-root.txt,写上如下内容: update mysql.user set password = pas ...
- PBOC规范研究
一.ISO14443协议和PBOC关于CID的约定 看过协议的人其实都明白,RATS命令中参数字节的低半字节是CID,期中,CID不能为15. ISO14443协议中要求当RATS命令的CID等于0时 ...
- Android AlarmManager实现不间断轮询服务
在消息的获取上是选择 轮询还是推送得根据实际的业务需要来技术选型,例如对消息实时性比较高的需求,比如微博新通知或新闻等那就最好是用推送了.但如果只是一般的消息检测比如 更新检查,可能是半个小时或一个小 ...
- 输入一个单向链表,输出该链表中倒数第K个结点
输入一个单向链表,输出该链表中倒数第K个结点,具体实现如下: #include <iostream> using namespace std; struct LinkNode { publ ...
- 【转】设置TextView文字居中
原文网址:http://blog.csdn.net/lanpy88/article/details/6616924 有2种方法可以设置TextView文字居中: 一:在xml文件设置:android: ...