private InstanceDescriptor GetInstanceDescriptorFromValues(Binding b)
        {
            b.FormattingEnabled = true;
            bool isComplete = true;
            int  index      = ConstructorParameterProperties.Length - 1;

            while (index >= 0)
            {
                if (ConstructorParameterProperties[index] == null)
                {
                    break;
                }
                PropertyDescriptor descriptor = TypeDescriptor.GetProperties(b)[ConstructorParameterProperties[index]];
                if ((descriptor != null) && descriptor.ShouldSerializeValue(b))
                {
                    break;
                }
                index--;
            }
            System.Type[] destinationArray = new System.Type[index + 1];
            Array.Copy(ConstructorParamaterTypes, 0, destinationArray, 0, destinationArray.Length);
            ConstructorInfo constructor = typeof(Binding).GetConstructor(destinationArray);

            if (constructor == null)
            {
                isComplete  = false;
                constructor = typeof(Binding).GetConstructor(new System.Type[] { typeof(string), typeof(object), typeof(string) });
            }
            object[] arguments = new object[destinationArray.Length];
            for (int i = 0; i < arguments.Length; i++)
            {
                object propertyName = null;
                switch (i)
                {
                case 0:
                    propertyName = b.PropertyName;
                    break;

                case 1:
                    propertyName = b.BindToObject.DataSource;
                    break;

                case 2:
                    propertyName = b.BindToObject.BindingMemberInfo.BindingMember;
                    break;

                default:
                    propertyName = TypeDescriptor.GetProperties(b)[ConstructorParameterProperties[i]].GetValue(b);
                    break;
                }
                arguments[i] = propertyName;
            }
            return(new InstanceDescriptor(constructor, arguments, isComplete));
        }
Beispiel #2
0
        /// <devdoc>
        ///      Gets the best matching ctor for a given binding and fills it out, based on the
        ///      state of the Binding and the optimal ctor.
        /// </devdoc>
        private InstanceDescriptor GetInstanceDescriptorFromValues(Binding b)
        {
            // The BindingFormattingDialog turns on Binding::FormattingEnabled property.
            // however, when the user data binds a property using the PropertyBrowser, Binding::FormattingEnabled is set to false
            // The Binding class is not a component class, so we don't have the ComponentInitialize method where we can set FormattingEnabled to true
            // so we set it here.
            b.FormattingEnabled = true;

            bool isComplete = true;
            int  lastItem   = ConstructorParameterProperties.Length - 1;

            for (; lastItem >= 0; lastItem--)
            {
                // null means no prop is available, we quit here.
                //
                if (ConstructorParameterProperties[lastItem] == null)
                {
                    break;
                }

                // get the property and see if it needs to be serialized.
                //
                PropertyDescriptor prop = TypeDescriptor.GetProperties(b)[ConstructorParameterProperties[lastItem]];
                if (prop != null && prop.ShouldSerializeValue(b))
                {
                    break;
                }
            }

            // now copy the type array up to the point we quit.
            //
            Type[] ctorParams = new Type[lastItem + 1];
            Array.Copy(ConstructorParamaterTypes, 0, ctorParams, 0, ctorParams.Length);

            // Get the ctor info.
            //
            ConstructorInfo ctor = typeof(Binding).GetConstructor(ctorParams);

            Debug.Assert(ctor != null, "Failed to find Binding ctor for types!");
            if (ctor == null)
            {
                isComplete = false;
                ctor       = typeof(Binding).GetConstructor(new Type[] {
                    typeof(string),
                    typeof(object),
                    typeof(string)
                });
            }

            // now fill in the values.
            //
            object[] values = new object[ctorParams.Length];

            for (int i = 0; i < values.Length; i++)
            {
                object val = null;
                switch (i)
                {
                case 0:
                    val = b.PropertyName;
                    break;

                case 1:
                    val = b.BindToObject.DataSource;
                    break;

                case 2:
                    val = b.BindToObject.BindingMemberInfo.BindingMember;
                    break;

                default:
                    val = TypeDescriptor.GetProperties(b)[ConstructorParameterProperties[i]].GetValue(b);
                    break;
                }
                values[i] = val;
            }
            return(new InstanceDescriptor(ctor, values, isComplete));
        }
 public override bool ShouldSerializeValue(object o)
 {
     return(baseProp.ShouldSerializeValue(o));
 }