我正在努力将object
转换为KeyValuePair.Key
。在我的方法中,我有签名:
public EstimatorChain<RegressionPredictionTransformer<PoissonRegressionModelParameters>> GetRegressionPipeline(MLContext context, KeyValuePair<Type, object> algorithm)
和cast:(algorithm.Key)algorithm.Value
或algorithm.Value as algorithm.Key
抛出一个异常:
算法是一个变量,但像类型一样使用
如何正确转换为Key
类型?设Value
为LbfgsPoissonRegressionTrainer 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));