zoukankan      html  css  js  c++  java
  • asp.net mvc 反射应用

    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;
            }
        }
    }
  • 相关阅读:
    config Doku wiki
    [转载]【python】ipython与python的区别
    数组和指针
    C++初始化数据成员
    RSA算法原理
    C++四种cast
    百度笔试准备2
    百度笔试准备1
    百度面试准备
    C++的空类中默认产生哪些类成员函数
  • 原文地址:https://www.cnblogs.com/david1989/p/3404551.html
Copyright © 2011-2022 走看看