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);
            }
        }
        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);
        }