【发布时间】:2021-10-14 15:10:15
【问题描述】:
我正在努力将object 转换为KeyValuePair.Key。在我的方法中,我有签名:
public EstimatorChain<RegressionPredictionTransformer<PoissonRegressionModelParameters>> GetRegressionPipeline(MLContext context, KeyValuePair<Type, object> algorithm)
铸造:(algorithm.Key)algorithm.Value 或 algorithm.Value as algorithm.Key 会抛出异常:
算法是一个变量,但用作类型
如何正确转换为Key 类型?假设Value 是LbfgsPoissonRegressionTrainer something = new LbfgsPoissonRegressionTrainer();
【问题讨论】:
-
这是因为即使该变量的类型为
Type,您也无法将某些内容强制转换为变量。如果您真的想要这样做,那么您需要使用reflection 来完成您想要在object上执行的所有操作 -
我还是不明白,我应该如何反思
Key,假设例如Key是typeof(LbfgsPoissonRegressionTrainer),或者object是LbfgsPoissonRegressionTrainer something = new LbfgsPoissonRegressionTrainer()? -
实际问题是什么?您是否在尝试调用方法并且无法提供参数?还是您想使用 keyValuePair 的键属性来做某事?
标签: c# dictionary casting type-conversion key-value