C# foreach的实现 foreach(new {Name=""} s in collection)

目的就是为了实现类似的写法

foreach(new {Name=String.Empty} Ele in (IEnumrable)Collection)

{

      // 要执行的操作...

}

 

适用场景... 

 var datas=from student in StudentCollection select  new{studentName=student.Name };

把datas存入ViewState中,若在一个方法内 则可以直接 foreach...

 

问题就出现在别的页面在此访问ViewState就无法使用foreach(var ele ...)了

 

具体请查看

http://topic.csdn.net/u/20100820/01/ef01ef42-8c67-4901-a039-5dcf0d0fdf00.html

 

最后看了大家的回答,自己思考后...实现了一个简单的方法...还是反射

 

用起来方便了,稍加改进效果应该还是不错的

 

第一步,声明一个类(相当于DataSourceTemplate):

   public class Stu
    {
        public Stu() { }

         //这里我只要3个属性就行了
         public Stu(String name,String mail,String address)
        {
            this.Name = name;
          
            this.Mail = mail;
           
            this.Address = address;
        }

        public String Name { get; set; }

        public String Mail { get; set; }

        public String Address { get; set; }

        public String Phone { get; set; }
    }

 

第二步 狂加数据:

 

            List< Stu> larr = new List< Stu>();
          
            larr.Add(new Stu("联想", "[email protected]", "中国"));
           
            larr.Add(new Stu("神州", "[email protected]", "美国"));
           
            larr.Add(new Stu("惠普", "[email protected]", "俄罗斯"));
           
            larr.Add(new Stu("苹果", "[email protected]", "印度"));

 

第三步 开始调用方法:

 

           CustomForeach customEach = new CustomForeach();       

 

           customEach.ForeachOfClass(new { Name="", Mail = "", Address = "" }, larr,
                (propertyName, originValue,currentValue) =>
            {
                 Response.Write(String.Format("{0}={1}", propertyName, currentValue));

                 //获取当前数据集合的元素索引 customEach.Elementindex

                 //获取当前数据集合的元素第几个属性索引 customEach.PropertyIndex

            });

 

整个使用过程结束...

 

 

CustomForeach源码如下:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Reflection; using System.Collections; using System.Collections.Specialized; namespace WebControlTestApp { public class CustomForeach { ///

/// 元素索引 /// public Int32 Elementindex { get; private set; } /// /// 成员索引 /// public Int32 PropertyIndex { get; private set; } /// /// 通过匿名类遍历当前数据集合 /// public void ForeachOfClass(object obj, IEnumerable collection, Action action) { ResetIndex(); PropertyInfo[] properties = obj.GetType().GetProperties(); foreach (Object p in collection) { foreach (PropertyInfo pinfo in properties) { Object originValue = pinfo.GetValue(pinfo, null); PropertyInfo CollectionPropertyOfCurrent = p.GetType().GetProperty(pinfo.Name); Object CurrentPValue = CollectionPropertyOfCurrent.GetValue(CollectionPropertyOfCurrent, null); action.Invoke(pinfo.Name,originValue, CurrentPValue); PropertyIndex++; } Elementindex++; } } /// /// 通过一个字符串数组遍历当前数据集合 /// /// 一个字符型的数组 如:new String[1]{"Name"} /// 实现了IEnumrable的集合 如:泛型List /// 每次遍历集合要进行的操作 相当于 /// foreach(....)后 /// { 这里您需要的操作... } /// 该匿名委托有两个参数 分别为:属性名称、当前属性值 /// public void ForeachOfString(String[] propertyNames, IEnumerable collection, Action action) { ResetIndex(); foreach (Object p in collection) { foreach (String name in propertyNames) { PropertyInfo Pinfos = p.GetType().GetProperty(name); action.Invoke(name, Pinfos.GetValue(p, null)); PropertyIndex++; } Elementindex++; } } private void ResetIndex() { Elementindex = 0; PropertyIndex = 0; } } }

 

你可能感兴趣的:(学习笔记,.Net,Fromawork,学习,c#,ASP.NET.AJAX)