您的位置:首页 > 编程语言 > ASP

asp.net mvc 反射应用

2013-11-03 01:05 447 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
//实体类信息复制
public static void EntityToEntity<T>(T pTargetObjSrc, T pTargetObjDest)
{
try
{
foreach (var mItem in typeof(T).GetProperties())
{
mItem.SetValue(pTargetObjDest, mItem.GetValue(pTargetObjSrc, new object[] { }), null);
}
}
catch (NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception Ex)
{
throw Ex;
}
}
static void Main(string[] args)
{
MyClass obj = new MyClass();
Type t = typeof(MyClass);
int i = 0;
obj.five = 11111111;
foreach (var item in t.GetProperties())
{
//设置实体类属性值
item.SetValue(obj, item.GetValue(obj, new object[] { }), null);
i += 1;
}
StringBuilder sb = new StringBuilder();

foreach (var item in t.GetProperties())
{
object[] attrs = item.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true);
if (attrs.Length > 0)
{
string attributename = (attrs[0] as System.ComponentModel.DisplayNameAttribute).DisplayName;
sb.Append("类型:" + item.PropertyType.FullName +attributename+ ":" + item.Name + "值:" + item.GetValue(obj, null) + "");
}
}
string result = sb.ToString();
//读取实体类所有信息
Console.Write(result);
}
}
public class MyClass
{
[System.ComponentModel.DisplayName("一")]
public int one
{
set;
get;
}
[System.ComponentModel.DisplayName("二")]
public int two
{
set;
get;
}
[System.ComponentModel.DisplayName("五")]
public int five
{
set;
get;
}
[System.ComponentModel.DisplayName("三")]
public int three
{
set;
get;
}
[System.ComponentModel.DisplayName("四")]
public int four
{
set;
get;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: