国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

.NET擴(kuò)展方法使用實(shí)例詳解

瀏覽:90日期:2022-06-08 14:23:31

擴(kuò)展方法有幾個(gè)必要前提:

  • 擴(kuò)展方法所在的類(lèi)必須是靜態(tài)類(lèi)
  • 擴(kuò)展方法本身必須是靜態(tài)方法
  • 擴(kuò)展方法參數(shù)中,對(duì)類(lèi)型的擴(kuò)展參數(shù)前必須加this關(guān)鍵字

擴(kuò)展基本數(shù)據(jù)類(lèi)型

針對(duì)DateTime類(lèi)型寫(xiě)一個(gè)擴(kuò)展方法。

    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("請(qǐng)輸入正確的出生日期~~");    }    else    {return currentYear - birthYear - 1;    }}    }

客戶端調(diào)用。

    class Program    {static void Main(string[] args){    try    {Console.WriteLine("請(qǐng)輸入您的出生年份");DateTime d = Convert.ToDateTime(Console.ReadLine());DateTime dateInstance = new DateTime();int age = dateInstance.Age(d);Console.WriteLine("您當(dāng)前的年齡是:{0}", age);Console.ReadKey();    }    catch (Exception ex)    {Console.WriteLine(ex.Message);    }}    }

擴(kuò)展接口

有這樣的一個(gè)產(chǎn)品模型。

    public class Product    {public int Id { get; set; }public string Name { get; set; }    }

接口提供獲取產(chǎn)品集合的方法。

    public interface IProductService    {IEnumerable<Product> GetProducts();    }

接口有2個(gè)實(shí)現(xiàn)類(lèi)。

    public class FoodProducts : IProductService    {public IEnumerable<Product> GetProducts(){    return new List<Product>    {new Product(){Id = 1, Name = "餅干"},new Product(){Id = 2, Name = "牛奶"}    };}    }    public class ElectronicProducts : IProductService    {public IEnumerable<Product> GetProducts(){    return new List<Product>    {new Product(){Id = 3, Name = "電風(fēng)扇"},new Product(){Id = 4, Name = "空調(diào)"}    };}    }

針對(duì)接口擴(kuò)展方法。

    public static class ProductServiceExtension    {public static IEnumerable<Product> GetProductsById(this IProductService productService, int id){    return productService.GetProducts().Where(p => p.Id == id);}    }

客戶端調(diào)用。

    class Program    {static void Main(string[] args){    IProductService productService = new FoodProducts();    Console.WriteLine("食物類(lèi)別下總數(shù)量是;{0}", productService.GetProducts().Count());    try    {Console.WriteLine("找到的產(chǎn)品名稱(chēng)是:{0}", (productService.GetProductsById(1).SingleOrDefault()).Name);    }    catch (Exception ex)    {Console.WriteLine(ex.Message);    }    Console.ReadKey();}    }

擴(kuò)展包含私有字段的類(lèi) 使用反射獲取類(lèi)的私有字段

擴(kuò)展一個(gè)類(lèi)的時(shí)候,有時(shí)候會(huì)用到該類(lèi)的私有字段,我們可以通過(guò)反射拿到類(lèi)的私有字段。

有這樣的一個(gè)類(lèi),包含私有字段和公共方法。

    {private DateTime _currentTime;public void SetTime(){    _currentTime = DateTime.Now;}public string GetMsg(){    if (_currentTime.Hour < 12)    {return "上午好~~";    }    else    {return "下午好~~";    }}    }

我們希望擴(kuò)展出一個(gè)顯示英文信息的問(wèn)候。

    public static class DisplayMessageExtensions    {public static string GetLocalMsg(this DisplayMessage message, string country){    //通過(guò)反射拿到私有字段    var privateField = typeof (DisplayMessage).GetField("_currentTime",BindingFlags.Instance | BindingFlags.NonPublic);    //獲取該私有字段的值    var currentDateTime = (DateTime)privateField.GetValue(message);    if (country == "USA" && currentDateTime.Hour < 12)    {return "Good Morning";    }    else    {return "Good Evening";    }}    }

客戶端調(diào)用。

    class Program    {static void Main(string[] args){    DisplayMessage displayMessage = new DisplayMessage();    displayMessage.SetTime();    Console.WriteLine("來(lái)自中國(guó)的問(wèn)候是:{0}", displayMessage.GetMsg());    Console.WriteLine("美國(guó)人怎么問(wèn)候?");    Console.WriteLine("來(lái)自美國(guó)的問(wèn)候是:{0}", displayMessage.GetLocalMsg("USA"));    Console.ReadKey();}    }

擴(kuò)展一個(gè)類(lèi)的私有嵌套類(lèi) 通過(guò)反射

當(dāng)一個(gè)類(lèi)有嵌套私有類(lèi)的時(shí)候,擴(kuò)展該類(lèi)的時(shí)候,有時(shí)候會(huì)用到該類(lèi)的嵌套私有類(lèi),我們可以通過(guò)反射擴(kuò)展私有嵌套類(lèi)。

有這樣的一個(gè)ParentClass類(lèi),包含一個(gè)私有嵌套類(lèi)ChildClass.

    public class ParentClass    {public string MessageFromParent(){    return "from parent~~";}private class ChildClass{    public string MessageFromChild()    {return "from child~";    }}    }

現(xiàn)在要擴(kuò)展這個(gè)私有嵌套類(lèi),為其添加一個(gè)轉(zhuǎn)換成大寫(xiě)的方法,通過(guò)反射來(lái)完成。

    public static class NestedClassExtension    {public static string ToUppeerCaseParentMessage(this ParentClass parent){    return parent.MessageFromParent().ToUpper();}public static string ToUpperCaseChildMessage(this object o){    var childUpper = "";    //通過(guò)反射獲取父類(lèi)中的私有嵌套類(lèi)    var privateClass = typeof (ParentClass).GetNestedType("ChildClass", BindingFlags.NonPublic);    if (o.GetType() == privateClass)    {//通過(guò)反射獲取嵌套私有類(lèi)的方法var callMethod = privateClass.GetMethod("MessageFromChild");childUpper = (callMethod.Invoke(o, null) as string).ToUpper();    }    return childUpper;}    }

客戶端,首先通過(guò)反射獲取私有嵌套類(lèi)的type類(lèi)型,然后運(yùn)用私有嵌套類(lèi)的擴(kuò)展方法。

try{    ParentClass p = new ParentClass();    //通過(guò)反射獲取父類(lèi)私有嵌套類(lèi)    var privateClass = typeof (ParentClass).GetNestedType("ChildClass", BindingFlags.NonPublic);    //通過(guò)反射創(chuàng)建父類(lèi)私有嵌套類(lèi)的實(shí)例    var c = Activator.CreateInstance(privateClass);    //通過(guò)反射獲取父類(lèi)私有嵌套類(lèi)的方法    //var callMethod = privateClass.GetMethod("MessageFromChild");    Console.WriteLine(c.ToUpperCaseChildMessage());}catch (Exception ex){    Console.WriteLine(ex.Message);   }Console.ReadKey();

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

標(biāo)簽: ASP.NET
相關(guān)文章:
主站蜘蛛池模板: 精品国产v无码大片在线观看 | 精品国产一区二区二三区在线观看 | 交性视频免费看 | 成人亚洲精品7777 | 精品综合久久久久久蜜月 | 国产精品三级在线播放 | 欧美黄网站 | 美国一级片在线观看 | 亚洲综合第一欧美日韩中文 | 日韩视频欧美视频 | 国产性较精品视频免费 | 亚洲网视频 | 欧美一级看片免费观看视频在线 | 欧美激情精品久久久久久久九九九 | 丰满老熟女毛片 | 精品国产91久久久久 | 一区二区三区中文 | 日韩精品久久久免费观看夜色 | 经典三级久久久久 | 久久精品视频8 | 亚洲免费在线看 | 99视频在线免费看 | 久久久久免费 | 精品国产福利 | 国产一在线 | 亚洲精品98久久久久久中文字幕 | 国产精品久久国产三级国不卡顿 | 国产伦精一区二区三区 | 国产手机在线视频放线视频 | 国产精品久久久亚洲 | 欧美成人h | 久久精品福利视频在线观看 | 男女扒开双腿猛进入免费网站 | 中国二级毛片 | 成在线人永久免费播放视频 | 久久婷五月天 | 日日噜噜噜夜夜爽爽狠狠69 | 免费日韩在线视频 | 豆国产97在线 | 亚洲 | 爽爽爽爽爽爽爽成人免费观看 | 亚洲三区视频 |