搜索

查看: 3086|回复: 11

[ASP.NET] .NET扩展方法使用实例详解

[复制链接]
发表于 2023-5-4 11:32:55 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:32:55 3086 11 看全部
扩展方法有几个必要前提:
  • 扩展方法所在的类必须是静态类
  • 扩展方法本身必须是静态方法
  • 扩展方法参数中,对类型的扩展参数前必须加this关键字
    扩展基本数据类型
    针对DateTime类型写一个扩展方法。
        public static class CalculateAge
        {
            public static int Age(this DateTime date, DateTime birthDate)
            {
                int birthYear = birthDate.Year;
                int currentYear = DateTime.Now.Year;
                if (birthYear >= currentYear)
                {
                    throw new Exception("请输入正确的出生日期~~");
                }
                else
                {
                    return currentYear - birthYear - 1;
                }
            }
        }
    客户端调用。
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("请输入您的出生年份");
                    DateTime d = Convert.ToDateTime(Console.ReadLine());
                    DateTime dateInstance = new DateTime();
                    int age = dateInstance.Age(d);
                    Console.WriteLine("您当前的年龄是:{0}", age);
                    Console.ReadKey();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

    2022813115309501.png

    2022813115309501.png


    扩展接口
    有这样的一个产品模型。
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    接口提供获取产品集合的方法。
        public interface IProductService
        {
            IEnumerable GetProducts();
        }
    接口有2个实现类。
        public class FoodProducts : IProductService
        {
            public IEnumerable GetProducts()
            {
                return new List
                {
                    new Product(){Id = 1, Name = "饼干"},
                    new Product(){Id = 2, Name = "牛奶"}
                };
            }
        }
        public class ElectronicProducts : IProductService
        {
            public IEnumerable GetProducts()
            {
                return new List
                {
                    new Product(){Id = 3, Name = "电风扇"},
                    new Product(){Id = 4, Name = "空调"}
                };
            }
        }
    针对接口扩展方法。
        public static class ProductServiceExtension
        {
            public static IEnumerable GetProductsById(this IProductService productService, int id)
            {
                return productService.GetProducts().Where(p => p.Id == id);
            }
        }
    客户端调用。
        class Program
        {
            static void Main(string[] args)
            {
                IProductService productService = new FoodProducts();
                Console.WriteLine("食物类别下总数量是;{0}", productService.GetProducts().Count());
                try
                {
                    Console.WriteLine("找到的产品名称是:{0}", (productService.GetProductsById(1).SingleOrDefault()).Name);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadKey();
            }
        }

    2022813115309502.png

    2022813115309502.png


    扩展包含私有字段的类 使用反射获取类的私有字段
    扩展一个类的时候,有时候会用到该类的私有字段,我们可以通过反射拿到类的私有字段。
    有这样的一个类,包含私有字段和公共方法。
        {
            private DateTime _currentTime;
            public void SetTime()
            {
                _currentTime = DateTime.Now;
            }
            public string GetMsg()
            {
                if (_currentTime.Hour
    我们希望扩展出一个显示英文信息的问候。
        public static class DisplayMessageExtensions
        {
            public static string GetLocalMsg(this DisplayMessage message, string country)
            {
                //通过反射拿到私有字段
                var privateField = typeof (DisplayMessage).GetField("_currentTime",
                    BindingFlags.Instance | BindingFlags.NonPublic);
                //获取该私有字段的值
                var currentDateTime = (DateTime)privateField.GetValue(message);
                if (country == "USA" && currentDateTime.Hour
    客户端调用。
        class Program
        {
            static void Main(string[] args)
            {
                DisplayMessage displayMessage = new DisplayMessage();
                displayMessage.SetTime();
                Console.WriteLine("来自中国的问候是:{0}", displayMessage.GetMsg());
                Console.WriteLine("美国人怎么问候?");
                Console.WriteLine("来自美国的问候是:{0}", displayMessage.GetLocalMsg("USA"));
                Console.ReadKey();
            }
        }

    2022813115309503.png

    2022813115309503.png


    扩展一个类的私有嵌套类 通过反射
    当一个类有嵌套私有类的时候,扩展该类的时候,有时候会用到该类的嵌套私有类,我们可以通过反射扩展私有嵌套类。
    有这样的一个ParentClass类,包含一个私有嵌套类ChildClass.
        public class ParentClass
        {
            public string MessageFromParent()
            {
                return "from parent~~";
            }
            private class ChildClass
            {
                public string MessageFromChild()
                {
                    return "from child~";
                }
            }
        }
    现在要扩展这个私有嵌套类,为其添加一个转换成大写的方法,通过反射来完成。
        public static class NestedClassExtension
        {
            public static string ToUppeerCaseParentMessage(this ParentClass parent)
            {
                return parent.MessageFromParent().ToUpper();
            }
            public static string ToUpperCaseChildMessage(this object o)
            {
                var childUpper = "";
                //通过反射获取父类中的私有嵌套类
                var privateClass = typeof (ParentClass).GetNestedType("ChildClass", BindingFlags.NonPublic);
                if (o.GetType() == privateClass)
                {
                    //通过反射获取嵌套私有类的方法
                    var callMethod = privateClass.GetMethod("MessageFromChild");
                    childUpper = (callMethod.Invoke(o, null) as string).ToUpper();
                }
                return childUpper;
            }
        }
    客户端,首先通过反射获取私有嵌套类的type类型,然后运用私有嵌套类的扩展方法。
    try
    {
        ParentClass p = new ParentClass();
        //通过反射获取父类私有嵌套类
        var privateClass = typeof (ParentClass).GetNestedType("ChildClass", BindingFlags.NonPublic);
        //通过反射创建父类私有嵌套类的实例
        var c = Activator.CreateInstance(privateClass);
        //通过反射获取父类私有嵌套类的方法
        //var callMethod = privateClass.GetMethod("MessageFromChild");
        Console.WriteLine(c.ToUpperCaseChildMessage());
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);   
    }
    Console.ReadKey();

    2022813115309504.png

    2022813115309504.png


    以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对知鸟论坛的支持。如果你想了解更多相关内容请查看下面相关链接
  • 回复

    使用道具 举报

    发表于 2023-6-28 17:41:54 | 显示全部楼层
    麻辣鸡翅 2023-6-28 17:41:54 看全部
    我看不错噢 谢谢楼主!知鸟论坛越来越好!
    回复

    使用道具 举报

    发表于 2023-6-28 20:38:55 | 显示全部楼层
    冀苍鸾 2023-6-28 20:38:55 看全部
    这个帖子不回对不起自己!我想我是一天也不能离开知鸟论坛
    回复

    使用道具 举报

    发表于 2023-6-29 15:30:31 | 显示全部楼层
    术数古籍专卖疤 2023-6-29 15:30:31 看全部
    楼主,我太崇拜你了!我想我是一天也不能离开知鸟论坛
    回复

    使用道具 举报

    发表于 2023-6-29 15:35:14 | 显示全部楼层
    惜颜705 2023-6-29 15:35:14 看全部
    楼主,大恩不言谢了!知鸟论坛是最棒的!
    回复

    使用道具 举报

    发表于 2023-6-29 21:15:25 | 显示全部楼层
    dxf17 2023-6-29 21:15:25 看全部
    感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
    回复

    使用道具 举报

    发表于 2023-6-30 09:21:26 | 显示全部楼层
    123456848 2023-6-30 09:21:26 看全部
    我看不错噢 谢谢楼主!知鸟论坛越来越好!
    回复

    使用道具 举报

    发表于 2023-6-30 14:09:42 | 显示全部楼层
    音乐之家1 2023-6-30 14:09:42 看全部
    其实我一直觉得楼主的品味不错!呵呵!知鸟论坛太棒了!
    回复

    使用道具 举报

    发表于 2023-6-30 19:53:14 | 显示全部楼层
    123456865 2023-6-30 19:53:14 看全部
    楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
    回复

    使用道具 举报

    发表于 2023-7-1 02:49:49 | 显示全部楼层
    知足常乐77 2023-7-1 02:49:49 看全部
    这个帖子不回对不起自己!我想我是一天也不能离开知鸟论坛
    回复

    使用道具 举报

    • 您可能感兴趣
    点击右侧快捷回复 【请勿灌水】
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则 返回列表

    RSS订阅| SiteMap| 小黑屋| 知鸟论坛
    联系邮箱E-mail:zniao@foxmail.com
    快速回复 返回顶部 返回列表