void IIdentifierCreationService.ValidateIdentifier(Activity activity, string identifier)
 {
     if (identifier == null)
     {
         throw new ArgumentNullException("identifier");
     }
     if (activity == null)
     {
         throw new ArgumentNullException("activity");
     }
     if (!activity.Name.ToLowerInvariant().Equals(identifier.ToLowerInvariant()))
     {
         if (this.Provider != null)
         {
             SupportedLanguages supportedLanguage = CompilerHelpers.GetSupportedLanguage(this.serviceProvider);
             if ((((supportedLanguage == SupportedLanguages.CSharp) && identifier.StartsWith("@", StringComparison.Ordinal)) || (((supportedLanguage == SupportedLanguages.VB) && identifier.StartsWith("[", StringComparison.Ordinal)) && identifier.EndsWith("]", StringComparison.Ordinal))) || !this.Provider.IsValidIdentifier(identifier))
             {
                 throw new Exception(SR.GetString("Error_InvalidLanguageIdentifier", new object[] { identifier }));
             }
         }
         StringDictionary  dictionary   = new StringDictionary();
         CompositeActivity rootActivity = Helpers.GetRootActivity(activity) as CompositeActivity;
         if (rootActivity != null)
         {
             foreach (string str in Helpers.GetIdentifiersInCompositeActivity(rootActivity))
             {
                 dictionary[str] = str;
             }
         }
         Type rootActivityType = this.GetRootActivityType(this.serviceProvider);
         if (rootActivityType != null)
         {
             foreach (MemberInfo info in rootActivityType.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance))
             {
                 Type c = null;
                 if (info is FieldInfo)
                 {
                     c = ((FieldInfo)info).FieldType;
                 }
                 if ((c == null) || !typeof(Activity).IsAssignableFrom(c))
                 {
                     dictionary[info.Name] = info.Name;
                 }
             }
         }
         if (dictionary.ContainsKey(identifier))
         {
             throw new ArgumentException(SR.GetString("DuplicateActivityIdentifier", new object[] { identifier }));
         }
     }
 }
        bool IExtenderProvider.CanExtend(object extendee)
        {
            bool     flag     = false;
            Activity activity = extendee as Activity;

            if (((activity != null) && (activity.Site != null)) && (activity == Helpers.GetRootActivity(activity)))
            {
                ActivityDesigner designer = ActivityDesigner.GetDesigner(activity);
                if ((designer != null) && (designer.ParentDesigner == null))
                {
                    flag = true;
                }
            }
            return(flag);
        }
Esempio n. 3
0
        bool IExtenderProvider.CanExtend(object extendee)
        {
            bool canExtend = false;

            Activity activity = extendee as Activity;

            if (activity != null && activity.Site != null && activity == Helpers.GetRootActivity(activity))
            {
                ActivityDesigner rootDesigner = ActivityDesigner.GetDesigner(activity);
                if (rootDesigner != null && rootDesigner.ParentDesigner == null)
                {
                    canExtend = true;
                }
            }

            return(canExtend);
        }
        private static void InternalFilterProperties(IServiceProvider serviceProvider, object propertyOwner, IDictionary properties)
        {
            // change property descriptors
            Hashtable newProperties = new Hashtable();

            foreach (object key in properties.Keys)
            {
                PropertyDescriptor propDesc = properties[key] as PropertyDescriptor;
                if (string.Equals(propDesc.Name, "Name", StringComparison.Ordinal) && typeof(Activity).IsAssignableFrom(propDesc.ComponentType))
                {
                    //Activity id
                    Activity activity = propertyOwner as Activity;
                    if (activity != null && activity == Helpers.GetRootActivity(activity))
                    {
                        newProperties[key] = new NamePropertyDescriptor(serviceProvider, propDesc);
                    }
                    else
                    {
                        newProperties[key] = new IDPropertyDescriptor(serviceProvider, propDesc);
                    }
                }
                else if (!(propDesc is ActivityBindPropertyDescriptor) && ActivityBindPropertyDescriptor.IsBindableProperty(propDesc))
                {
                    if (typeof(Type).IsAssignableFrom(propDesc.PropertyType) && !(propDesc is ParameterInfoBasedPropertyDescriptor))
                    {
                        propDesc = new TypePropertyDescriptor(serviceProvider, propDesc);
                    }
                    newProperties[key] = new ActivityBindPropertyDescriptor(serviceProvider, propDesc, propertyOwner);
                }
                else if (typeof(Type).IsAssignableFrom(propDesc.PropertyType))
                {
                    newProperties[key] = new TypePropertyDescriptor(serviceProvider, propDesc);
                }
                else
                {
                    newProperties[key] = new DynamicPropertyDescriptor(serviceProvider, propDesc);
                }
            }

            foreach (object key in newProperties.Keys)
            {
                properties[key] = newProperties[key];
            }
        }
        public override void SetValue(object component, object value)
        {
            Activity activity = component as Activity;

            if (activity != null)
            {
                IIdentifierCreationService service = activity.Site.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
                if (service == null)
                {
                    throw new Exception(SR.GetString("General_MissingService", new object[] { typeof(IIdentifierCreationService).FullName }));
                }
                string identifier = value as string;
                service.ValidateIdentifier(activity, identifier);
                bool ignoreCase      = CompilerHelpers.GetSupportedLanguage(activity.Site) == SupportedLanguages.VB;
                Type dataSourceClass = Helpers.GetDataSourceClass(Helpers.GetRootActivity(activity), activity.Site);
                if ((dataSourceClass != null) && (ActivityBindPropertyDescriptor.FindMatchingMember(identifier, dataSourceClass, ignoreCase) != null))
                {
                    throw new ArgumentException(SR.GetString("Error_ActivityNameExist", new object[] { identifier }));
                }
                IMemberCreationService service2 = activity.Site.GetService(typeof(IMemberCreationService)) as IMemberCreationService;
                if (service2 == null)
                {
                    throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IMemberCreationService).FullName }));
                }
                IDesignerHost host = activity.Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
                if (host == null)
                {
                    throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IDesignerHost).FullName }));
                }
                string newClassName = identifier;
                int    num          = host.RootComponentClassName.LastIndexOf('.');
                if (num > 0)
                {
                    newClassName = host.RootComponentClassName.Substring(0, num + 1) + identifier;
                }
                service2.UpdateTypeName(((Activity)host.RootComponent).GetValue(WorkflowMarkupSerializer.XClassProperty) as string, newClassName);
                ((Activity)host.RootComponent).SetValue(WorkflowMarkupSerializer.XClassProperty, newClassName);
                base.SetValue(component, value);
                DesignerHelpers.UpdateSiteName((Activity)host.RootComponent, identifier);
            }
        }
Esempio n. 6
0
        private static void InternalFilterProperties(IServiceProvider serviceProvider, object propertyOwner, IDictionary properties)
        {
            Hashtable hashtable = new Hashtable();

            foreach (object obj2 in properties.Keys)
            {
                PropertyDescriptor actualPropDesc = properties[obj2] as PropertyDescriptor;
                if (string.Equals(actualPropDesc.Name, "Name", StringComparison.Ordinal) && typeof(Activity).IsAssignableFrom(actualPropDesc.ComponentType))
                {
                    Activity activity = propertyOwner as Activity;
                    if ((activity != null) && (activity == Helpers.GetRootActivity(activity)))
                    {
                        hashtable[obj2] = new NamePropertyDescriptor(serviceProvider, actualPropDesc);
                    }
                    else
                    {
                        hashtable[obj2] = new IDPropertyDescriptor(serviceProvider, actualPropDesc);
                    }
                }
                else if (!(actualPropDesc is ActivityBindPropertyDescriptor) && ActivityBindPropertyDescriptor.IsBindableProperty(actualPropDesc))
                {
                    if (typeof(Type).IsAssignableFrom(actualPropDesc.PropertyType) && !(actualPropDesc is ParameterInfoBasedPropertyDescriptor))
                    {
                        actualPropDesc = new TypePropertyDescriptor(serviceProvider, actualPropDesc);
                    }
                    hashtable[obj2] = new ActivityBindPropertyDescriptor(serviceProvider, actualPropDesc, propertyOwner);
                }
                else if (typeof(Type).IsAssignableFrom(actualPropDesc.PropertyType))
                {
                    hashtable[obj2] = new TypePropertyDescriptor(serviceProvider, actualPropDesc);
                }
                else
                {
                    hashtable[obj2] = new DynamicPropertyDescriptor(serviceProvider, actualPropDesc);
                }
            }
            foreach (object obj3 in hashtable.Keys)
            {
                properties[obj3] = hashtable[obj3];
            }
        }
 internal static bool CreateField(ITypeDescriptorContext context, ActivityBind activityBind, bool throwOnError)
 {
     if (!string.IsNullOrEmpty(activityBind.Path))
     {
         Type toType = PropertyDescriptorUtils.GetBaseType(context.PropertyDescriptor, context.Instance, context);
         Activity component = PropertyDescriptorUtils.GetComponent(context) as Activity;
         if ((component != null) && (toType != null))
         {
             component = Helpers.ParseActivityForBind(component, activityBind.Name);
             if (component == Helpers.GetRootActivity(component))
             {
                 bool ignoreCase = CompilerHelpers.GetSupportedLanguage(context) == SupportedLanguages.VB;
                 Type dataSourceClass = Helpers.GetDataSourceClass(component, context);
                 if (dataSourceClass != null)
                 {
                     string path = activityBind.Path;
                     int length = path.IndexOfAny(new char[] { '.', '/', '[' });
                     if (length != -1)
                     {
                         path = path.Substring(0, length);
                     }
                     MemberInfo info = FindMatchingMember(path, dataSourceClass, ignoreCase);
                     if (info != null)
                     {
                         Type fromType = null;
                         bool isPrivate = false;
                         if (info is FieldInfo)
                         {
                             isPrivate = ((FieldInfo) info).IsPrivate;
                             fromType = ((FieldInfo) info).FieldType;
                         }
                         else if (info is PropertyInfo)
                         {
                             MethodInfo getMethod = ((PropertyInfo) info).GetGetMethod();
                             MethodInfo setMethod = ((PropertyInfo) info).GetSetMethod();
                             isPrivate = ((getMethod != null) && getMethod.IsPrivate) || ((setMethod != null) && setMethod.IsPrivate);
                         }
                         else if (info is MethodInfo)
                         {
                             isPrivate = ((MethodInfo) info).IsPrivate;
                         }
                         if (length != -1)
                         {
                             PathWalker walker = new PathWalker();
                             PathMemberInfoEventArgs finalEventArgs = null;
                             walker.MemberFound = (EventHandler<PathMemberInfoEventArgs>) Delegate.Combine(walker.MemberFound, delegate (object sender, PathMemberInfoEventArgs eventArgs) {
                                 finalEventArgs = eventArgs;
                             });
                             if (!walker.TryWalkPropertyPath(dataSourceClass, activityBind.Path))
                             {
                                 if (throwOnError)
                                 {
                                     throw new InvalidOperationException(SR.GetString("Error_MemberWithSameNameExists", new object[] { activityBind.Path, dataSourceClass.FullName }));
                                 }
                                 return false;
                             }
                             fromType = BindHelpers.GetMemberType(finalEventArgs.MemberInfo);
                         }
                         if (((info.DeclaringType == dataSourceClass) || !isPrivate) && ((info is FieldInfo) && TypeProvider.IsAssignable(toType, fromType)))
                         {
                             return true;
                         }
                         if (throwOnError)
                         {
                             throw new InvalidOperationException(SR.GetString("Error_MemberWithSameNameExists", new object[] { activityBind.Path, dataSourceClass.FullName }));
                         }
                         return false;
                     }
                     Activity activity2 = null;
                     if (string.Compare(component.Name, path, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0)
                     {
                         activity2 = component;
                     }
                     else if ((component is CompositeActivity) && (component is CompositeActivity))
                     {
                         foreach (Activity activity3 in Helpers.GetAllNestedActivities(component as CompositeActivity))
                         {
                             if (string.Compare(activity3.Name, path, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0)
                             {
                                 activity2 = activity3;
                             }
                         }
                     }
                     if (activity2 != null)
                     {
                         if (TypeProvider.IsAssignable(toType, activity2.GetType()))
                         {
                             return true;
                         }
                         if (throwOnError)
                         {
                             throw new InvalidOperationException(SR.GetString("Error_MemberWithSameNameExists", new object[] { activityBind.Path, dataSourceClass.FullName }));
                         }
                         return false;
                     }
                     IMemberCreationService service = context.GetService(typeof(IMemberCreationService)) as IMemberCreationService;
                     if (service != null)
                     {
                         IDesignerHost host = context.GetService(typeof(IDesignerHost)) as IDesignerHost;
                         if (host != null)
                         {
                             service.CreateField(host.RootComponentClassName, activityBind.Path, toType, null, MemberAttributes.Public, null, false);
                             return true;
                         }
                         if (throwOnError)
                         {
                             throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IDesignerHost).FullName }));
                         }
                     }
                     else if (throwOnError)
                     {
                         throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IMemberCreationService).FullName }));
                     }
                 }
             }
         }
         else
         {
             if ((component == null) && throwOnError)
             {
                 throw new InvalidOperationException(SR.GetString("Error_InvalidActivityIdentifier", new object[] { activityBind.Name }));
             }
             if ((toType == null) && throwOnError)
             {
                 throw new InvalidOperationException(SR.GetString("Error_PropertyTypeNotDefined", new object[] { context.PropertyDescriptor.Name, typeof(ActivityBind).Name, typeof(IDynamicPropertyTypeProvider).Name }));
             }
         }
     }
     return false;
 }
        void IIdentifierCreationService.ValidateIdentifier(Activity activity, string identifier)
        {
            if (identifier == null)
            {
                throw new ArgumentNullException("identifier");
            }
            if (activity == null)
            {
                throw new ArgumentNullException("activity");
            }

            if (activity.Name.ToLowerInvariant().Equals(identifier.ToLowerInvariant()))
            {
                return;
            }

            if (this.Provider != null)
            {
                SupportedLanguages language = CompilerHelpers.GetSupportedLanguage(this.serviceProvider);
                if (language == SupportedLanguages.CSharp && identifier.StartsWith("@", StringComparison.Ordinal) ||
                    language == SupportedLanguages.VB && identifier.StartsWith("[", StringComparison.Ordinal) && identifier.EndsWith("]", StringComparison.Ordinal) ||
                    !this.Provider.IsValidIdentifier(identifier))
                {
                    throw new Exception(SR.GetString(SR.Error_InvalidLanguageIdentifier, identifier));
                }
            }

            StringDictionary  identifiers  = new StringDictionary();
            CompositeActivity rootActivity = Helpers.GetRootActivity(activity) as CompositeActivity;

            if (rootActivity != null)
            {
                foreach (string existingIdentifier in Helpers.GetIdentifiersInCompositeActivity(rootActivity))
                {
                    identifiers[existingIdentifier] = existingIdentifier;
                }
            }

            Type customActivityType = GetRootActivityType(this.serviceProvider);

            if (customActivityType != null)
            {
                foreach (MemberInfo member in customActivityType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
                {
                    Type memberType = null;
                    if (member is FieldInfo)
                    {
                        memberType = ((FieldInfo)member).FieldType;
                    }

                    if (memberType == null || !typeof(Activity).IsAssignableFrom(memberType))
                    {
                        identifiers[member.Name] = member.Name;
                    }
                }
            }

            if (identifiers.ContainsKey(identifier))
            {
                throw new ArgumentException(SR.GetString(SR.DuplicateActivityIdentifier, identifier));
            }
        }
        /// <summary>
        /// This method will ensure that the identifiers of the activities to be added to the parent activity
        /// are unique within the scope of the parent activity.
        /// </summary>
        /// <param name="parentActivity">THis activity is the parent activity which the child activities are being added</param>
        /// <param name="childActivities"></param>
        void IIdentifierCreationService.EnsureUniqueIdentifiers(CompositeActivity parentActivity, ICollection childActivities)
        {
            if (parentActivity == null)
            {
                throw new ArgumentNullException("parentActivity");
            }
            if (childActivities == null)
            {
                throw new ArgumentNullException("childActivities");
            }

            ArrayList allActivities = new ArrayList();

            Queue activities = new Queue(childActivities);

            while (activities.Count > 0)
            {
                Activity activity = (Activity)activities.Dequeue();
                if (activity is CompositeActivity)
                {
                    foreach (Activity child in ((CompositeActivity)activity).Activities)
                    {
                        activities.Enqueue(child);
                    }
                }

                //If we are moving activities, we need not regenerate their identifiers
                if (((IComponent)activity).Site != null)
                {
                    continue;
                }

                //If the activity is built-in, we won't generate a new ID.
                if (IsPreBuiltActivity(activity))
                {
                    continue;
                }

                allActivities.Add(activity);
            }

            // get the root activity
            CompositeActivity rootActivity = Helpers.GetRootActivity(parentActivity) as CompositeActivity;
            StringDictionary  identifiers  = new StringDictionary(); // all the identifiers in the workflow

            Type customActivityType = GetRootActivityType(this.serviceProvider);

            if (rootActivity != null)
            {
                foreach (string identifier in Helpers.GetIdentifiersInCompositeActivity(rootActivity))
                {
                    identifiers[identifier] = identifier;
                }
            }

            if (customActivityType != null)
            {
                foreach (MemberInfo member in customActivityType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
                {
                    Type memberType = null;
                    if (member is FieldInfo)
                    {
                        memberType = ((FieldInfo)member).FieldType;
                    }

                    if (memberType == null || !typeof(Activity).IsAssignableFrom(memberType))
                    {
                        identifiers[member.Name] = member.Name;
                    }
                }
            }

            // now loop until we find a identifier that hasn't been used
            foreach (Activity activity in allActivities)
            {
                int    index           = 0;
                string baseIdentifier  = Helpers.GetBaseIdentifier(activity);
                string finalIdentifier = null;

                if (string.IsNullOrEmpty(activity.Name) || string.Equals(activity.Name, activity.GetType().Name, StringComparison.Ordinal))
                {
                    finalIdentifier = string.Format(CultureInfo.InvariantCulture, "{0}{1}", new object[] { baseIdentifier, ++index });
                }
                else
                {
                    finalIdentifier = activity.Name;
                }

                while (identifiers.ContainsKey(finalIdentifier))
                {
                    finalIdentifier = string.Format(CultureInfo.InvariantCulture, "{0}{1}", new object[] { baseIdentifier, ++index });
                    if (this.Provider != null)
                    {
                        finalIdentifier = this.Provider.CreateValidIdentifier(finalIdentifier);
                    }
                }

                // add new identifier to collection
                identifiers[finalIdentifier] = finalIdentifier;
                activity.Name = finalIdentifier;
            }
        }
        void IIdentifierCreationService.EnsureUniqueIdentifiers(CompositeActivity parentActivity, ICollection childActivities)
        {
            if (parentActivity == null)
            {
                throw new ArgumentNullException("parentActivity");
            }
            if (childActivities == null)
            {
                throw new ArgumentNullException("childActivities");
            }
            ArrayList list  = new ArrayList();
            Queue     queue = new Queue(childActivities);

            while (queue.Count > 0)
            {
                Activity activity = (Activity)queue.Dequeue();
                if (activity is CompositeActivity)
                {
                    foreach (Activity activity2 in ((CompositeActivity)activity).Activities)
                    {
                        queue.Enqueue(activity2);
                    }
                }
                if ((activity.Site == null) && !IsPreBuiltActivity(activity))
                {
                    list.Add(activity);
                }
            }
            CompositeActivity rootActivity = Helpers.GetRootActivity(parentActivity) as CompositeActivity;
            StringDictionary  dictionary   = new StringDictionary();
            Type rootActivityType          = this.GetRootActivityType(this.serviceProvider);

            if (rootActivity != null)
            {
                foreach (string str in Helpers.GetIdentifiersInCompositeActivity(rootActivity))
                {
                    dictionary[str] = str;
                }
            }
            if (rootActivityType != null)
            {
                foreach (MemberInfo info in rootActivityType.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance))
                {
                    Type c = null;
                    if (info is FieldInfo)
                    {
                        c = ((FieldInfo)info).FieldType;
                    }
                    if ((c == null) || !typeof(Activity).IsAssignableFrom(c))
                    {
                        dictionary[info.Name] = info.Name;
                    }
                }
            }
            foreach (Activity activity4 in list)
            {
                int    num            = 0;
                string baseIdentifier = Helpers.GetBaseIdentifier(activity4);
                string key            = null;
                if (string.IsNullOrEmpty(activity4.Name) || string.Equals(activity4.Name, activity4.GetType().Name, StringComparison.Ordinal))
                {
                    key = string.Format(CultureInfo.InvariantCulture, "{0}{1}", new object[] { baseIdentifier, ++num });
                }
                else
                {
                    key = activity4.Name;
                }
                while (dictionary.ContainsKey(key))
                {
                    key = string.Format(CultureInfo.InvariantCulture, "{0}{1}", new object[] { baseIdentifier, ++num });
                    if (this.Provider != null)
                    {
                        key = this.Provider.CreateValidIdentifier(key);
                    }
                }
                dictionary[key] = key;
                activity4.Name  = key;
            }
        }
        public override void SetValue(object component, object value)
        {
            Activity activity = component as Activity;

            if (activity != null)
            {
                // validate the identifier
                IIdentifierCreationService identifierCreationService = activity.Site.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
                if (identifierCreationService == null)
                {
                    throw new Exception(SR.GetString(SR.General_MissingService, typeof(IIdentifierCreationService).FullName));
                }

                string name = value as string;
                identifierCreationService.ValidateIdentifier(activity, name);

                bool isVB         = (CompilerHelpers.GetSupportedLanguage(activity.Site) == SupportedLanguages.VB);
                Type designedType = Helpers.GetDataSourceClass(Helpers.GetRootActivity(activity), activity.Site);
                if (designedType != null)
                {
                    MemberInfo matchingMember = ActivityBindPropertyDescriptor.FindMatchingMember(name, designedType, isVB);
                    if (matchingMember != null)
                    {
                        throw new ArgumentException(SR.GetString(SR.Error_ActivityNameExist, name));
                    }
                }
                IMemberCreationService memberCreationService = activity.Site.GetService(typeof(IMemberCreationService)) as IMemberCreationService;
                if (memberCreationService == null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(IMemberCreationService).FullName));
                }

                IDesignerHost host = activity.Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
                if (host == null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(IDesignerHost).FullName));
                }

                // We need to update the activityType's name before trying to update the type because
                // updating the type causes a flush, which access the custom activity's properties, and
                // doing so requires the new type name
                string newClassName = name;
                int    indexOfDot   = host.RootComponentClassName.LastIndexOf('.');
                if (indexOfDot > 0)
                {
                    newClassName = host.RootComponentClassName.Substring(0, indexOfDot + 1) + name;
                }

                // IMPORTANT: You must update the class name in code before renaming the site, since
                // VS's OnComponentRename updates the RootComponentClassName, so the flush code called
                // in our OnComponentRename tries to access the new class for information.
                memberCreationService.UpdateTypeName(((Activity)host.RootComponent).GetValue(WorkflowMarkupSerializer.XClassProperty) as string, newClassName);

                //((Activity)host.RootComponent).Name = name;
                ((Activity)host.RootComponent).SetValue(WorkflowMarkupSerializer.XClassProperty, newClassName);
                base.SetValue(component, value);

                // Update the site name so the component name shows up correctly in the designer
                DesignerHelpers.UpdateSiteName((Activity)host.RootComponent, name);
            }
        }
        internal static bool CreateField(ITypeDescriptorContext context, ActivityBind activityBind, bool throwOnError)
        {
            //Check if the activity is root activity and has valid design time type
            if (!String.IsNullOrEmpty(activityBind.Path))
            {
                Type     boundType = PropertyDescriptorUtils.GetBaseType(context.PropertyDescriptor, context.Instance, context);
                Activity activity  = PropertyDescriptorUtils.GetComponent(context) as Activity;
                if (activity != null && boundType != null)
                {
                    activity = Helpers.ParseActivityForBind(activity, activityBind.Name);
                    if (activity == Helpers.GetRootActivity(activity))
                    {
                        bool isVB         = (CompilerHelpers.GetSupportedLanguage(context) == SupportedLanguages.VB);
                        Type designedType = Helpers.GetDataSourceClass(activity, context);
                        if (designedType != null)
                        {
                            //field path could be nested too.
                            //need to find field only with the name up to the first dot (CimplexTypeField in the example below)
                            //and the right type (that would be tricky if the field doesnt exist yet)
                            //example: CimplexTypeField.myIDictionary_int_string[10].someOtherGood2

                            string fieldName  = activityBind.Path;
                            int    indexOfDot = fieldName.IndexOfAny(new char[] { '.', '/', '[' });
                            if (indexOfDot != -1)
                            {
                                fieldName = fieldName.Substring(0, indexOfDot); //path is a nested field access
                            }
                            MemberInfo matchingMember = ActivityBindPropertyDescriptor.FindMatchingMember(fieldName, designedType, isVB);
                            if (matchingMember != null)
                            {
                                Type memberType = null;
                                bool isPrivate  = false;
                                if (matchingMember is FieldInfo)
                                {
                                    isPrivate  = ((FieldInfo)matchingMember).IsPrivate;
                                    memberType = ((FieldInfo)matchingMember).FieldType;
                                }
                                else if (matchingMember is PropertyInfo)
                                {
                                    MethodInfo getMethod = ((PropertyInfo)matchingMember).GetGetMethod();
                                    MethodInfo setMethod = ((PropertyInfo)matchingMember).GetSetMethod();
                                    isPrivate = ((getMethod != null && getMethod.IsPrivate) || (setMethod != null && setMethod.IsPrivate));
                                }
                                else if (matchingMember is MethodInfo)
                                {
                                    isPrivate = ((MethodInfo)matchingMember).IsPrivate;
                                }

                                if (indexOfDot != -1)
                                { //need to find the type of the member the path references (and if the path is valid at all)
                                    PathWalker pathWalker = new PathWalker();
                                    PathMemberInfoEventArgs finalEventArgs = null;
                                    pathWalker.MemberFound += delegate(object sender, PathMemberInfoEventArgs eventArgs)
                                    { finalEventArgs = eventArgs; };

                                    if (pathWalker.TryWalkPropertyPath(designedType, activityBind.Path))
                                    {
                                        //successfully walked the entire path
                                        memberType = BindHelpers.GetMemberType(finalEventArgs.MemberInfo);
                                    }
                                    else
                                    {
                                        //the path is invalid
                                        if (throwOnError)
                                        {
                                            throw new InvalidOperationException(SR.GetString(SR.Error_MemberWithSameNameExists, activityBind.Path, designedType.FullName));
                                        }

                                        return(false);
                                    }
                                }

                                if ((matchingMember.DeclaringType == designedType || !isPrivate) &&
                                    matchingMember is FieldInfo &&
                                    TypeProvider.IsAssignable(boundType, memberType))
                                {
                                    return(true);
                                }
                                else
                                {
                                    if (throwOnError)
                                    {
                                        throw new InvalidOperationException(SR.GetString(SR.Error_MemberWithSameNameExists, activityBind.Path, designedType.FullName));
                                    }
                                    return(false);
                                }
                            }
                            else
                            {
                                // Find out if the name conflicts with an existing activity that has not be flushed in to the
                                // code beside.  An activity bind can bind to this field only if the type of the property
                                // is the assignable from the activity type.
                                Activity matchingActivity = null;
                                if (string.Compare(activity.Name, fieldName, isVB ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0)
                                {
                                    matchingActivity = activity;
                                }
                                else if (activity is CompositeActivity)
                                {
                                    if (activity is CompositeActivity)
                                    {
                                        foreach (Activity existingActivity in Helpers.GetAllNestedActivities(activity as CompositeActivity))
                                        {
                                            if (string.Compare(existingActivity.Name, fieldName, isVB ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0)
                                            {
                                                matchingActivity = existingActivity;
                                            }
                                        }
                                    }
                                }

                                if (matchingActivity != null)
                                {
                                    if (TypeProvider.IsAssignable(boundType, matchingActivity.GetType()))
                                    {
                                        return(true);
                                    }
                                    else
                                    {
                                        if (throwOnError)
                                        {
                                            throw new InvalidOperationException(SR.GetString(SR.Error_MemberWithSameNameExists, activityBind.Path, designedType.FullName));
                                        }
                                        return(false);
                                    }
                                }
                            }

                            IMemberCreationService memberCreationService = context.GetService(typeof(IMemberCreationService)) as IMemberCreationService;
                            if (memberCreationService == null)
                            {
                                if (throwOnError)
                                {
                                    throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(IMemberCreationService).FullName));
                                }
                            }
                            else
                            {
                                IDesignerHost designerHost = context.GetService(typeof(IDesignerHost)) as IDesignerHost;
                                if (designerHost == null)
                                {
                                    if (throwOnError)
                                    {
                                        throw new InvalidOperationException(SR.GetString("General_MissingService", typeof(IDesignerHost).FullName));
                                    }
                                }
                                else
                                {
                                    memberCreationService.CreateField(designerHost.RootComponentClassName, activityBind.Path, boundType, null, MemberAttributes.Public, null, false);
                                    return(true);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (activity == null && throwOnError)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.Error_InvalidActivityIdentifier, activityBind.Name));
                    }

                    if (boundType == null && throwOnError)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.Error_PropertyTypeNotDefined, context.PropertyDescriptor.Name, typeof(ActivityBind).Name, typeof(IDynamicPropertyTypeProvider).Name));
                    }
                }
            }

            return(false);
        }