【问题标题】:Unable to cast object of type CRM 2013 Plugin无法投射 CRM 2013 插件类型的对象
【发布时间】:2015-02-18 11:52:03
【问题描述】:

我有一个同步插件,可以在任何机会创建/删除/更新时运行。如果出现任何错误,我会在插件中创建一个将日志插入数据库的功能。

如果是EntityId,则在表一字段中,所以我正在编写以下代码:

foreach (PropertyBagEntry entry in (IEnumerable<PropertyBagEntry>)context.InputParameters.Values)
{

        DynamicEntity entity = (DynamicEntity)entry.Value;
        foreach (Property property in (IEnumerable<Property>)entity.Properties)
        {
            if (property.GetType().Name == "KeyProperty")
            {
                str4 = ((Key)entity.Properties[property.Name]).Value.ToString();
                break;
            }
        }
}

在 str4 中,我正在获取当前进程的 EntityId。

但它非常频繁地给出一个例外:

未处理的异常:System.InvalidCastException:无法转换类型的对象 'ValueCollection [System.String,System.Object]' 输入'System.Collections.Generic.IEnumerable`1[Microsoft.Crm.Sdk.PropertyBagEntry]'

我已经确定以下行给出错误

foreach((IEnumerable)context.InputParameters.Values 中的PropertyBagEntry 条目)

有人知道用另一种方式转换这条线吗?

【问题讨论】:

  • 这看起来是 CRM 4.0 代码,因为 DynamicEntity 是一个 4.0 SDK 实体...

标签: dynamics-crm dynamics-crm-2013


【解决方案1】:

我的理解是你想获取当前记录的 GUID,如果是这种情况,那么你可以这样做:

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    try
    {
        if (context.MessageName == "Create" || context.MessageName == "Update")
        {
            if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"] is Entity))
            {
                Entity currentEntity = (Entity) context.InputParameters["Target"];
                Guid currentRecordGuid = currentEntity.Id;
            }

        }
    }
    catch (Exception ex)
    {

    }
}

【讨论】:

  • 不,这不是当前记录的 GUID,它是我从 PropertyBagEntry 条目中获取的属性 ID 是“动态实体”或“名字对象”的类型。
【解决方案2】:

我相信该集合的类型因一条消息而异。如果您想在插件中获取记录的 ID,这个辅助函数可能会派上用场:

public static Guid GetEntityIdFromContext(IPluginExecutionContext context)
{
    string messageName = context.MessageName;
    if (context.PrimaryEntityId != Guid.Empty)
    {
        return context.PrimaryEntityId;
    }
    else if (messageName == "Create")
    {
        return new Guid(context.OutputParameters["id"].ToString());
    }
    else
    {
        return context.PrimaryEntityId;
    }
}

如果这没有帮助,您介意提供导致错误的消息吗?

【讨论】:

  • e8bba7ae-a552-de11-b475-001e0b4882e2 --------------------------------- ---------------------- 2b301650-2a38-e411-8cda-005056a07625 机会插件未处理异常:System.InvalidCastException:无法转换类型为“ValueCollection [System”的对象.String,System.Object]' 来输入'System.Collections.Generic.IEnumerable`1[Microsoft.Crm.Sdk.PropertyBagEntry]'。
【解决方案3】:

如果

未处理的异常:System.InvalidCastException:无法转换 要键入的“ValueCollection [System.String,System.Object]”类型的对象 'System.Collections.Generic.IEnumerable`1[Microsoft.Crm.Sdk.PropertyBagEntry]'

确实是您的错误,而不是您的问题与行

foreach (Property property in (IEnumerable<Property>)entity.Properties)

但有行:

foreach (PropertyBagEntry entry in (IEnumerable<PropertyBagEntry>)context.InputParameters.Values)

context.InputParameters.Values 的类型不能转换为 IEnumerable

【讨论】:

  • @Chirag 不要强制转换整个 Values 属性,而是枚举 InputParameters.Values 集合中的每个值,然后在强制转换之前检查它是什么类型(使用 is 或 as 运算符)。
  • 功能上我明白你在说什么,但由于我是 CRM 的新手,所以无法编写代码来获得相同的结果。请给它的代码。
  • @chirag 这不是 CRM 问题,这是一个基本的 C# 问题。使用插件分析器来调试您的插件并查看输入参数中的类型以及您希望如何处理它们。
猜你喜欢
  • 2015-02-04
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 2023-03-15
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
相关资源
最近更新 更多