C# 将对象类型转换为Microsoft.Xrm.Sdk.OptionSetValue类型
我正在编写一个程序,它使用Microsoft Dynamics CRM API用从CRM提供的EntityCollection中获得的信息填充表单。我的问题是实体由C# 将对象类型转换为Microsoft.Xrm.Sdk.OptionSetValue类型,c#,dynamics-crm,crm,C#,Dynamics Crm,Crm,我正在编写一个程序,它使用Microsoft Dynamics CRM API用从CRM提供的EntityCollection中获得的信息填充表单。我的问题是实体由KeyValuePair组成,这让人头疼。kvps中的一些对象在运行时属于OptionSetValue类型,我需要一种实际访问该值的方法,因为OptionSetValue需要额外的访问器 下面是一些示例代码来演示我的问题(“e”是实体): 在这种情况下,.Value访问器将返回我需要的值,但由于C编译器在运行时之前不知道thePair
KeyValuePair
组成,这让人头疼。kvps中的一些对象在运行时属于OptionSetValue
类型,我需要一种实际访问该值的方法,因为OptionSetValue
需要额外的访问器
下面是一些示例代码来演示我的问题(“e”是实体):
在这种情况下,.Value
访问器将返回我需要的值,但由于C编译器在运行时之前不知道thePair
的类型是OptionSetValue
,因此它不会编译,因为对象类型没有.Value
成员
对我的问题有什么想法或需要澄清的吗?似乎键入这些都给了我一些清晰,因为我在这篇文章发表后不到5分钟就解决了这个问题。您可以简单地使用(OptionSetValue)进行类型转换
foreach(e.Attributes.ToList()中的KeyValuePair)
{
int theResult=(OptionSetValue)thePair.Value.Value;
}
foreach (KeyValuePair<string, object> thePair in e.Attributes.ToList())
{
int theResult = thePair.Value;
}
foreach (KeyValuePair<string, object> thePair in e.Attributes.ToList())
{
int theResult = thePair.Value.Value;
}
foreach (KeyValuePair<string, object> thePair in e.Attributes.ToList())
{
int theResult = (OptionSetValue)thePair.Value.Value;
}