• Linq--扩展方法


    如果现在有一个这样的需求,求筛选出来的大于20MB的进程的和,常用的方法是写一个静态方法传进去一个ProcessData列表

    比如:

     public static Int64 TotalMemory( IEnumerable<ProcessData> process)

            {

                Int64 result = 0;

                foreach(var pro in  process)

                {

                    result +=pro.Memory;

                }

                return result;

            }

    如果现在还有一个需求:将筛选出来的结果用MB表示

    比如:

    Public static Int64  ByteToMegaByte(Int64 bytes)

    {

        return bytes/1024/1024;

    }

    我们就要如此来使用了:ByteToMegaByte(TotalMemory())

     

    好像这样做也很完美,但是.net有更好的方法——使用扩展方法。使用扩展发法就好像此方法真的"添加"到IEnumable<ProcessData>中一样。

    修改类Programe为静态方法

    注意:

          1. 扩展方法必须在非泛型的静态类中

          2.扩张方法的第一参数必须为带扩张的方法类型,并且用this来修饰

          3.扩展方法接受任意多参数

    修改后的代码如下:

     public static Int64 TotalMemory( this IEnumerable<ProcessData> process)

            {

                Int64 result = 0;

                foreach(var pro in  process)

                {

                    result +=pro.Memory;

                }

                return result;

            }

            public static Int64 ByteToMegaByte(this IEnumerable<ProcessData> process, Int64 bytes)//扩张方法的第一参数必须为带扩张的方法类型,并且用this来修饰

            {

                return bytes / 1024 * 1024;

            }

     public static void LambdaDemoDelegate(Predicate<Process> match)

            {

                var process = new List<ProcessData>();

                Int64 res = 0;

                foreach (var pro in Process.GetProcesses())

                {

                    if (match(pro))

                    {

                        process.Add(new ProcessData { id = pro.Id, Name = pro.ProcessName, Memory = pro.WorkingSet64 });

                        res = process.TotalMemory();

                        res = process.ByteToMegaByte(res);

                       

                    }

                }

                Array.ForEach(process.ToArray(),c=>Console.Write("id为:"+c.id+"进程名为:"+c.Name+"进程大小为:"+c.Memory+" "));

               

                Console.WriteLine("筛选后的进程总共占用的内存:"+res+"MB");

           

            } 

     

    如图中所示:ByteToMegaByte就像是本来Ienumberable<ProcessData>就有的方法,而且执行res=process.TotalMemory();

    Res=process.ByteToMegaByte(res);

    这样的“链式”操作。不再用ByteToMegaByte(TotalMemory(process))这样的操作了。

    测试结果:

    完整代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace LinqDemo
    {
       public  class ProcessData
        {
           public int id { get; set; }  
         
           public string Name { get; set; }
           public Int64 Memory { get; set; }
        }

      
    }

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Xml.Linq;

    using System.Xml;

    using System.Diagnostics;

    namespace LinqDemo

    {

       static  class Program

        {

            static void Main(string[] args)

            {

                      

            }

            public static void LambdaDemoGeneral()

            {

                var process = new List<ProcessData>();

                foreach(var pro in Process.GetProcesses())

                {

                    if(pro.WorkingSet64>=20*1024*1024)

                    {

                        process.Add(new ProcessData {id=pro.Id,Name=pro.ProcessName,Memory=pro.WorkingSet64});

                    }

                }

                Array.ForEach(process.ToArray(), c => Console.Write("id为:" + c.id + "进程名为:" + c.Name + "进程大小为:" + c.Memory + " "));

            }

            public static bool Filter(Process process)

            {

                return process.WorkingSet64 >= 20*1024*1024;

            }

            public static void LambdaDemoDelegate(Predicate<Process> match)

            {

                var process = new List<ProcessData>();

                Int64 res = 0;

                foreach (var pro in Process.GetProcesses())

                {

                    if (match(pro))

                    {

                        process.Add(new ProcessData { id = pro.Id, Name = pro.ProcessName, Memory = pro.WorkingSet64 });

                        res = process.TotalMemory();

                        res = process.ByteToMegaByte(res);

                       

                       

                    }

                }

                Array.ForEach(process.ToArray(),c=>Console.Write("id为:"+c.id+"进程名为:"+c.Name+"进程大小为:"+c.Memory+" "));

               

                Console.WriteLine("筛选后的进程总共占用的内存:"+res+"MB");

           

            }

            public static void DelegateDemo()

            {

                LambdaDemoDelegate(Filter);

            }

            public static void LambdaDemo()

            {

                LambdaDemoDelegate(process=>process.WorkingSet64>=20*1000*1000);

            }

            public static Int64 TotalMemory( this IEnumerable<ProcessData> process)

            {

                Int64 result = 0;

                foreach(var pro in  process)

                {

                    result +=pro.Memory;

                }

                return result;

            }

            public static Int64 ByteToMegaByte(this IEnumerable<ProcessData> process,Int64 bytes)

            {

                return bytes / 1024 * 1024;

            }

        }

    }

  • 相关阅读:
    设计模式4---策略模式
    设计模式3---抽象工厂模式
    209. Minimum Size Subarray Sum(双指针)
    174. Dungeon Game(动态规划)
    二分查找
    74. Search a 2D Matrix(二分查找,剑指offer 1)
    81. Search in Rotated Sorted Array II
    33. Search in Rotated Sorted Array(二分查找)
    34. Search for a Range (二分查找)
    35. Search Insert Position(二分查找)
  • 原文地址:https://www.cnblogs.com/flyaway100/p/3465495.html
Copyright © 2020-2023  润新知