2017-05-24 139 views
0

我写一个使用的Microsoft Dynamics CRM API来用用CRM给出的EntityCollection获取信息的表格的程序。我的问题是,该实体是由KeyValuePair<string, object>组成,这导致头痛。运行时kvps中的一些对象类型为OptionSetValue,我需要一种实际访问值的方式,因为OptionSetValue需要额外的存取器。转换对象类型Microsoft.Xrm.Sdk.OptionSetValue型

下面是一些例子代码来证明我的问题(“E”是实体):

foreach (KeyValuePair<string, object> thePair in e.Attributes.ToList()) 
{ 
    int theResult = thePair.Value; 
} 

在上面的例子中,程序将编译,但在运行时将抛出厚望,因为它会尝试从转换OptionSetValueint32

这是我想以某种方式完成的:

foreach (KeyValuePair<string, object> thePair in e.Attributes.ToList()) 
{ 
    int theResult = thePair.Value.Value; 
} 

在这种情况下的.Value访问将返回我需要的价值,但因为C#编译器不知道thePairOptionSetValue型的,直到运行时,它将不会编译,因为对象类型没有.Value成员。

任何想法或需要澄清我的问题?

回答

0

它似乎打字出这一切给了我一些清晰度我之后不到5分钟这篇文章修复了这个问题。你可以简单地使用强制转换(OptionSetValue)

foreach (KeyValuePair<string, object> thePair in e.Attributes.ToList()) 
{ 
    int theResult = (OptionSetValue)thePair.Value.Value; 
} 
相关问题