2011-12-22 36 views
0

我有一个类,我从字符串调用方法名称与下面的代码,其中TalentProfile是我的类我运行该方法和返回一个未知的对象类型。滚动浏览一个未知类型的列表

public Object RegulatorValue 
public Type RegulatorType 

BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; 

      List<MemberInfo> members = Common.AttributeHelpers.GetFieldAndPropertyList(this.TalentProfile, bindingFlags); 

      foreach (MemberInfo member in members.Where(t => t.Name == methodName)) 
      { 
       this.RegulatorValue = member.GetValue(this.TalentProfile); 
       this.RegulatorType = this.RegulatorValue.GetType(); 
       this.Regulator = methodName; 
      } 

这很好,如果我带回像字符串的东西,但我有麻烦,当我带回列表。我有一个特别的方法可以返回一个列表。如果我硬编码一个foreach寻找phonenumber它效果很好。我希望能够在不知道列表的类型是什么类型的情况下浏览列表,然后通过列表中的项目的属性并能够做到这一点。

+0

对于列表中项目的属性,您希望能够做什么“某事”? – phoog 2011-12-23 03:27:53

回答

0

.net中最基本的收集类型是IEnumerable。将您的值转换为IEnumerable并对其进行迭代。

IEnumerable enumerable = (IEnumerable)member.GetValue(this.TalentProfile); 
if (enumerable != null) 
{ 
    IEnumerator enumerator = enumerable.GetEnumerator(); 
    while (enumerator.MoveNext()) 
    { 
     object obj = enumerator.Current; 
     // do something with obj 
    } 
}