当前位置:网站首页>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;
}
边栏推荐
- 基于Unittest的ddt+yaml实现数据驱动机制
- DC motor winding parameters
- The influence of head zeroing and tail zeroing on FFT output
- Possible reasons why there is no voltage in the corresponding channel, but the ADC value is changing greatly and is not equal to 0
- 网络设备硬核技术内幕 防火墙与安全网关篇 (六) 安全双修大法 下
- 自动推理的逻辑07–谓词演算
- [must read for new products] valuation analysis of Meishi technology, distributed audio-visual products and Solutions
- Threejs personal notes
- 单片机之led、数码管与按键
- Redis-三大特殊数据类型的学习和理解
猜你喜欢

Fastjson历史漏洞复现

Selection of FFT sampling frequency and sampling points

Code review tool

map集合

激活最大化
![Jericho will make a popping sound when turning on, broadcasting a prompt tone, and turning off [chapter]](/img/e3/ea7278eb855cca2a1af4f96772bcb1.png)
Jericho will make a popping sound when turning on, broadcasting a prompt tone, and turning off [chapter]

立即报名 | 云原生技术交流 Meetup 广州站已开启,8 月 6 号与你相遇!

How does matlab set the K-line diagram to classic red and green color matching?
![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]

《KMP复习 + AC自动机》前传
随机推荐
Y79. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (10)
Read cmake in one article
网络设备硬核技术内幕 防火墙与安全网关篇 (九) 虚拟化神器 (下)
Possible reasons why there is no voltage in the corresponding channel, but the ADC value is changing greatly and is not equal to 0
加拿大法院认定孟晚舟“双重犯罪”成立,引渡程序将继续进行!
What is the org relationship mitigation strategy of Microsoft edge browser tracking prevention
Recurrence of fastjson historical vulnerabilities
特权更改对现有连接的影响
网络设备硬核技术内幕 防火墙与安全网关篇 (十)
Set data constructor
Network equipment hard core technology insider firewall and security gateway (12) the mystery of zero contact office
一周年创作纪念日,冲吧少年郎
Matlab | those matlab tips you have to know (3)
立即报名 | 云原生技术交流 Meetup 广州站已开启,8 月 6 号与你相遇!
Redis learning and understanding of three special data types
Jericho will make a popping sound when turning on, broadcasting a prompt tone, and turning off [chapter]
自动推理的逻辑09–自动定理证明
Logic of automatic reasoning 07 - predicate calculus
程序员成长第三十篇:你真的懂反馈吗?
递归求解迷宫问题