如何正确转换一个对象到KeyValuePair.Key?



我正在努力将object转换为KeyValuePair.Key。在我的方法中,我有签名:

public EstimatorChain<RegressionPredictionTransformer<PoissonRegressionModelParameters>> GetRegressionPipeline(MLContext context, KeyValuePair<Type, object> algorithm)

和cast:(algorithm.Key)algorithm.Valuealgorithm.Value as algorithm.Key抛出一个异常:

算法是一个变量,但像类型一样使用

如何正确转换为Key类型?设ValueLbfgsPoissonRegressionTrainer something = new LbfgsPoissonRegressionTrainer();

我的问题有两个解决方案。

首先是对EstimatorChain<RegressionPredictionTransformer<PoissonRegressionModelParameters>>类型进行强制转换:

(IEstimator<RegressionPredictionTransformer<PoissonRegressionModelParameters>>)algorithm.Value
另一个是使用var value = Convert.ChangeType(item.Value, item.Key);:
.Append((dynamic)Convert.ChangeType(algorithm.Value, algorithm.Key));

最新更新