1

我有两个插件 PreEntityUpdate 和 PostEntityAssign。在更新 PostEntityUpdate 中,我执行 assign 并调用 PostEntityAssign 插件执行。是否可以将共享变量从 PreEntityUpdate 传递给 PostEntityAssign?我试过但没有成功...

4

1 回答 1

1

我认为您应该查看插件的所有父上下文,并尝试在 SharedVariables 集合中找到必要的共享数据。这是一个代码示例:

 public new void Execute(IServiceProvider serviceProvider)
    {
        string sharedDataKey = "your key defined here";
        bool found = false;

        IPluginExecutionContext currentContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        // Find shared data among parent contexts
        IPluginExecutionContext context = currentContext;
        while (context != null)
        {
            if (context.SharedVariables.ContainsKey(sharedDataKey))
            {
                found = true;
                break;
            }

            context = context.ParentContext;
        }

        object sharedData = null;
        if (found)
        {
            // Data was found in parent context
            sharedData = context.SharedVariables[sharedDataKey];
        }
        else
        {
            // Data was NOT found in parent context, thereby we create new one
            sharedData = new object();
            currentContext.SharedVariables[sharedDataKey] = sharedData;
        }

        // Do what you want with 'sharedData'
    }

我用于您描述的情况的东西非常相似。也就是说,我有一个 PRE 更新实体插件,它有时会更新相关实体,这会立即导致调用我的插件的另一个实例。

于 2014-04-25T12:47:12.943 回答