已知
mD tmp = Item as mD;
string a=tmp.DeptCode.ToLower().IndexOf(search.ToLower());
现在如果mD可能是一个未知类似我用反射的方法去实现上面的功能string dp="DeptCode";
PropertyInfo[] properties = Item.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo a in properties)
{
  if (dp== a.Name)  //存在DeptCode属性
  {
    string a=    //这里用反射怎么写?
  }
}

解决方案 »

  1.   

    string a=tmp.DeptCode.ToLower().IndexOf(search.ToLower());这样的赋值能成立?
      

  2.   


    string dp="DeptCode";
    PropertyInfo[] properties = Item.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
    foreach (PropertyInfo a in properties)
    {
      if (dp== a.Name) //存在DeptCode属性
      {
      int index=(a.GetValue(Item,null) as string).ToLower().IndexOf(search.ToLower());
    //这里用反射怎么写?
      }
    }
      

  3.   

    foreach (PropertyInfo info in b.GetType().GetProperty("Property2").GetValue(b, null).GetType().GetProperties()) 
                {
    MessageBox.Show(string.Format("{0}={1}",info.Name,info.GetValue(b.Property2,null)));
                }