C# 遍历泛型集合

时间:2021-03-09 16:51:18
     /// <summary>
/// 遍历泛型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static List<T> Abcdefg<T>(List<T> obj)
{
foreach (T t in obj)
{
if (t == null)
{
continue;
}
PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
int length = myPropertyInfo.Length;
for (int i = ; i < length; i++)
{
PropertyInfo pi = myPropertyInfo[i];
// 获取值
pi.GetValue(t, null);
// 修改值
pi.SetValue(t, "要修改的值,但是必须保证与当前字段类型相同", null); }
}
return obj;
}