asp.net mvc 反射应用
2024-08-21 17:58:54
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 = ;
obj.five = ;
foreach (var item in t.GetProperties())
{
//设置实体类属性值
item.SetValue(obj, item.GetValue(obj, new object[] { }), null);
i += ;
}
StringBuilder sb = new StringBuilder(); foreach (var item in t.GetProperties())
{
object[] attrs = item.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true);
if (attrs.Length > )
{
string attributename = (attrs[] 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;
}
}
}
最新文章
- 关于java中创建文件,并且写入内容
- 关于HTML+CSS设置图片居中的方法
- noi 9265 取数游戏
- Java反射机制练习(增强可扩展性)
- 错误:类Byte是公共的,应在名为Byte.java 的文件中声明public class Byte{}一个错误
- [置顶] WEBSOKET服务器搭建
- 1bpp像素遍历(找了半天,感谢github)
- spring mvc 资源包的映射
- Struts2拦截器登录验证
- BZOJ 3697: 采药人的路径 [点分治] [我想上化学课]
- CodeForces 1151E Number of Components
- 问题解决:Spyder不支持OpenCV模块代码提示
- codeforces 803D Magazine Ad(二分+贪心)
- windows10创建ftp服务器
- CSS3 过渡动画
- DOM操作之获取HTML、文本和值
- 01:zabbix监控redis
- git学习------>Git 分支管理最佳实践
- java中静态变量与静态方法的继承(转)
- HDFS 详解
热门文章
- ios开发中APP底部上滑不能调出如WiFi、蓝牙、播放等的设置页面的解决的方法
- 2017.7.10 Package name does not correspond to the file path
- 对checkpoint not completed的理解
- Eclipse web项目导入Intellij 并且部署
- Android Activity之间经典切换动画
- 倍福TwinCAT(贝福Beckhoff)基础教程 松下官方软件开启报错伺服未就绪怎么办
- python数据类型整理
- 冰点文库下载器V3.1.4
- ubuntu+tomcat,多环境、自动化部署脚本,git+maven+tomcat+ubuntu
- mongo: 索引