Set the default value for DesignerSerializationVisibility for hidden

Is there a way to set the default value for the DesignerSerializationVisibility attribute for all properties of this class?

In practice, the way to switch the default behavior in the blacklist of properties using the whitelist method.

thanks

+1
c # visual-studio winforms windows-forms-designer
source share
1 answer

My preference

You can provide default values ​​for the properties in the constructor and decorate them with the appropriate DefaultValue attribute, then the constructor will serialize them only if their value is different from the default.

Also, if you need to make them invisible during development, you can simply decorate them using Browsable(false) , after which they will not be shown during development.

You can also check the DesignMode in the property adjuster to prevent the property from setting a value at design time and make it a run-time property.

I will also answer your question that you need so as not to serialize properties that do not have the DesignerSerializationVisibility attribute.

At the very least, this approach will present you with a really useful feature that you might find useful in the future.

Do not serialize properties that do not have the DesignerSerializationVisibility attribute

A way to switch the default behavior to a blacklist of properties using a whitelist.

As an option, you can create a custom type descriptor for your component and use the custom property descriptors that it returns, tell the developer not to serialize properties that do not have DesignerSerializationVisibility .

Thus, if you want the constructor to serialize the property, you must decorate it with the DesignerSerializationVisibility attribute with visible as the value.

Implemetaion

The designer will ask PropertyDescriptor properties to decide to serialize the property. If the ShouldSerialize descriptor method returns true , it serializes the property; otherwise, it does not serialize the property.

To change this behavior, you must override this method. For a developer to use your property descriptor, you must register a TypeDescriptionProvider for your class. The provider must provide a custom TypeDescriptor for your class. A custom type descriptor should return a list of your new PropertyDescriptor , which you will override this method.

Important Note: To test implementations, you must restart visual studio.

TypeDescriptionProvider

Here we create a custom type description provider for our component. Then we will register the supplier for our component.

 public class MyTypeDescriptionProvider : TypeDescriptionProvider { public MyTypeDescriptionProvider() : base(TypeDescriptor.GetProvider(typeof(object))) { } public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) { ICustomTypeDescriptor baseDescriptor = base.GetTypeDescriptor(objectType, instance); return new MyTypeDescriptor(baseDescriptor); } } 

TypeDescriptor

Here we implement our type descriptor, in which the job returns a list of our custom property descriptors.

 public class MyTypeDescriptor : CustomTypeDescriptor { ICustomTypeDescriptor original; public MyTypeDescriptor(ICustomTypeDescriptor originalDescriptor) : base(originalDescriptor) { original = originalDescriptor; } public override PropertyDescriptorCollection GetProperties() { return this.GetProperties(new Attribute[] { }); } public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) { var properties = base.GetProperties(attributes).Cast<PropertyDescriptor>() .Select(p => new MyPropertyDescriptor(p)) .ToArray(); return new PropertyDescriptorCollection(properties); } } 

PropertyDescriptor

Here is the implementation of our custom property descriptor. The implementation of most properties and methods is trivial. Only for the ShouldSerialize method we decide based on the presence of DesignerSerializationVisibility

 public class MyPropertyDescriptor : PropertyDescriptor { PropertyDescriptor original; public MyPropertyDescriptor(PropertyDescriptor originalProperty) : base(originalProperty) { original = originalProperty; } // Implement other properties and methods simply using return original // The implementation is trivial like this one: // public override Type ComponentType // { // get { return original.ComponentType; } // } public override bool ShouldSerializeValue(object component) { if (original.Attributes.OfType<DesignerSerializationVisibilityAttribute>() .Count() == 0) return false; return original.ShouldSerializeValue(component); } } 

Component

Finally, here is the component. As you can see in the component code, we decorated the property (white list strategy) for serialization. All other properties will not be serialized, because this is a new behavior that we tied to our properties.

 [TypeDescriptionProvider(typeof(MyTypeDescriptionProvider))] public class MyCustomClass : Component { public string Property1 { get; set; } public string Property2 { get; set; } [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public string Property3 { get; set; } } 
+4
source share

All Articles