栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

Attribute/特性心得随笔

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Attribute/特性心得随笔

复制代码 代码如下:



复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

///
/// DisAttribute 的摘要说明
///

public class DisAttribute : Attribute
{
private string _message;
///
/// 描述
///

public string Message
{
get { return _message; }
}

public DisAttribute(string message)
{
this._message = message;
}
}


复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Linq;
using System.Web;
using System.Web.DynamicData;


///
/// User 的摘要说明
///

[DisAttribute("User"),TableName("user"),Description("user")]
public class User
{
private int? _id;
///
/// Id
///

[DisAttribute("主键")]
public int? Id
{
get { return _id; }
set { _id = value; }
}


private string _name;
///
/// 名称
///

[DisAttribute("名称")]
public string Name
{
get { return _name; }
set { _name = value; }
}
}


复制代码 代码如下:
//获取特性
User u = new User();
Type _t = u.GetType();
foreach (Attribute a in _t.GetCustomAttributes(true))
{
if (a.GetType().ToString() == "DisAttribute")
{
DisAttribute _da = (DisAttribute)a;
if (_da != null)
{
Response.Write(_da.Message + "
");
}
}
}
//获取所有属性
u.Id = 888888;
u.Name = "陈奕迅";
foreach (PropertyInfo item in _t.GetProperties())
{
//特性
Attribute atr = item.GetCustomAttribute(typeof(DisAttribute));
if (atr.GetType().ToString() == "DisAttribute")
{
DisAttribute _da = (DisAttribute)atr;
if (_da != null)
{
Response.Write(_da.Message + "
");
}
}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/57407.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号