将DesignerSerializationVisibility的默认值设置为hidden [英] Set the default value of DesignerSerializationVisibility to hidden

查看:138
本文介绍了将DesignerSerializationVisibility的默认值设置为hidden的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为给定类的所有属性设置属性DesignerSerializationVisibility的默认值?

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

在实践中,这是一种使用 white-list 方法切换黑名单的默认行为的方法.

In practice, a way to switch the default behavior of black-listing the properties with a white-list approach.

谢谢

推荐答案

我的偏爱

您可以为构造函数中的属性提供默认值,并使用适当的

My preferance

You can provide default values for properties in constructor and decorate them using suitable DefaultValue attribute, then the designer will serialize them only if the value of them is different than default value.

如果还需要在设计时使其不可见,则可以使用

Also if you need to make them invisible at design-time, you can simply decorate them using Browsable(false) then they will not be shown at design time.

您还可以检查> DesignMode 可以防止在设计时为属性设置值,并使其成为运行时属性.

Also you can check DesignMode in property setter to prevent setting a value for the property at design time and make it a run-time property.

我还回答了您的问题,不对没有DesignerSerializationVisibility属性的属性进行序列化.

I also answer your question which you need to Not serialize properties that doesn't have DesignerSerializationVisibility attribute.

至少该方法将为您引入一个真正有用的功能 将来会对您有所帮助.

At least the approach will introduce a really useful feature to you which you may find helpful in the future.

不对没有DesignerSerializationVisibility属性的属性进行序列化

一种切换将属性列入黑名单的默认行为的方法 使用白名单方法.

A way to switch the default behavior of blacklisting the properties with a white list approach.

作为一种选择,您可以为组件创建一个自定义类型描述符,并使用它返回的自定义属性描述符,告诉设计人员不要序列化没有

As an option you can create a custom type descriptor for your component and using custom property descriptors which it returns, tell the designer to don't serialize properties that doesn't have DesignerSerializationVisibility.

这样,当您希望设计人员序列化属性时,应使用DesignerSerializationVisibility属性(以visible作为值)修饰它.

This way when you want the designer serialize a property you should decorate it with DesignerSerializationVisibility attribute with visible as value.

Implemetaion

设计者询问 PropertyDescriptor 属性来决定要对该属性进行序列化.如果 ShouldSerialize <描述符的/a>方法返回true,它将序列化属性,否则不序列化属性.

The designer ask the PropertyDescriptor of a property to decide to serialize the property. If the ShouldSerialize method of the descriptor returns true it serializes the property, otherwise it doesn't serialize the property.

要更改此行为,您应该覆盖该方法.为了使设计人员能够使用您的属性描述符,您应该注册一个自定义 .自定义类型描述符应返回新的PropertyDescriptor的列表,您可以覆盖该方法.

To change this behavior you should override that method. To make the designer to use your property descriptor, you should register a custom TypeDescriptionProvider for your class. The provider should provide a custom TypeDescriptor for your class. The custom type descriptor should return a list of your new PropertyDescriptor which you override that method.

重要说明::要测试实现,应重新启动Visual Studio.

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

TypeDescriptionProvider

在这里,我们为组件创建一个自定义类型描述提供程序.然后,我们将注册组件的提供程序.

Here we create a custom type description provider for our component. Then we will register the provider 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 which it's job is returning 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);
    }
}

属性描述符

这是我们的自定义属性描述符的实现.大多数属性和方法的实现都是微不足道的.仅对于ShouldSerialize方法,我们基于具有DesignerSerializationVisibility

Here is the implementation of our custom property descriptor. The implementation of most properties and methods is trivial. Only for ShouldSerialize method, we decide based on having 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);
    }
}

组件

最后是组件.如您在组件代码中看到的,我们装饰了要序列化的属性(白名单策略).所有其他属性都不会序列化,因为这是我们附加到属性中的新行为.

At last here is the component. As you see in the component code, we decorated a property (white list strategy) to be serialized. All other properties will not serialize because it's new behavior which we attached 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; }
}

这篇关于将DesignerSerializationVisibility的默认值设置为hidden的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆