【问题标题】:OptionSetValue behaving like an integerOptionSetValue 表现得像一个整数
【发布时间】:2017-12-11 19:19:23
【问题描述】:

我最近在我的插件的一个方法中遇到了一个 OptionSetValue 表现得像一个整数。以前,与所有其他 OptionSetValues 一起,为了检索整数值,我使用了以下模式:

localIntegerVariable = (new_myEntity.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute")).Value;

这不再适用于插件的方法之一。如果我把它当作一个整数,它就可以工作。

localIntegerVariable = (new_myEntity.GetAttributeValue<int>("new_myOptionSetAttribute"));

奇怪的是,在我检索前图像实体之前,在同一插件的主要部分中,我将相同的属性视为 OptionSetValue 如下所示,它工作得很好。

int incominglocalIntegerVariable = temp.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute") == null ? _OSV_Empty : temp.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute").Value;

我已经验证entities.cs 文件中new_myOptionSetAttribute 的定义是一个OptionSetValue。我还验证了 CRM 中的定义是一个 OptionSet 值。

有人经历过吗?

【问题讨论】:

  • '不再工作'是什么意思?
  • 当它到达这个语句时: localIntegerVarialbe = new_myEntity.GetAttributeValue("new_myOptionSetAttribute ").Value 它返回以下错误。我们将 OptionSetValue.Value (int) 转换为整数,但似乎抱怨我们无法将 int 转换为 OptionSetValue InvalidCastException: Unable to cast object of type 'System.Int32' to type 'Microsoft.Xrm.Sdk.OptionSetValue '
  • 您为 localIntegerVariable 声明的数据类型是什么?

标签: c# plugins dynamics-crm


【解决方案1】:

下面的代码会抛出确切的错误,因为您试图将右侧的 int 值分配给左侧的 OptionSetValue 变量:

InvalidCastException:无法将“System.Int32”类型的对象转换为“Microsoft.Xrm.Sdk.OptionSetValue”类型

OptionSetValue localIntegerVariable;

localIntegerVariable = (new_myEntity.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute")).Value;

在这种情况下,localIntegerVariable 应该是int,因为.Value 将给你int 数据类型结果。

要保持相同的数据类型,请将其更改为

int localIntegerVariable;

localIntegerVariable = (new_myEntity.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute")).Value;

OptionSetValue localIntegerVariable;

localIntegerVariable = new_myEntity.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute");

最后一个例子更好,因为它在使用表达式temp.GetAttributeValue&lt;OptionSetValue&gt;("new_myOptionSetAttribute") == null ? 访问.Value 之前检查null

【讨论】:

  • 已经是这种形式了:int localIntegerVariable; localIntegerVariable = (new_myEntity.GetAttributeValue("new_myOptionSetAttribute")).Value;
  • @THilding 这个属性如何包含在 new_myEntity 中?像 new OptionSetValue(10000) 之类的东西?或来自 Retrieve/Retrievemultiple 或目标实体的 PreImage 或 Entity?
猜你喜欢
  • 2013-07-25
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
  • 2013-04-14
  • 2020-04-17
  • 2012-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多