日期:2014-05-20  浏览次数:20764 次

贴一个Linq使用小例子
linq提供了lamd表达方法,我使用它已经是家常便饭。这里show两端代码。

在我的一个测试驱动引擎系统中,它首先要检查当前应用程序域中的程序集,把那些有可能包含测试程序的(他们都引用了包含测试程序的程序集的dll)挑出来。之后,它要便利这些assembly,查找各个class,找出所有没有参数没有返回值并且声明为static的方法,这些方法还应该贴有RunTestAttribute这个标签,这两段程序我可以及这样简单地写出:

C# code

    public class TestRunner
    {
        private List<Assembly> _assemblies = new List<Assembly>();

        private List<Pair> _TestProcs = null;

        /// <summary>
        /// 从测试程序集中找出的测试用例。可以通过修改此集合内容自定义测试用例。
        /// </summary>
        public List<Pair> TestProcs
        {
            get
            {
                if (_TestProcs == null)
                {
                    _TestProcs = new List<Pair>();
                    _assemblies.ForEach(asm =>
                        asm.GetTypes()
                        .ForEach(t => t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
                        .Where(m => m.ReturnParameter.ParameterType == typeof(void) && m.GetParameters().Length == 0)
                        .ForEach(m =>
                        {
                            var att = m.GetCustomAttributes(typeof(RunTestAttribute), false);
                            if (att.Length != 0)
                            {
                                RunTestAttribute attr = att[0] as RunTestAttribute;
                                if (attr.TimeStamp >= this.MinTime && attr.TimeStamp <= this.MaxTime)
                                    for (var n = 0; n < attr.实例数 * this.FuckTimes; n++)
                                        TestProcs.Add(new Pair()
                                        {
                                            Proc = (ThreadStart)Delegate.CreateDelegate(typeof(ThreadStart), m),
                                            Attr = attr
                                        });
                            }
                        })));
                }
                return _TestProcs;
            }
        }

        public TestRunner()
        {
            var thisAsmName = Assembly.GetExecutingAssembly().GetName().Name;
            AppDomain.CurrentDomain.GetAssemblies()
                .Where(asm => asm.GetReferencedAssemblies().Any(a => a.Name == thisAsmName))
                .ForEach(asm => _assemblies.Add(asm));
        }

.................



大家都知道,Where这个Linq方法它并不是创建另一数据或者List,而是使用c#枚举器方法一次仅返回一个结果,因此上述所有对IEnumable<T>的操作其实都是“狗熊掰棒子”一样处理内存对象创建,而不是生成大量数据集合。所以我觉得Linq大多数方法用的越多则越是能从枚举代替数据的编程风格中获得性能提高。

另外,ForEach我是这样写的:
C# code

        static public void ForEach<T>(this IEnumerable<T> array, Action<T> action)
        {
            foreach (T x in array)
            {
                action(x); ;
            }
        }



我其实就是这样写我的代码的,使用lamda表达方式这已经成了我的习惯。

------解决方案--------------------
UP
坐在沙发上支持lz
------解决方案--------------------
linq很好很强大,n多地方都能用,
------解决方案--------------------
好是精通啊,慢慢来品味吧
------解决方案--------------------
希望能有更多像sp1234的高手来这个版块show一下
------解决方案--------------------
能不能出个VB的代码

谢谢
------解决方案--------------------
我其实就是这样写我的代码的,使用lamda表达方式这已经成了我的习惯。 -----努力方向!
------解决方案--------------------
用到反射吗?linq没有用过.反射倒看出了