C#通过属性名字符串获取、设置对象属性值操作示例
作者:willingtolove 时间:2022-01-06 14:28:08
本文实例讲述了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
标签:C#,属性名,对象属性值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 实现单线程轮循机制批量下载图片
2022-11-05 11:03:45
浅谈SpringBoot资源初始化加载的几种方式
2022-03-09 08:14:34
![](https://img.aspxhome.com/file/2023/3/80253_0s.jpg)
深入理解Java显式锁的相关知识
2022-06-16 09:59:41
android传送照片到FTP服务器的实现代码
2021-07-09 22:59:32
Spring JPA 增加字段执行异常问题及解决
2023-06-25 23:55:58
RocketMQ消息存储文件的加载与恢复机制源码分析
2021-12-29 20:23:19
![](https://img.aspxhome.com/file/2023/3/95943_0s.jpg)
Seata AT模式TransactionHook被删除探究
2022-01-12 14:56:49
![](https://img.aspxhome.com/file/2023/2/63492_0s.png)
IDEA+JRebel实现全自动热部署的方法步骤
2023-11-29 13:53:30
![](https://img.aspxhome.com/file/2023/4/60454_0s.jpg)
java中javaBean与Bean的深入理解
2021-11-27 13:19:11
Java实现经典游戏Flappy Bird的示例代码
2022-03-28 07:05:37
![](https://img.aspxhome.com/file/2023/5/88155_0s.png)
obix协议在java中的配置和使用详解
2023-11-25 20:59:42
![](https://img.aspxhome.com/file/2023/5/60275_0s.jpg)
C#实现QQ截图功能及相关问题
2022-10-30 08:47:01
![](https://img.aspxhome.com/file/2023/5/84415_0s.png)
C#先判断是否存在再创建文件夹或文件与递归计算文件夹大小
2023-07-29 00:04:58
![](https://img.aspxhome.com/file/2023/6/89806_0s.png)
Spring JDK动态 代理实现过程详解
2023-11-16 19:42:15
![](https://img.aspxhome.com/file/2023/2/59392_0s.png)
从Cocos2d-x2迁移到Cocos2d-x3的过程分享
2021-06-07 23:52:28
java:程序包org.springframework.boot不存在的完美解决方法
2023-09-03 20:37:32
![](https://img.aspxhome.com/file/2023/3/84163_0s.jpg)
java基础之数组常用操作总结(必看篇)
2022-12-09 03:32:17
![](https://img.aspxhome.com/file/2023/0/88160_0s.png)
Spring的事务管理你了解吗
2023-02-05 19:44:16
![](https://img.aspxhome.com/file/2023/4/88204_0s.png)
AndroidStduio3.0 使用gradle将module打包jar文件的方法
2023-07-01 06:57:20
![](https://img.aspxhome.com/file/2023/4/84224_0s.png)
Java中两个List之间的比较方法(差集、交集和并集)
2023-03-06 06:34:42