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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在運(yùn)行時(shí)反映接口類型<t>

如何在運(yùn)行時(shí)反映接口類型<t>

C#
慕尼黑8549860 2021-07-07 13:10:33
我有多個(gè)數(shù)據(jù)點(diǎn)和每個(gè)相關(guān)的數(shù)據(jù)處理器。public interface IDataPointProcessor<T> where T : DataPointInputBase{    DataPointOutputBase GetOutput(T input);}我從文件加載數(shù)據(jù)點(diǎn)列表,并希望使用其單個(gè)關(guān)聯(lián)處理器處理它們。foreach (DataPointInputBase item in input.DataPoints){    //assuming item coming in is of type 'X' how do I get correct processor    var type = typeof(IDataPointProcessor<X>);    var types = AppDomain.CurrentDomain.GetAssemblies()                .SelectMany(s => s.GetTypes())                .Where(p => type.IsAssignableFrom(p) && !p.IsAbstract);    IDataPointProcessor<X> matchedType = ??}我如何解決“X”以便我可以實(shí)例化它并處理輸入?更新 #1 結(jié)合下面來自 Slava 和 Lucky 的答案,我得到以下結(jié)果,但它引發(fā)了一個(gè)異?!皩?duì)象與目標(biāo)類型不匹配?!?即使在調(diào)試器中看起來一切都很好。是否可以轉(zhuǎn)換為 IDataPointProcessor<> 并干凈地調(diào)用接口方法,即: instance.GetOutput(item);foreach (DataPointInputBase item in input.DataPoints){    Type typeGenArg = item.GetType();    Type typeInterfaceGen = typeof(IDataPointProcessor<>).MakeGenericType(typeGenArg);    Type type = AppDomain.CurrentDomain.GetAssemblies()        .SelectMany(x => x.GetTypes())        .Where(x => typeInterfaceGen.IsAssignableFrom(x) && !x.IsAbstract)        .FirstOrDefault();    Type genericType = typeof(IDataPointProcessor<>);    Type dependedGenericType = genericType.MakeGenericType(typeof(DataPointInputBase));    var method = dependedGenericType.GetMethod("GetOutput");    var instance = Activator.CreateInstance(type);    //currently throws:System.Reflection.TargetException: 'Object does not match target type.'    var result = method.Invoke(instance, new object[] { item });    //Ideally I want to do this and avoid the magic strings etc    //var temp_output = instance.GetOutput(item);}
查看完整描述

2 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

首先,你應(yīng)該像這樣使你的界面協(xié)變。


public interface IDataPointProcessor<in T> where T : DataPointInputBase

{

    DataPointOutputBase GetOutput(T input);

}

您應(yīng)該檢索由 實(shí)現(xiàn)的類型IDataPointProcessor<>,然后您應(yīng)該創(chuàng)建檢索類型的實(shí)例并調(diào)用泛型類型的方法。


Type genericType = typeof(IDataPointProcessor<>);

var types = AppDomain.CurrentDomain.GetAssemblies()

    .SelectMany(s => s.GetTypes())

    .Where(p => genericType.IsAssignableFrom(p) && !p.IsAbstract).ToList();



var dependedGenericType = genericType.MakeGenericType(typeof(DataPointInputBase));

var method = dependedGenericType.GetMethod("GetOutput");

var instance = Activator.CreateInstance(types[0]);

method.Invoke(instance, new object[] { new DataPointInputBase() });


查看完整回答
反對(duì) 回復(fù) 2021-07-11
  • 2 回答
  • 0 關(guān)注
  • 205 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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