/// <summary>
/// 根据对象集合获取下拉列表的绑定对象
/// </summary>
/// <typeparam name="T">任意对象</typeparam>
/// <param name="t">任意对象集合</param>
/// <param name="k">下拉显示字段</param>
/// <param name="v">下拉隐藏字段</param>
/// <returns></returns>
public Hashtable ToHashtable<T>(T[] t, string k, string v)
{
if (t == null || t.Length == 0)
{
return null;
} Hashtable hst = new Hashtable();
for (int i = 0; i < t.Length; i++)
{
PropertyInfo[] properties = t[i].GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0)
{
return null;
}
hst.Add(getValue<T>(t[i], properties, k), getValue<T>(t[i], properties, v));
}
return hst;
} private object getValue<T>(T t, PropertyInfo[] properties, string k)
{
foreach (PropertyInfo item in properties)
{
string name = item.Name;
object value;
if (name == k)
{
return value = item.GetValue(t, null);
}
}
return null;
}
该方法在webform调用使用正常,但是在winform调用是属性数组长度为0.请问怎么解决?
/// 根据对象集合获取下拉列表的绑定对象
/// </summary>
/// <typeparam name="T">任意对象</typeparam>
/// <param name="t">任意对象集合</param>
/// <param name="k">下拉显示字段</param>
/// <param name="v">下拉隐藏字段</param>
/// <returns></returns>
public Hashtable ToHashtable<T>(T[] t, string k, string v)
{
if (t == null || t.Length == 0)
{
return null;
} Hashtable hst = new Hashtable();
for (int i = 0; i < t.Length; i++)
{
PropertyInfo[] properties = t[i].GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0)
{
return null;
}
hst.Add(getValue<T>(t[i], properties, k), getValue<T>(t[i], properties, v));
}
return hst;
} private object getValue<T>(T t, PropertyInfo[] properties, string k)
{
foreach (PropertyInfo item in properties)
{
string name = item.Name;
object value;
if (name == k)
{
return value = item.GetValue(t, null);
}
}
return null;
}
该方法在webform调用使用正常,但是在winform调用是属性数组长度为0.请问怎么解决?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货