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; } } }
相关文章推荐
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
- Asp.net MVC 插件式应用框架
- MVC架构在Asp.net中的应用和实现
- 一起谈.NET技术,现有ASP.NET MVC框架应用巧妙移植到手机
- 关于Asp.net mvc 2 Ajax thiskbox应用
- ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据
- MVC架构在Asp.net中的应用和实现
- ASP.NET MVC]WebAPI应用支持HTTPS的经验总结
- ASP.NET MVC中的AJAX应用
- ASP.NET MVC 中应用主题
- ASP.NET MVC基于标注特性的Model验证:将ValidationAttribute应用到参数上
- 让我们的ASP.NET MVC应用可以单独维护验证消息
- Ninject之旅之十三:Ninject在ASP.NET MVC程序上的应用(附程序下载)
- MVC架构在Asp.net中的应用和实现
- [ASP.NET MVC] ASP.NET Identity登入技术应用
- 让windows日志记录web程序的异常-EnterpriseLibrary.ExceptionHandling在asp.net mvc中的初步应用
- ASP.NET Mvc Preview 5 演示Demo #4 实现RadioButtonList与DropDownList应用
- Asp.Net MVC4开发二: Entity Framework在Asp.Net MVC4中的应用
- asp.net mvc中应用Grid++ Report (基本应用示例)
- Log4Net异常日志记录在asp.net mvc3.0的应用