Beispiel #1
0
        public void ValidateUsageWithContainerType_ContainerTypeWithOnePropertyWithArgumentsAttribute_ValidatesOK()
        {
            Type containerType = typeof(ContainerWithOneArgumentsAttributeProperty);
            var  attr          = new ArgumentsAttribute();

            attr.ValidateUsage(containerType);
        }
Beispiel #2
0
        public void ValidateUsageWithContainerType_ContainerTypeWithTwoPropertiesWithArgumentsAttribute_ThrowsInvalidOperationException()
        {
            Type containerType = typeof(ContainerWithTwoArgumentsAttributeProperties);
            var  attr          = new ArgumentsAttribute();

            Assert.Throws <InvalidOperationException>(() => attr.ValidateUsage(containerType));
        }
Beispiel #3
0
        public void ValidateUsageWithPropertyInfo_PropertyWithoutArgumentsAttribute_ThrowsInvalidOperationException()
        {
            PropertyInfo property = typeof(ContainerWithPropertyWithoutArgumentsAttribute).GetProperty("Arguments1");
            var          attr     = new ArgumentsAttribute();

            Assert.Throws <InvalidOperationException>(() => attr.ValidateUsage(property));
        }
Beispiel #4
0
        public void ValidateUsageWithPropertyInfo_PropertyWithArgumentsAttributeOnStringCollectionProperty_ValidatesOK()
        {
            PropertyInfo property = typeof(ContainerWithStringCollectionArgumentsProperty).GetProperty("Arguments");
            var          attr     = new ArgumentsAttribute();

            attr.ValidateUsage(property);
        }
Beispiel #5
0
        public void ValidateUsageWithPropertyInfo_PropertyWithArgumentsAttributeButNotACollection_ThrowsInvalidOperationException()
        {
            PropertyInfo property = typeof(ContainerWithNonCollectionArgumentsProperty).GetProperty("Arguments");
            var          attr     = new ArgumentsAttribute();

            Assert.Throws <InvalidOperationException>(() => attr.ValidateUsage(property));
        }
Beispiel #6
0
        ///////////////////////////////////////////////////////////////////////

        #region MemberInfo (Mostly Type) Attribute Methods
        public static int GetArguments(
            MemberInfo memberInfo
            )
        {
            if (memberInfo != null)
            {
                try
                {
                    if (memberInfo.IsDefined(
                            typeof(ArgumentsAttribute), false))
                    {
                        ArgumentsAttribute arguments =
                            (ArgumentsAttribute)
                            memberInfo.GetCustomAttributes(
                                typeof(ArgumentsAttribute), false)[0];

                        return(arguments.Arguments);
                    }
                }
                catch
                {
                    // do nothing.
                }
            }

            return((int)Arity.None);
        }
Beispiel #7
0
        /// <summary>Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"/> method.</summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that can be used to gain additional context information. </param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"/> that this editor can use to obtain services. </param>
        /// <param name="value">The object to edit. </param>
        /// <returns>The new value of the object. If the value of the object has not changed, this should return the same object it was passed.</returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if ((context == null) || (provider == null))
            {
                return(base.EditValue(context, provider, value));
            }

            IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (editorService == null)
            {
                return(base.EditValue(context, provider, value));
            }

            Location      = Convert.ToString(value);
            DirectoryInfo = Directory.GetParent(Location);
            RootFolder    = DefaultRootFolder;

            Description         = string.Empty;
            ShowNewFolderButton = false;

            // Validate the custom attributes.
            if (context.PropertyDescriptor != null)
            {
                // Get all the arguments of the specific type to a list.
                var argumentsAttributes = context.PropertyDescriptor.Attributes.OfType <ArgumentsAttribute>().ToList();

                // Handle when the arguments list contains items.
                if (argumentsAttributes.Any())
                {
                    // Get the first argument in the list index.
                    ArgumentsAttribute argumentsAttribute = argumentsAttributes.First();

                    // Initialize new values
                    Location      = argumentsAttribute.Location;
                    DirectoryInfo = Directory.GetParent(Location);
                    RootFolder    = argumentsAttribute.RootFolder;

                    // Pass variables to properties
                    Description         = argumentsAttribute.Description;
                    ShowNewFolderButton = argumentsAttribute.ShowNewFolderButton;
                }
            }

            // Open the specified directory / folder browser dialog.
            Location = OpenFolderBrowserDialog();

            // Validate the file path.
            if (!string.IsNullOrEmpty(Location))
            {
                return(Location);
            }
            else
            {
                return(base.EditValue(context, provider, value));
            }
        }