首先你要先知道IndexFactory这个DLL中执行mGetDataByPage.Invoke(obj, new object[] { i, 1000 });得到的是什么?
如果是list集合你可以参照下面代码 public static List<object> GetRefection(object obj) { List<object> objs = new List<object>(); Type t = obj.GetType();
PropertyInfo[] pros = t.GetProperties(); for (int i = 0; i < 2; i++) {
foreach (PropertyInfo pro in pros) { pro.SetValue(obj, i.ToString(), null); } objs.Add(obj); }
return objs; }
得到的是List<Model.Asppatent>
得到的是List<Model.Asppatent>
如果你的得到的是List<Model.Asppatent> 那就是 foreach(Model.Asppatent item in list)
反射调用方法只有执行完后才知道数据类型,我现在是没执行之前用var去接获取的数据,然后再遍历他
反射调用方法只有执行完后才知道数据类型,我现在是没执行之前用var去接获取的数据,然后再遍历他 var list = mGetDataByPage.Invoke(obj, new object[] { i, 1000 }); 你这不是执行了么? 你不执行你这list怎么来啊? 还是你没说清楚
遍历的时候转换 形如 foreach (IMachine m in machineList) { if (m is MachineLine) { ProcessLine((MachineLine) m); } else if (m is MachineCircle) { ProcessLine((MachineCircle) m); } }
IList MakeListOfType(Type listType) { Type type = typeof(List<>); Type specificListType = type.MakeGenericType(listType); return (IList)Activator.CreateInstance(specificListType); } IList list = MakeListOfType(tm);
最简单的是用dynamic,连反射都省了 假设obj是你反射得到的list: object list = ... foreach (dynamic item in list as IEnumerable) { string s = item.Name; // 假设 item 有个属性叫 Name }
{
List<object> objs = new List<object>();
Type t = obj.GetType();
PropertyInfo[] pros = t.GetProperties();
for (int i = 0; i < 2; i++)
{
foreach (PropertyInfo pro in pros)
{
pro.SetValue(obj, i.ToString(), null);
}
objs.Add(obj);
}
return objs;
}
那就是
foreach(Model.Asppatent item in list)
var list = mGetDataByPage.Invoke(obj, new object[] { i, 1000 });
你这不是执行了么? 你不执行你这list怎么来啊? 还是你没说清楚
形如
foreach (IMachine m in machineList) {
if (m is MachineLine) {
ProcessLine((MachineLine) m);
} else if (m is MachineCircle) {
ProcessLine((MachineCircle) m);
}
}
{
Type type = typeof(List<>);
Type specificListType = type.MakeGenericType(listType); return (IList)Activator.CreateInstance(specificListType);
} IList list = MakeListOfType(tm);
假设obj是你反射得到的list:
object list = ...
foreach (dynamic item in list as IEnumerable)
{
string s = item.Name; // 假设 item 有个属性叫 Name
}