反射的使用
2014-02-20 17:50
555 查看
例如:将公司信息分类显示。
1.公司信息分为“基本信息”、“公司描述”。
public class CompanyInfo
{
[Describute("基本信息", true, "公司名称")]
public string Name { get; set; }
[Describute("基本信息",true,"成立时间")]
public string Foundtion {get;set;}
[Describute("公司描述", true, "公司描述")]
public string OverView { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
public class Describute : Attribute
{
public string style { get; set; }
public bool show { get; set; }
public string content { get; set; }
/// <summary></summary>
/// <param name="style">父标签</param>
/// <param name="show">是否显示</param>
/// <param name="content">标签内容</param>
public Describute(string style, bool show, string content)
{
this.style = style;
this.show = show;
this.content = content;
}
}
2.根据不同类别获取对应的信息。
public void GetCompanyInfo()
{
CompanyInfo info = new CompanyInfo()
{
Name = "名称",
OverView = "公司简介"
};
foreach (System.Reflection.PropertyInfo p in info.GetType().GetProperties())
{
Describute describute = (Describute)(p.GetCustomAttributes(typeof(Describute), false))[0];
object pValue = p.GetValue(info, null);
string Info = string.Empty;
if (pValue != null) { Info = pValue.ToString().Replace(" ", string.Empty); }
if (string.IsNullOrEmpty(Info)) { Info = "暂无信息"; }
string valueStr = describute.content == string.Empty ? Info : describute.content + ":" + Info ; //格式(描述:值)
switch (describute.style)
{
case "基本信息":
//TODO:获取基本信息:公司名称、成立时间
break;
case "公司描述":
//TODO:获取公司描述
break;
default:
break;
}
}
}
1.公司信息分为“基本信息”、“公司描述”。
public class CompanyInfo
{
[Describute("基本信息", true, "公司名称")]
public string Name { get; set; }
[Describute("基本信息",true,"成立时间")]
public string Foundtion {get;set;}
[Describute("公司描述", true, "公司描述")]
public string OverView { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
public class Describute : Attribute
{
public string style { get; set; }
public bool show { get; set; }
public string content { get; set; }
/// <summary></summary>
/// <param name="style">父标签</param>
/// <param name="show">是否显示</param>
/// <param name="content">标签内容</param>
public Describute(string style, bool show, string content)
{
this.style = style;
this.show = show;
this.content = content;
}
}
2.根据不同类别获取对应的信息。
public void GetCompanyInfo()
{
CompanyInfo info = new CompanyInfo()
{
Name = "名称",
OverView = "公司简介"
};
foreach (System.Reflection.PropertyInfo p in info.GetType().GetProperties())
{
Describute describute = (Describute)(p.GetCustomAttributes(typeof(Describute), false))[0];
object pValue = p.GetValue(info, null);
string Info = string.Empty;
if (pValue != null) { Info = pValue.ToString().Replace(" ", string.Empty); }
if (string.IsNullOrEmpty(Info)) { Info = "暂无信息"; }
string valueStr = describute.content == string.Empty ? Info : describute.content + ":" + Info ; //格式(描述:值)
switch (describute.style)
{
case "基本信息":
//TODO:获取基本信息:公司名称、成立时间
break;
case "公司描述":
//TODO:获取公司描述
break;
default:
break;
}
}
}
相关文章推荐
- Mahout学习路线图
- 风宇冲Unity3D教程学院-----Shader篇
- TabBar背景色
- C#在button上显示图片
- IOS7 隐藏状态栏 (电池栏)
- c# Byte[]、Image、Bitmap 之间的相互转换
- C# 解析字符串成xml格式
- 关于bat文件语法
- Android 自定义View及其在布局文件中的使用示例
- 换晶振导致stm32串口数据飞码的解决办法
- mongodb,数值类型查找不到数据的问题
- QueryRunner的使用
- 在linux系统中配置java环境变量的三种方法
- PostgreSQL使用手册
- protobuf for lua in Mac
- 【快速幂取模】【AOJ-569】乘的更快
- 换晶振导致stm32串口数据飞码的解决办法
- 取石子
- POSIX 线程详解,第 2部分
- Last_Errno: 1062,Last_Error: Error Duplicate entry