2 回答

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() });
- 2 回答
- 0 關(guān)注
- 205 瀏覽
添加回答
舉報(bào)