将多个属性组合到单个属性中

时间:2016-07-21 11:32:52

标签: c# .net winforms visual-studio windows-forms-designer

在控件上我使用多个属性属性:

[Browsable(false)]
[Bindable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Obsolete("", true)]
public new Boolean AllowDrop;

我也在许多其他控件属性上使用这些属性。

我想知道是否有办法减少每次写入的代码量。

如果我可以组合多个这样的属性,那就太好了:

[Hidden(true)]
public new Boolean AllowDrop;

Hidden属性包含上述所有属性。所以只有一行代码。

也许还有一种方法可以将宏中的属性组合起来?

我知道还有其他隐藏属性的方法,但我选择了使用属性的方式。

由于

1 个答案:

答案 0 :(得分:12)

组合属性对于使用和解释属性的上下文可能是有意义的。例如,对于那些使用.Net Type Description机制的上下文,您可以自定义.Net返回给消费者的类型描述。

可以为此目的使用标准.Net机制为类型提供自定义元数据,为您的对象注册自定义类型描述符。

这个想法将以这种方式工作,您为您的类型创建自定义类型描述符。在自定义类型描述符中,返回类型属性的自定义属性描述符,并在属性描述符中返回属性的自定义属性集。

这种方法需要更多代码,但它非常有趣,并且对如何为您的类型提供自定义元数据有一些好主意:

IMetedataAttribute接口

用法是提供创建MetaDataAttributes的标准方法。实现此接口的每个属性将用作元数据,而不是属性,将使用它在Process方法中返回的属性:

public interface IMetadatAttribute
{
    Attribute[] Process();
}

示例MetadataAttribute

它是一个示例元数据属性,它在处理属性时返回一些属性:

public class MySampleMetadataAttribute : Attribute, IMetadatAttribute
{
    public Attribute[] Process()
    {
        var attributes = new Attribute[]{ 
            new BrowsableAttribute(false),
            new EditorBrowsableAttribute(EditorBrowsableState.Never), 
            new BindableAttribute(false),
            new DesignerSerializationVisibilityAttribute(
                    DesignerSerializationVisibility.Hidden),
            new ObsoleteAttribute("", true)
        };
        return attributes;
    }
}

属性描述符

自定义类型描述符将使用此类为属性提供自定义属性列表:

public class MyPropertyDescriptor : PropertyDescriptor
{
    PropertyDescriptor original;
    public MyPropertyDescriptor(PropertyDescriptor originalProperty)
        : base(originalProperty) { original = originalProperty;}
    public override AttributeCollection Attributes
    {
        get
        {
            var attributes = base.Attributes.Cast<Attribute>();
            var result = new List<Attribute>();
            foreach (var item in attributes)
            {
                if(item is IMetadatAttribute)
                {
                    var attrs = ((IMetadatAttribute)item).Process();
                    if(attrs !=null )
                    {
                        foreach (var a in attrs)
                            result.Add(a);
                    }
                }
                else
                    result.Add(item);
            }
            return new AttributeCollection(result.ToArray());
        }
    }
    // 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 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);
    }
}

Typedescriptor Provider

此类将在您的类型上方的属性中使用,以引入我们创建的自定义类型描述符作为类型的元数据引擎:

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

样本类

这是我的示例类,其Name属性使用MySampleMetadataAttribute进行修饰,并且类本身已注册使用我们的自定义类型描述符提供程序:

[TypeDescriptionProvider(typeof(MyTypeDescriptionProvider))]
public class MySampleClass
{
    public int Id { get; set; }
    [MySampleMetadataAttribue]
    [DisplayName("My Name")]
    public string Name { get; set; }
}

要查看结果,它足以创建类的实例并在PropertyGrid中查看结果:

var o = new MySampleClass();
this.propertyGrid1.SelectedObject = o;

关于回答的一些注意事项

  • 可能它并不像你期望的那样简单。但它正在发挥作用。
  • 这是一个冗长的答案,但包含一个完整的工作示例,说明如何将类型描述符应用于您的类型以提供自定义元数据。
  • 该方法不适用于使用反射而非类型描述的引擎。但它完全适用于例如PropertyGrid控件,它适用于类型描述。
相关问题