foreach(类型 变量 in 集合) { }如:foreach(DataRow row in dt.rows) { 然后对row 进行操作. }
补充VB的for each语法:For Each element [ As datatype ] In group [ statements ] [ Exit For ] [ statements ] Next [ element ] 各部分说明 element 在 For Each 语句中是必选项。在 Next 语句中是可选项。变量。用于循环访问集合的元素。datatype 如果尚未声明 element,则是必选项。element 的数据类型。group 必选。对象变量。引用要重复 statements 的集合。statements 可选。For Each 和 Next 之间的一条或多条语句,这些语句在 group 中的每一项上运行。Exit For 可选。将控制转移到 For Each 循环外。Next 必选。终止 For Each 循环的定义。
ref:http://msdn2.microsoft.com/zh-cn/library/a4a92379(VS.80).aspx System.Reflection.MemberInfo info = typeof(MyClass); object[] attributes = info.GetCustomAttributes(true); for (int i = 0; i < attributes.Length; i++) { System.Console.WriteLine(attributes[i]); }
http://topic.csdn.net/t/20051116/17/4397951.html
//获取对象类型 Type t = obj.GetType(); //获取类的属性 PropertyInfo[] propertys = t.GetProperties();
foreach (System.Reflection.PropertyInfo pro in propertys) { string fieldName = pro.Name; object fieldValue = pro.GetValue(o,null);
{
}如:foreach(DataRow row in dt.rows)
{
然后对row 进行操作.
}
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
各部分说明
element
在 For Each 语句中是必选项。在 Next 语句中是可选项。变量。用于循环访问集合的元素。datatype
如果尚未声明 element,则是必选项。element 的数据类型。group
必选。对象变量。引用要重复 statements 的集合。statements
可选。For Each 和 Next 之间的一条或多条语句,这些语句在 group 中的每一项上运行。Exit For
可选。将控制转移到 For Each 循环外。Next
必选。终止 For Each 循环的定义。
System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes(true);
for (int i = 0; i < attributes.Length; i++)
{
System.Console.WriteLine(attributes[i]);
}
Type t = obj.GetType();
//获取类的属性
PropertyInfo[] propertys = t.GetProperties();
foreach (System.Reflection.PropertyInfo pro in propertys)
{
string fieldName = pro.Name;
object fieldValue = pro.GetValue(o,null);