第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通用List <>類型的擴(kuò)展方法

通用List <>類型的擴(kuò)展方法

C#
翻閱古今 2021-05-12 18:34:16
簡化method<returnType, ListType>(this List<ListType>, ...)到method<returnType>(this List<anyType>, ...)了泛型列表我正在尋找一種擴(kuò)展方法,該方法允許我獲取(任何類型的對象)列表中所有屬性“ P”的值到目前為止,我已經(jīng)可以使用這種方法了:public static T[] selectAll<T, U>(this List<U> list, string property){    List<T> r = new List<T>();          // prepare a list of values of the desired type to be returned    foreach(object o in list)    {        Type mt = o.GetType();          // what are we actually dealing with here? (List of what?)   <-- This should be the same as Type U        IList<PropertyInfo> props = new List<PropertyInfo>(mt.GetProperties());          // Get all properties within that type        foreach(PropertyInfo p in props)        {            if (p.Name == property)                   // Are we looking for this property?                r.Add((T)p.GetValue(o, null));        // then add it to the list to be returned        }    }    return r.ToArray();}因?yàn)槟荒芎唵蔚負(fù)碛形粗姆祷仡愋?,所以我理解有必要在方法調(diào)用中指出返回類型,例如:List<Control> SomeListOfControls = ...string[] s = SomeListOfControls.selectAll<細(xì)繩, Control>("Text");但是,由于該列表中項(xiàng)目的類型與該方法無關(guān),因此我想從方程式中消除Type變量。我希望我可以簡單地打電話List<Control> SomeListOfControls = ...string[] s = SomeListOfControls.selectAll<string>("Text"); <-您知道該列表由什么組成>>。例如。但我想不出一種方法來做到這一點(diǎn)。即使在編譯之前,我也可以看到public static T[] selectAll<T>(this List<> list, string property)是Unexpected use of an unbound generic name(含義List<>)。并且List<object>不能注冊為各種擴(kuò)展名List<?extends object>,可以這么說。如果可能的話,我該如何做呢?PS:似乎可能存在一種“本機(jī)”方式(通常是.net甚至C#),該方式是從可能具有T型屬性P的集合中檢索<T> P的集合-我無法弄清楚使用select和所有...但是如果有的話,我很樂意了解:)
查看完整描述

2 回答

?
胡說叔叔

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個贊

像你看起來是在尋找非通用版本的參數(shù)-無論是IList或IEnumerable將工作


public static T[] selectAll<T>(this IList list, string property){

    ...

}


查看完整回答
反對 回復(fù) 2021-05-21
?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個贊

后果(哪里出了問題)


雖然我都輸入了(using)


System.Collections.Generic



System.Collections.Specialized,


我不知道這些命名空間部分繼承的類實(shí)際上在中System.Collections。我以為我要得到兩半蛋糕,而我卻得到兩個餡而又沒有外殼。


因此,例如,當(dāng)我嘗試使用IEnumerable時,我親愛且可信賴的IDE(Visual Studio 2017)在沒有Type指示器的情況下不會接受它。


對于通過Google遇到同樣問題的任何人:


同時使用這兩種方法.Generic并.Specialized不會覆蓋您,集合類型的很多借鑒都來自父級System.Collections。


public static T[] selectAll<T>(this IList list, string property){

    ...

}


public static T[] selectAll<T>(this IEnnumerable list, string property){

    ...

}

可能會為您服務(wù)。


雖然,對于我上面概述的情況,


public static T[] selectAll<T>(this IEnumerable<object> list, string property){

    ...

}

效果也一樣(而未IList<object>注冊為擴(kuò)展名List<?>


空間和概念的命名可能會引起誤解:


我會認(rèn)為IList<Type>是專門的(按類型)和IList泛型的(因?yàn)樗m用于所有<Type>s)-但是在C#世界中,這是相反的:IList本身被認(rèn)為是“非泛型的”-泛型的問題...內(nèi)在性是從外部(在上下文中如何處理)而不是從內(nèi)部(它包含(或可以包含)什么)來處理-像我這樣的高級程序員可能會直覺上出錯。


總之:

  • System.Collections包含的不僅是其類的總和

  • 直觀地將泛型集合稱為非泛型集合,因?yàn)檫@樣說的是較低級別的能力

  • System.Collections.IEnumerable 似乎適用于各種列表

  • 閱讀官方文檔通常很有用。(等等...一般還是專門?哦,誰知道了:P)


查看完整回答
反對 回復(fù) 2021-05-21
  • 2 回答
  • 0 關(guān)注
  • 273 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號