(转)c# 扩展方法
擴展方法能夠向現有類型“添加”方法,而無需創建新的派生類型,重新編譯或以其他方式修改原始類型。擴展方法必須是靜態方法,可以像實例方法一樣進行調用。且調用同名中實際定義的方法優先級要高于擴展方法。
先來看看在經常使用List類型中使用擴展方法的例子,首先看看List是如何定義的:
// 摘要: // Represents a strongly typed list of objects that can be accessed by index.// Provides methods to search, sort, and manipulate lists.To browse the .NET// Framework source code for this type, see the Reference Source.// // 類型參數: // T: // The type of elements in the list. [在List的類型定義中我們并沒有看到有定義Union方法的地方,但是當我們在調用的時候就會出現:
<span style="white-space:pre"> </span>/// <summary>/// 通過集合使用 /// </summary> /// <param name="needSearchList"></param> /// <param name="areaid"></param> /// <returns></returns> public List<AreaLineInfoModel> UseSetSearchCollection(List<AreaLineInfoModel> needSearchList, int areaid) { if (needSearchList == null || !needSearchList.Any()) return null; const int area15 = 15; var area15List = new List<AreaLineInfoModel>(); const int area16 = 16; var area16List = new List<AreaLineInfoModel>(); const int area17 = 17; var area17List = new List<AreaLineInfoModel>(); needSearchList.ForEach( m => { if (m.AreaIdList.Contains(area15)) area15List.Add(m); if (m.AreaIdList.Contains(area16)) area16List.Add(m); if (m.AreaIdList.Contains(area17)) area17List.Add(m); }); if (areaid == area15) return area15List.Union(area16List).Union(area17List).ToList(); if (areaid == area16) return area16List.Union(area15List).Union(area17List).ToList(); if (areaid == area17) return area17List.Union(area15List).Union(area16List).ToList(); return null; }其中的Union方法哪里來的呢?我們轉到定義看一看:
namespace System.Linq {public static class Enumerable { …… public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) { if (first == null) throw Error.ArgumentNull("first"); if (second == null) throw Error.ArgumentNull("second"); return UnionIterator<TSource>(first, second, null); } } }so,也就是說,List的一個實例里面可以調用Enumerable里面的Union方法,如果我們不知道有擴展方法這回事的時候,以通常的想法,通過繼承關系來找Union方法,會發現,List并沒有實現Union方法,而且在繼承的接口中也沒有定義Union方法。這就比較納悶了,這不是違背了面向對象的三大基本原則么?此話后說,我們先來自己實現一個擴展方法:
public interface IFreshList<T>{}public static class testjinni { public static IFreshList<TSource> Union<TSource>(this IFreshList<TSource> first, IFreshList<TSource> second) { return second; } } public class MyList<T> : IFreshList<T> { } public class use { public void meth() { var temiList=new MyList<int>(); var mdaidnnf = new MyList<int>(); temiList.Union(mdaidnnf); } }這只是一個簡單的例子,你可以做你自己的擴展方法。
所有對象都能使用擴展:
public static class ExtendExt
{
public static void FuncExt(this object obj)
{
int b = 0;
}
}
msdn是這樣規定擴展方法的:“擴展方法被定義為靜態方法,但它們是通過實例方法語法進行調用的。 它們的第一個參數指定該方法作用于哪個
類型,并且該參數以 this 修飾符為前綴?!蓖ㄋ椎恼f就是,擴展方法跟靜態類的名稱無關,只需要在一個靜態類里面定義一個靜態方法,第一個參數必須this T開頭,
這個T就是一個泛型類型了。
小結:
本質上來說: 擴展方法是破壞原來的層次結構,通過網絡結構加快業務邏輯處理;
擴展方法不改變被擴展類的代碼,不用重新編譯、修改、派生被擴展類;
擴展方法不能訪問被擴展類的私有成員;
擴展方法會被被擴展類的同名方法覆蓋,所以實現擴展方法我們需要承擔隨時被覆蓋的風險;
擴展方法看似實現了面向對象中擴展對修改說不的特性,但是也違背了面向對象的繼承原則,被擴展類的派生類是不能繼承擴展擴展方法的,從而又違背了面向對象的多態性。;
在我們穩定的引用同一個版本的類庫,但是我們沒有該類庫的源代碼,那么我們可以使用擴展方法;但是從項目的可擴展、可維護和版本控制方面來說,都不建議使用擴展方法進行類的擴展。
文章轉載自:https://blog.csdn.net/qin_zhangyongheng/article/details/52469476
轉載于:https://www.cnblogs.com/yeshenmeng/p/9559077.html
總結
以上是生活随笔為你收集整理的(转)c# 扩展方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vim学习笔记(4)帮助与配置
- 下一篇: C#中用WMI实现对驱动的查询