我有一个 .Net 对象(在 C# 中),它具有名为 event1、event2 等的属性。
我必须对每一个做一些 if-else。有没有办法我可以遍历这些。如果这些是控件,我可以使用控件集合,但这些是对象的属性。
有什么帮助吗?
最佳答案
假设您知道要处理的属性有多少
for(int eventIndex = 0; eventIndex < NUM_EVENTS; eventIndex++)
{
PropertyInfo eventPropertyInfo =
this.GetType().GetProperty("Event" + eventIndex);
if (eventPropertyInfo.GetValue(this, null) == yourValue)
{
//Do Something here
}
}
关于c# - 在循环中访问相似的命名属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/750204/