OptionSetValue表现为整数 [英] OptionSetValue behaving like an integer

查看:218
本文介绍了OptionSetValue表现为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在插件的方法中遇到了一个OptionSetValue,它的行为类似于整数.以前,与所有其他OptionSetValues一样,我使用以下模式来检索整数值:

I recently experienced an OptionSetValue behaving like an integer in a method of my plugin. Previously, and with all other OptionSetValues, to retrieve the integer value, I have used the pattern:

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

在插件的方法之一中不再起作用.如果我将其视为整数,则可以使用.

That no longer works in the one of the methods of the plugin. If i treat it like an integer, it works.

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

奇怪的是,在检索前映像实体之前,在同一个插件的主要部分中,我将相同的属性视为如下所示的OptionSetValue,并且效果很好.

Strangely enough, in the main section of the same plugin before I retrieve the pre image entity, I treat the same attribute as an OptionSetValue like below and it works just fine.

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

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

I have verified that the definition of new_myOptionSetAttribute in the entities.cs file is an OptionSetValue. I have also verified that the definition in CRM is an OptionSet value.

有人经历过吗?

推荐答案

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

Below code will throw exact error, because you are trying to assign right side int value to left side OptionSetValue variable:

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

InvalidCastException: Unable to cast object of type 'System.Int32' to type 'Microsoft.Xrm.Sdk.OptionSetValue'

OptionSetValue localIntegerVariable;

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

在这种情况下,localIntegerVariable应该为int,因为.Value将为您提供int数据类型结果.

In this case localIntegerVariable should be int, because .Value will be giving you int datatype result.

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

To maintain same datatype, either change it to

int localIntegerVariable;

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

OptionSetValue localIntegerVariable;

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

最后一个例子更好,因为它在使用表达式temp.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute") == null ?

Last example is better, as it checks null check before accessing .Value using expression temp.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute") == null ?

这篇关于OptionSetValue表现为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆