不废话了,直接上代码吧:
public static object GetElementFromCollection(object obj, int index)
{
Type type = obj.GetType();
if (type.IsArray)
{
return ((Array) obj).GetValue(index);
}
else
{
object[] attributes = type.GetCustomAttributes(typeof(DefaultMemberAttribute), true);
DefaultMemberAttribute attribute = (DefaultMemberAttribute) attributes[0];
String indexerName = attribute.MemberName;
type.GetProperties();
PropertyInfo pi2 = type.GetProperty(indexerName, new Type[] { typeof(int) });
return pi2.GetValue(obj, new Object[] {index});
}
}
(Visited 625 times, 1 visits today)
2010年7月5日 at 14:52
java?
我还是学习中
2010年7月7日 at 17:34
呵呵,C#