Example #1
0
 public bool IsMatch <TTarget>(object value)
     where TTarget : class
 {
     Fx.Assert(value != null, "extension providers never return a null extension");
     if (value is TTarget)
     {
         if (this.GeneratedTypeMatchesDeclaredType)
         {
             return(true);
         }
         else
         {
             return(TypeHelper.AreReferenceTypesCompatible(this.Type, typeof(TTarget)));
         }
     }
     else
     {
         return(false);
     }
 }
Example #2
0
        internal WorkflowInstanceExtensionCollection(Activity workflowDefinition, WorkflowInstanceExtensionManager?extensionManager)
        {
            this.extensionManager = extensionManager;

            var extensionProviderCount = 0;

            if (extensionManager != null)
            {
                extensionProviderCount      = extensionManager.ExtensionProviders?.Count ?? 0;
                this.hasTrackingParticipant = extensionManager.HasSingletonTrackingParticipant;
                this.hasPersistenceModule   = extensionManager.HasSingletonPersistenceModule;

                // create an uber-IEnumerable to simplify our iteration code
                this.allSingletonExtensions = this.extensionManager?.GetAllSingletonExtensions();
            }
            else
            {
                this.allSingletonExtensions = WorkflowInstanceExtensionManager.EmptySingletonExtensions;
            }

            // Resolve activity extensions
            Dictionary <Type, WorkflowInstanceExtensionProvider>?filteredActivityExtensionProviders = null;

            if (workflowDefinition.GetActivityExtensionInformation(out var activityExtensionProviders, out var requiredActivityExtensionTypes))
            {
                // a) filter out the extension Types that were already configured by the host. Note that only "primary" extensions are in play here, not
                // "additional" extensions
                var allExtensionTypes = new HashSet <Type>();
                if (extensionManager != null)
                {
                    extensionManager.AddAllExtensionTypes(allExtensionTypes);
                }

                if (activityExtensionProviders != null)
                {
                    filteredActivityExtensionProviders = new Dictionary <Type, WorkflowInstanceExtensionProvider>(activityExtensionProviders.Count);
                    foreach (var keyedActivityExtensionProvider in activityExtensionProviders)
                    {
                        var newExtensionProviderType = keyedActivityExtensionProvider.Key;
                        if (!TypeHelper.ContainsCompatibleType(allExtensionTypes, newExtensionProviderType))
                        {
                            // first see if the new provider supersedes any existing ones
                            List <Type>?typesToRemove            = null;
                            var         skipNewExtensionProvider = false;
                            foreach (var existingExtensionProviderType in filteredActivityExtensionProviders.Keys)
                            {
                                // Use AreReferenceTypesCompatible for performance since we know that all of these must be reference types
                                if (TypeHelper.AreReferenceTypesCompatible(existingExtensionProviderType, newExtensionProviderType))
                                {
                                    skipNewExtensionProvider = true;
                                    break;
                                }

                                if (TypeHelper.AreReferenceTypesCompatible(newExtensionProviderType, existingExtensionProviderType))
                                {
                                    if (typesToRemove == null)
                                    {
                                        typesToRemove = new List <Type>();
                                    }
                                    typesToRemove.Add(existingExtensionProviderType);
                                }
                            }

                            // prune unnecessary extension providers (either superseded by the new extension or by an existing extension that supersedes them both)
                            if (typesToRemove != null)
                            {
                                for (var i = 0; i < typesToRemove.Count; i++)
                                {
                                    filteredActivityExtensionProviders.Remove(typesToRemove[i]);
                                }
                            }

                            // and add a new extension if necessary
                            if (!skipNewExtensionProvider)
                            {
                                filteredActivityExtensionProviders.Add(newExtensionProviderType, keyedActivityExtensionProvider.Value);
                            }
                        }
                    }
                    if (filteredActivityExtensionProviders.Count > 0)
                    {
                        allExtensionTypes.UnionWith(filteredActivityExtensionProviders.Keys);
                        extensionProviderCount += filteredActivityExtensionProviders.Count;
                    }
                }

                // b) Validate that all required extensions will be provided
                if (requiredActivityExtensionTypes != null && requiredActivityExtensionTypes.Count > 0)
                {
                    foreach (var requiredType in requiredActivityExtensionTypes)
                    {
                        if (!TypeHelper.ContainsCompatibleType(allExtensionTypes, requiredType))
                        {
                            throw FxTrace.Exception.AsError(new ValidationException(SR.RequiredExtensionTypeNotFound(requiredType.ToString())));
                        }
                    }
                }
            }

            // Finally, if our checks of passed, resolve our delegates
            if (extensionProviderCount > 0)
            {
                this.instanceExtensions = new List <KeyValuePair <WorkflowInstanceExtensionProvider, object> >(extensionProviderCount);

                if (extensionManager != null)
                {
                    var extensionProviders = extensionManager.ExtensionProviders;
                    if (extensionProviders != null)
                    {
                        for (var i = 0; i < (extensionProviders.Count); i++)
                        {
                            var extensionProvider = extensionProviders[i];
                            this.AddInstanceExtension(extensionProvider.Value);
                        }
                    }
                }

                if (filteredActivityExtensionProviders != null)
                {
                    foreach (var extensionProvider in filteredActivityExtensionProviders.Values)
                    {
                        this.AddInstanceExtension(extensionProvider);
                    }
                }
            }
        }
Example #3
0
 public bool IsMatch <TTarget>(object value) where TTarget : class
 {
     return((value is TTarget) && (this.GeneratedTypeMatchesDeclaredType || TypeHelper.AreReferenceTypesCompatible(this.Type, typeof(TTarget))));
 }