亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

創建從 ToDictionary() 派生的擴展方法

創建從 ToDictionary() 派生的擴展方法

C#
MYYA 2023-09-16 20:06:33
我想創建一個源自ToDictionary(). 目前為了達到預期的結果,我這樣做:ObjectAttributes = model.ObjectAttributes.ToDictionary(    oa => oa.Attribute.Name, oa => oa.ToWrapper<ObjectAttributeWrapper>());所以我使用以下ToDictionary簽名:public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(    this IEnumerable<TSource> source,    Func<TSource, TKey> keySelector,    Func<TSource, TElement> elementSelector);我想知道是否可以這樣做?ObjectAttributes = model.ObjectAttributes.ToDictionaryWrapper<ObjectAttributeWrapper>(    oa => oa.Attribute.Name);這是當前的實現,但顯然不起作用:public static Dictionary<TKey, TWrapper> ToDictionaryWrapper<TWrapper, TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) where TSource : BaseModel where TWrapper : IBaseWrapper{        return source.ToDictionary(keySelector, model => model.ToWrapper<TWrapper>());}編輯:實施ToWrapper():public static TWrapper ToWrapper<TWrapper>(this BaseModel model) where TWrapper : IBaseWrapper    {        if (model == null)            return default;        var type = typeof(TWrapper);        if (_wrapperParents.ContainsKey(type) && _wrapperParents[type].Id == model.Id)            return (TWrapper)_wrapperParents[type];        else            return (TWrapper)GetConstructor<TWrapper>().Invoke(new object[] { model });    }    public static IEnumerable<TWrapper> ToListWrapper<TWrapper>(this IEnumerable models) where TWrapper : IBaseWrapper    {        var _models = models as IEnumerable<BaseModel>;        if (_models == null)            return default;        return _models.Select(model => model.ToWrapper<TWrapper>());    }
查看完整描述

1 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

通常,對于單個通用約束列表,您只能顯式提供所有參數或自動解析所有參數。您不能部分提供約束。

所以你的選擇如下(我假設TWrapper會有一個屬性WrappedObject):

  • 按原樣提供所有參數

  • 減少泛型參數的數量(如果密鑰類型始終相同,您甚至可以將其設置為非泛型)

// signature

Dictionary<TKey, TWrapper> ToDictionaryWrapper<TWrapper, TKey>(

    this IEnumerable<BaseModel> source,

    Func<BaseModel, TKey> keySelector)

    where TWrapper : IBaseWrapper;

// call

model.ObjectAttributes

    .ToDictionaryWrapper<ObjectAttributeWrapper, string>(oa => oa.Attribute.Name);

將函數調用分為兩部分,一部分是顯式的,另一部分是隱式的

// signature

IEnumerable<TWrapper> Wrap<TWrapper>(this IEnumerable<BaseModel> source)

    where TWrapper : IBaseWrapper;


Dictionary<TKey, TWrapper> ToDictionaryWrapper<TWrapper, TKey>(

    this IEnumerable<TWrapper> source,

    Func<BaseModel, TKey> keySelector)

    where TWrapper : IBaseWrapper;

// call

model.ObjectAttributes

    .Wrap<ObjectAttributeWrapper>()

    .ToDictionaryWrapper(oa => oa.Attribute.Name);

不用擔心自定義ToDictionaryWrapper,只需使用 Wrap-Function 和 FrameworkToDictionary

// call (signature for Wrap same as above)

model.ObjectAttributes

    .Wrap<ObjectAttributeWrapper>()

    .ToDictionary(w => w.WrappedObject.Attribute.Name);


查看完整回答
反對 回復 2023-09-16
  • 1 回答
  • 0 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號