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; } } }