惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Security Latest
Security Latest
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
S
Secure Thoughts
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
D
DataBreaches.Net
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
罗磊的独立博客
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
V
V2EX
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tenable Blog
T
Threat Research - Cisco Blogs
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
F
Full Disclosure
H
Help Net Security
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
O
OpenAI News
S
Securelist

博客园 - baileyer

VisualSVN破解安装到VS2026 使用 SQL 中的递归查询(Recursive CTE)来实现1-50数字 git 忽略本地文件提交(VS) C# 自定义导出模板(NPOI) Abp vue项目找不到模块“./app.vue” MSSqlserver分割文件备份恢复 github的镜像下载加快clone下载速度 widows部署.NET Core 3.1项目到IIS问题 .net core/mvc获取特性 oracle instr 替代like查询 quartz3.0.7和topshelf4.2.1实现任务调度 VS2017同时生成.net core和.net framework两份代码 .net core Failed to load API definition 错误 vs 2017 调试中断问题 .net Core 2.2实现京东宙斯API采用OAuth授权方式调用 WinForm 校验只能输入数字英文字母退格键 NPOI导出excel activemq.bat 在window7 x64下启动(安装)报错解决方案 <asp:RadioButton> 选项判断
C#通过反射获取相应的字段和值
baileyer · 2018-07-21 · via 博客园 - baileyer

代码比较简单,只作为简单的例子参考

首先先看运行的代码:

 class Program
    {
        static void Main(string[] args)
        {
            UserInfo userInfo = new UserInfo();
            userInfo.ID = 1;
            userInfo.Name = "bailey";
            userInfo.CreateDate = DateTime.Now;
            userInfo.Number = Convert.ToDecimal(456.6467);

            string values = string.Empty;
            foreach (System.Reflection.PropertyInfo p in userInfo.GetType().GetProperties())
            {
                if (p.PropertyType == typeof(string))
                {
                    values += string.Format("{0}='{1}', ", p.Name, p.GetValue(userInfo));
                }
                if (p.PropertyType == typeof(int)|| p.PropertyType == typeof(uint))
                {
                    values += string.Format("{0}={1},", p.Name, p.GetValue(userInfo));
                }
                if (p.PropertyType == typeof(DateTime))
                {
                    values += string.Format("{0}='{1}', ", p.Name, p.GetValue(userInfo));
                }
                if (p.PropertyType == typeof(decimal) || p.PropertyType == typeof(double)|| p.PropertyType == typeof(float))
                {
                    values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                }
               
                if (p.PropertyType == typeof(bool))
                {
                    values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                }
                if (p.PropertyType == typeof(sbyte))
                {
                    values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                }
                if (p.PropertyType == typeof(byte) || p.PropertyType == typeof(short) || p.PropertyType == typeof(ushort) )
                {
                    values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                }
                if (p.PropertyType == typeof(long) || p.PropertyType == typeof(ulong))
                {
                    values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                }

                // values +=string.Format( "{0}={1},", p.Name, p.GetValue(userInfo));
                // Console.WriteLine("Name:{0} Value:{1}", p.Name, p.GetValue(userInfo));
            }
            Console.WriteLine(values);
            Console.ReadLine();
        }

    }

再看对象:

 class UserInfo {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime? CreateDate { get; set; }
        public decimal? Number { get; set; }
        public bool IsUse { get; set; }
    }