当前位置:网站首页>C type use of reflection
C type use of reflection
2022-07-28 00:56:00 【L-960】
c# Copy different object attributes through reflection
public static void CopyPropertiesTo<T, TU>(T source, TU dest)
{
// PropertyInfo: Member field of class
var sourceProps = typeof(T).GetProperties().Where(x => x.CanRead).ToList();
var destProps = typeof(TU).GetProperties()
.Where(x => x.CanWrite)
.ToList();
foreach (var sourceProp in sourceProps)
{
if (destProps.Any(x => x.Name == sourceProp.Name))
{
var p = destProps.First(x => x.Name == sourceProp.Name);
if (p.CanWrite)
{
// check if the property can be set or no.
p.SetValue(dest, sourceProp.GetValue(source, null), null);
}
}
}
}
Get the properties of generic objects through reflection
public static HashEntry[] Bean2HashEntry<T>(T t)
{
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
HashEntry[] hashEntries = new HashEntry[properties.Length];
// Inside are all public attributes
int i = 0;
Array.ForEach<PropertyInfo>(properties,
filed =>
{
// t The attribute value
object? filedValue = filed.GetValue(t);
// t The property name
object? filedName = filed.Name;
var hashEntry = new HashEntry(new RedisValue(filed.Name),
new RedisValue(filed.GetValue(t)?.ToString() ?? string.Empty));
hashEntries[i] = hashEntry;
i++;
});
return hashEntries;
}
边栏推荐
- Selection of FFT sampling frequency and sampling points
- Invest 8billion! Nanjing Huatian sealed test phase I project is about to be put into production!
- 网络设备硬核技术内幕 防火墙与安全网关篇 (小结)
- At least 42 employees are infected with novel coronavirus! Nokia announces closure of telecom equipment plant in India
- Rongyun IM & RTC capabilities on new sites
- Leetcode 452. minimum number of arrows to burst balloons (medium)
- 592. Fraction addition and subtraction: introduction to expression calculation
- 小程序助力智能家居生态平台
- [BuildRelease Management]Parabuild
- 网络设备硬核技术内幕 防火墙与安全网关篇 (九) 虚拟化神器 (下)
猜你喜欢

一周年创作纪念日,冲吧少年郎

Postman下载、使用教程

startUMl
![Leetcode:1997. the first day after visiting all rooms [jump DP]](/img/6e/52d5871a11d1b27e673112a8245b28.png)
Leetcode:1997. the first day after visiting all rooms [jump DP]

Read cmake in one article

The influence of head zeroing and tail zeroing on FFT output

LSB steganography

Leetcode 415. string addition and 43. string multiplication

Resolved Unicode decodeerror: 'UTF-8' codec can't decode byte 0xa1 in position 0: invalid start byte

红队大杀器 Behinder_v4.0(冰蝎4.0)
随机推荐
函数相关知识
蓝桥杯单片机第十一届国赛程序设计试题
Operators in MySQL
数据分析:拆解方法(详情整理)
R language evaluates the relative importance of the predictive factors (variables, characteristics) of the regression model, scales the predictive variables of the regression model, and then construct
投资80亿!南京华天封测一期项目即将投产!
Jerry caused other messages to accumulate in the message pool [article]
分支和循环语句题目练习
Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]
LeetCode_位运算_中等_137.只出现一次的数字 II
The server is poisoned - the dish is the original sin
ASML推出第一代HMI多光束检测机:速度提升600%,适用于5nm及更先进工艺
自动推理的逻辑09–自动定理证明
MySQL limit usage and large paging problem solving
SRv6初登场
LeetCode - 寻找两个正序数组的中位数
mysql分表之后怎么平滑上线?
Ali Er Mian: why do we need to separate databases and tables?
leetcode:1997. 访问完所有房间的第一天【跳跃dp】
单片机之led、数码管与按键