- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个插件,我在其中检查字段的特定值、选项集是否等于特定值,如果是,则执行某些操作。
现在,在插件 C# 代码中,我如何检查 Option Set 字段是否不为空 - 即设置为默认值?
我所做的(显然,那是错误的)因为它从未通过 Null 检查语句。而且,如果我没有支票,则会收到此错误消息
错误:
Unexpected exception from plug-in (Execute): CRM.AppStateHandler.Plugins.PostApplicationCreate: System.NullReferenceException: Object reference not set to an instance of an object.
代码:
application applicationEntity = entity.ToEntity<new_application>();
if (new_applicationEntity.new_Applicationstatus != null)
{
var applicationStatus = applicationEntity.new_Applicationstatus.Value;
if (applicationStatus == CRMConstants.EntityApplication.Attributes.ApplicationStatusOptions.Applying)
{
//my logic
}
}
文件constants.cs有以下内容
class CRMConstants
{
public struct EntityApplication
{
public struct Attributes
{
public struct ApplicationStatusOptions
{
// More before this
public const int Applying = 100000006;
// More to come
}
}
}
最佳答案
我认为 SergeyS 可以解决您的问题,但我会添加一些其他(希望如此)有用的评论。
不要为您的选项集值自定义创建结构。使用 CrmSrvcUtil自动为您创建枚举。
我对必须检查 OptionSetValues 是否为空感到恼火,所以我使用这些扩展方法让我的生活更轻松:
/// <summary>
/// Returns the value of the OptionSetValue, or int.MinValue if it is null
/// </summary>
/// <param name="osv"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int GetValueOrDefault(this OptionSetValue osv)
{
return GetValueOrDefault(osv, int.MinValue);
}
/// <summary>
/// Returns the value of the OptionSetValue, or int.MinValue if it is null
/// </summary>
/// <param name="osv"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int GetValueOrDefault(this OptionSetValue osv, int defaultValue)
{
if (osv == null)
{
return defaultValue;
}
else
{
return osv.Value;
}
}
/// <summary>
/// Allows for Null safe Equals Comparison for more concise code. ie.
/// if(contact.GenderCode.NullSafeEquals(1))
/// vs.
/// if(contact.GenderCode != null && contact.gendercode.Value == 1)
/// </summary>
/// <param name="osv"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool NullSafeEquals(this OptionSetValue osv, int value)
{
if (osv == null)
{
return false;
}
else
{
return osv.Value == value;
}
}
/// <summary>
/// Allows for Null safe Equals Comparison for more concise code. ie.
/// if(contact.GenderCode.NullSafeEquals(new OptionSet(1)))
/// vs.
/// if(contact.GenderCode != null && contact.gendercode.Value == new OptionSet(1))
/// </summary>
/// <param name="osv"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool NullSafeEquals(this OptionSetValue osv, OptionSetValue value)
{
if (osv == null)
{
return osv == value;
}
else
{
return osv.Equals(value);
}
}
有两个方法,每个方法都有一个重载:
GetValueOrDefault - 这等同于 Nullable.GetValueOrDefault()。一个区别是我没有默认为 0,而是默认为 int.MinValue
以确保我不会意外匹配 0 选项集值。重载允许您根据需要指定默认值。
NullSafeEquals - 这是您在代码中使用的不必检查 null 的方法
application applicationEntity = entity.ToEntity<new_application>();
if (applicationEntity.new_Applicationstatus.NullSafeEquals(CRMConstants.EntityApplication.Attributes.ApplicationStatusOptions.Applying))
{
//my logic
}
关于plugins - CRM 2011 插件 - 检查 OptionSet 是否不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15571108/
我正在尝试编写一个辅助函数,它将位索引数组转换为符合 OptionSet 的类。 func getOptionSet(bitIndexes: [Int64]) -> T { var resul
假设我有一组选项可以传递给某些进程,例如 struct Options: OptionSet { let rawValue: Int static let floogled = Optio
这可能最好用一个用例来解释。 我有一个记录器类。它将消息记录到级别的输出。 class Logger { var outputs: OutputOptions var filter: Le
所以我有一个OptionSet: struct Ability: OptionSet { let rawValue: Int static let create = Ability(
我正在 Xcode 8 的 iOS playground 中尝试这个,但它不起作用: struct Direction: OptionSet { let rawValue: UInt8
Swift 有 OptionSet 类型,它基本上将设置操作添加到 C 风格的位标志。 Apple 在他们的框架中广泛使用它们。示例包括 animate(withDuration:delay:opti
定义一个简单的 OptionSet: public struct TestSet : OptionSet, Hashable { public let rawValue: Int pu
我需要将多个安全组添加到我的 EB 环境中。这是我的 EB 环境模板的一部分: Type: 'AWS::ElasticBeanstalk::Environment' Properties:
基于 https://developer.apple.com/documentation/swift/optionset , 我注意到 OptionSet 中的成员不可能超过 64 个 例如 stru
基于 https://developer.apple.com/documentation/swift/optionset , 我注意到 OptionSet 中的成员不可能超过 64 个 例如 stru
我正在尝试创建一个可以与 @IBInspectable 结合使用的 OptionSet 这似乎在 Swift 2.2 中是可能的 我遇到了 this库似乎将 OptionSet 与 @IBInspec
使用以下选项集: struct StatusOptions : OptionSet { let rawValue: Int static let CountdownDuration0
我有一个OptionSet,我希望能够对其执行位操作(BitwiseOperation 协议(protocol)中的操作)。我为我的类型写了一个扩展,然后意识到我应该让它成为可重用的代码。我花了一段时
我有一个示例代码: public struct MyOptions: OptionSet { public let rawValue: Int public init(rawValue
我正在编写一个插件,我在其中检查字段的特定值、选项集是否等于特定值,如果是,则执行某些操作。 现在,在插件 C# 代码中,我如何检查 Option Set 字段是否不为空 - 即设置为默认值? 我所做
我正在编写一个插件,我在其中检查字段的特定值、选项集是否等于特定值,如果是,则执行某些操作。 现在,在插件 C# 代码中,我如何检查 Option Set 字段是否不为空 - 即设置为默认值? 我所做
请看一下这段简单的 Swift 代码: UIView.animate(withDuration: duration, delay: interButtonDelay * Double(expandin
使用 Dynamics CRM 365 时,我想找到一种方法,使用 Javascript 库“XrmServiceToolkit”和 Soap 方法(我们需要异步)从全局选项集中获取标签和值。 谢谢。
我正在尝试为 2 个 UIButton 的状态设置相同的 UIImage - Normal 和 Highlighted UIControlState 是一个 OptionSet,所以如果我传递一个数组
位移选项集... struct VerifiedOptions : OptionSet { let rawValue: Int static let facebook = Verifi
我是一名优秀的程序员,十分优秀!