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;
            }
        }
    }
  • 相关阅读:
    c语言求最大公约数和最小公倍数(转)
    git 提交去除每次输账号密码
    phpstorm使用zen coding 快速编辑补全html/css代码
    YII2.0使用ActiveForm表单(转)
    php面向对象之trait
    php操作redis(转)
    模块
    列表生成式 与生成器表达式
    三元表达式,递归,内置函数
    面向过程的编程思想
  • 原文地址:https://www.cnblogs.com/david1989/p/3404551.html
Copyright © 2011-2022 走看看