CodeGo.net>使用反射,我如何检测具有二传手的属性?

我有以下代码循环遍历一个对象并通过反射获取其所有属性:

foreach (var propertyInfo in typeof(TBase).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
    var oldValue = propertyInfo.GetValue(oldVersion, null);
}

如何检查仅查看带有“设置”的属性? (我想忽略只读值-只是“获取”。)

leora asked 2020-08-06T10:46:30Z
2个解决方案
102 votes

PropertyInfo.GetSetMethod(文档)

要么

PropertyInfo.GetSetMethod(文档)

STO answered 2020-08-06T10:46:44Z
15 votes
propertyInfo.GetSetMethod() != null
Kirk Woll answered 2020-08-06T10:47:00Z
translate from https://stackoverflow.com:/questions/3390358/using-reflection-how-do-i-detect-properties-that-have-setters