簡體   English   中英

PropertyDescriptor和屬性

[英]PropertyDescriptor and Attributes

我繼承了PropertyDescriptor類,以提供某種“動態”屬性。 我向PropertyDescriptor添加了一些屬性。 這很完美。

PropertyGrid顯示對象時, ReadOnlyAttribute有效,但EditorAttribute不起作用!

internal class ParameterDescriptor: PropertyDescriptor {
    //...
    public ParameterDescriptor(/* ... */) {
        List<Attribute> a = new List<Attribute>();
        string editor = "System.ComponentModel.Design.MultilineStringEditor,System.Design";
        //...
        a.Add(new ReadOnlyAttribute(true));                         // works
        a.Add(new DescriptionAttribute("text"));                    // works
        a.Add(new EditorAttribute(editor, typeof(UITypeEditor)));   // doesn't work!
        //...    
        this.AttributeArray = a.ToArray();
    }
}

顯示的對象使用繼承的TypeConverter

public class ParameterBoxTypeConverter: TypeConverter {
    public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
        return true;
    }

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) {
        List<PropertyDescriptor> desc = new List<PropertyDescriptor>();
        //...
        ParameterDescriptor d = new ParameterDescriptor(/* ... */);
        desc.Add(d);
        //....
        return new PropertyDescriptorCollection(desc.ToArray());
    }

我被困住了,因為PropertyGrid根本不顯示任何內容(我希望屬性值出現“ ...”)。 而且似乎無法調試!

那么,我怎么在這里找到問題所在?
有沒有一種方法可以調試到PropertyGrid等中?

通過一些快速測試,該名稱需要完全合格:

const string name = "System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
attribs.Add(new EditorAttribute(name, typeof(UITypeEditor)));

在內部,它使用Type.GetType ,以及:

var type1 = Type.GetType("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
// ^^^ not null
var type2 = Type.GetType("System.ComponentModel.Design.MultilineStringEditor, System.Design");
// ^^^ null

當然,您可以使用:

attribs.Add(new EditorAttribute(typeof(MultilineStringEditor), typeof(UITypeEditor)));

或者,您可以override GetEditor並執行所需的任何操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM