C#通过属性名字符串获取、设置对象属性值操作示例
作者:willingtolove 发布时间:2022-01-06 14:28:08
标签:C#,属性名,对象属性值
本文实例讲述了C#通过属性名字符串获取、设置对象属性值操作.分享给大家供大家参考,具体如下:
#通过反射获取对象属性值并设置属性值
0、定义一个类
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
}
1、通过属性名(字符串)获取对象属性值
User u = new User();
u.Name = "lily";
var propName = "Name";
var propNameVal = u.GetType().GetProperty(propName).GetValue(u, null);
Console.WriteLine(propNameVal);// "lily"
2、通过属性名(字符串)设置对象属性值
User u = new User();
u.Name = "lily";
var propName = "Name";
var newVal = "MeiMei";
u.GetType().GetProperty(propName).SetValue(u, newVal);
Console.WriteLine(propNameVal);// "MeiMei"
#获取对象的所有属性名称及类型
通过类的对象实现
User u = new User();
foreach (var item in u.GetType().GetProperties())
{
Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}");
}
// propName: Id,propType: Int32
// propName:Name,propType: String
// propName:Age,propType: String
通过类实现
foreach (var item in typeof(User).GetProperties())
{
Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}");
}
// propName: Id,propType: Int32
// propName:Name,propType: String
// propName:Age,propType: String
#判断对象是否包含某个属性
static void Main(string[] args)
{
User u = new User();
bool isContain= ContainProperty(u,"Name");// true
}
public static bool ContainProperty( object instance, string propertyName)
{
if (instance != null && !string.IsNullOrEmpty(propertyName))
{
PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName);
return (_findedPropertyInfo != null);
}
return false;
}
将其封装为扩展方法
public static class ExtendLibrary
{
/// <summary>
/// 利用反射来判断对象是否包含某个属性
/// </summary>
/// <param name="instance">object</param>
/// <param name="propertyName">需要判断的属性</param>
/// <returns>是否包含</returns>
public static bool ContainProperty(this object instance, string propertyName)
{
if (instance != null && !string.IsNullOrEmpty(propertyName))
{
PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName);
return (_findedPropertyInfo != null);
}
return false;
}
}
static void Main(string[] args)
{
User u = new User();
bool isContain= u.ContainProperty("Name");// true
}
希望本文所述对大家C#程序设计有所帮助。
来源:https://www.cnblogs.com/willingtolove/p/12198871.html


猜你喜欢
- 本文实例讲述了Android判断Activity是否在最上层的方法。分享给大家供大家参考,具体如下:private boolean isTo
- JSON字符串和java对象的互转【json-lib】在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML、JSON等,JSON
- 前言通过adb shell input可以模拟android各种输入事件,比如文字、按键、触摸等等。adb shell inputUsage
- 前端开发工程师和关注前端开发的开发者们在2015年中肯定被腾讯的JSSDk引爆过,搞APP的、搞前端的甚至是是搞后端的都跑过来凑热闹,一时之
- 本文实例为大家分享了Java网络编程TCP程序设计的具体代码,供大家参考,具体内容如下[1] TCP编程的主要步骤客户端(client):1
- maven运行依赖于 JAVA_HOME如果各位还没有配置 JAVA_HOME,可以参考我的另一篇博客 JDK环境变量配置 JDK 环境变量
- 简介本次五子棋使用的是光标控制移动,通过按空格键(键值32)来落子,实现游戏的。我们额外用到的头文件有:#include<getch.
- 关于死信队列在大多数的MQ中间件中,都有死信队列的概念。死信队列同其他的队列一样都是普通的队列。在RabbitMQ中并没有特定的“死信队列”
- 这几天恰好和朋友谈起了递归,忽然发现不少朋友对于“尾递归”的概念比较模糊,网上搜索一番也没有发现讲解地完整详细的资料,于是写了这么一篇文章,
- 简介Security 是 Spring 家族中的一个安全管理框架。相比与另外一个安全框架Shiro,它提供了更丰富的功能,社区资源也比Shi
- 本文实例讲述了C#接口interface用法。分享给大家供大家参考。具体如下:using System;//example of inter
- 前言最近,新来的同事写接口,需要知道lua怎么调用C#脚本,趁这个机会也给大家分享一下。道理我也不多少,直接上干货。框架介绍本项目采用lua
- String类原生的字符串处理方法short s=1;s=s+1;与short s=1;s+=1;的区别一、“+&
- 首先对于一个SpringBoot工程来说,最明显的标志的就是 @SpringBootApplication它标记了这是一个SpringBoo
- Android ViewPager2 UsageViewPager2 是 ViewPager 的升级版本,解决了 ViewPager 的大部
- @Async注解如何实现方法异步处理大批量数据的时候,效率很慢。所以考虑一下使用多线程。刚开始自己手写的一套,用了线程池启动固定的线程数进行
- 本文实例讲述了C#快速排序算法。分享给大家供大家参考。具体实现方法如下:public static int[] QuickSort(int[
- 【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1
- 以前就遇到过这个问题,今天重新拾起来。跑马灯效果其实就是当文字超过TextView控件宽度的时候,使用滚动的方式显示出来:方法1:(直接xm
- 本文实例讲述了C#实现的MD5加密功能与用法。分享给大家供大家参考,具体如下:1、创建MD5Str.cs加密处理类public class