当前位置:网站首页>c# 反射之Type使用
c# 反射之Type使用
2022-07-27 22:10:00 【L-960】
c# 通过反射复制不同对象属性
public static void CopyPropertiesTo<T, TU>(T source, TU dest)
{
// PropertyInfo:类的成员字段
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);
}
}
}
}
通过反射获取泛型对象的属性
public static HashEntry[] Bean2HashEntry<T>(T t)
{
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
HashEntry[] hashEntries = new HashEntry[properties.Length];
//里面是所有公共属性
int i = 0;
Array.ForEach<PropertyInfo>(properties,
filed =>
{
// t的属性值
object? filedValue = filed.GetValue(t);
// t的属性名
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;
}
边栏推荐
- Basic elementary function
- 小波变换学习笔记
- 单片机之led、数码管与按键
- Code review tool
- 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
- Redis-三大特殊数据类型的学习和理解
- 估值360亿美元!即将进行首次载人发射的SpaceX筹资3.46亿美元
- 多线程及多线程程序的编写
- leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
- Basic operations of MySQL database (3) --- Based on fields
猜你喜欢

【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发

How does JMeter solve the problem of garbled code?
![[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development](/img/29/f31fa3af18198754d2433f47c0c556.jpg)
[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development

Matlab | those matlab tips you have to know (3)

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

Selection of FFT sampling frequency and sampling points

Ali Er Mian: why do we need to separate databases and tables?
![[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development](/img/17/15c759aadafc335028d37380903ee7.jpg)
[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development

Map set

Code review tool
随机推荐
Impulse attends the 2022 Forum on safe circulation of data elements Online - a special session in the field of government affairs, and helps the construction and innovative development of big data for
Threejs personal notes
Leetcode 452. minimum number of arrows to burst balloons (medium)
[BuildRelease Management]Parabuild
When Jerry made a phone call, recording to SD card /u disk was not enough [article]
Matlab | those matlab tips you have to know (3)
Firefox 103, the Firefox browser, has been released to improve performance under high refresh rate displays
mysql数据库的基本操作(二)-——基于数据表
Selection of FFT sampling frequency and sampling points
Redis transaction and optimistic lock
What is the org relationship mitigation strategy of Microsoft edge browser tracking prevention
FFT 采样频率和采样点数的选取
Redis-三大特殊数据类型的学习和理解
基本初等函数
IP address & subnet mask
投资80亿!南京华天封测一期项目即将投产!
Intel AI practice day issue 56 | explore new trends in industry development
Impact of privilege changes on existing connections
Volkswagen China invested 8billion yuan and became the largest shareholder of GuoXuan high tech
网络设备硬核技术内幕 防火墙与安全网关篇 (七) 虚拟化神器 (上)