.NET شبكة الملكية. هل هناك طريقة للسماح للشبكة بمعالجة الكائن بطريقة مختلفة

StackOverflow https://stackoverflow.com/questions/931644

سؤال

كما فهمت، يتم إعطاء شبكة الخاصية كائن يمكنه التعامل معه عن طريق استخراج خصائصه باستخدام تأملات.

مشكلتي هي أن لدي مجموعة من المعلمات التي يتم تحديدها أثناء وقت التشغيل، وبالتالي لا أستطيع إنشاء فئة واحدة مع خصائص تمثل هذه المجموعة.

لدي فكران في الاعتبار لحل هذه المشكلة، لكن كلاهما معقد وسيستهلك الكثير من الوقت، والصمم سأقول إنهم غير عمليين تحت قيود وقتي. واحد هو استخدام الانعكاس ينبعث من أجل تحديد فئة ديناميكيا والآخر هو بناء ملف مصدر C # ثم قم بتجميعه باستخدام Codedom.

يمكن أن تتصرف شبكة الممتلكات بطريقة مختلفة (أخرى ثم استخراج خصائص كائن باستخدام الانعكاسات) التي يمكن أن تنفجر مشكلتي؟

إذا لم يكن هناك أي عنصر تحكم آخر يمكنه القيام بهذه المهمة بالنسبة لي؟

أريد أن أقول إن السبب في أنني ذهبت إلى شبكة الممتلكات من البداية كانت قدرتها على توفير بيانات استرجاع البيانات لطيفة حقا بالنسبة للأنواع المشتركة. بالنسبة للون تلقي لوحة تلقائيا، بالنسبة ل DataTime، لديك تقويم لطيف تلقائيا. أود الحصول على تلك الأشياء تلقائيا، إن أمكن.

هل كانت مفيدة؟

المحلول

نعم، PropertyGrid يمكن عرض الأشياء آخر من خصائص وقت الترجمة فقط، باستخدام أي من TypeConverter, ICustomTypeDescriptor أو TypeDescriptionProvider لتوفير عقارات Runtime Pseudo. هل يمكنك إعطاء مثال على ما يبدو عليه المعلمات؟ يجب أن أكون قادرا على تقديم مثال ...


إليك مثال أساسي (كل شيء string, ، إلخ) بناء على الرد في وقت سابق (ذات الصلة ولكن مختلفة):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
class PropertyBagPropertyDescriptor : PropertyDescriptor {
    public PropertyBagPropertyDescriptor(string name) : base(name, null) { }
    public override object GetValue(object component) {
        return ((PropertyBag)component)[Name];
    }
    public override void SetValue(object component, object value) {
        ((PropertyBag)component)[Name] = (string)value;
    }
    public override void ResetValue(object component) {
        ((PropertyBag)component)[Name] = null;
    }
    public override bool CanResetValue(object component) {
        return true;
    }
    public override bool ShouldSerializeValue(object component)
    { // *** this controls whether it appears bold or not; you could compare
      // *** to a default value, or the last saved value...
        return ((PropertyBag)component)[Name] != null;
    }
    public override Type PropertyType {
        get { return typeof(string); }
    }
    public override bool IsReadOnly {
        get { return false; }
    }
    public override Type ComponentType {
        get { return typeof(PropertyBag); }
    }
}
[TypeConverter(typeof(PropertyBagConverter))]
class PropertyBag {
    public string[] GetKeys() {
        string[] keys = new string[values.Keys.Count];
        values.Keys.CopyTo(keys, 0);
        Array.Sort(keys);
        return keys;
    }
    private readonly Dictionary<string, string> values
        = new Dictionary<string, string>();
    public string this[string key] {
        get {
            string value;
            values.TryGetValue(key, out value);
            return value;
        }
        set {
            if (value == null) values.Remove(key);
            else values[key] = value;
        }
    }
}
// has the job of (among other things) providing properties to the PropertyGrid
class PropertyBagConverter : TypeConverter {
    public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
        return true; // are we providing custom properties from here?
    }
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, System.Attribute[] attributes) {
        // get the pseudo-properties
        PropertyBag bag = (PropertyBag)value;
        string[] keys = bag.GetKeys();
        PropertyDescriptor[] props = Array.ConvertAll(
            keys, key => new PropertyBagPropertyDescriptor(key));
        return new PropertyDescriptorCollection(props, true);
    }
}

static class Program {
    [STAThread]
    static void Main() { // demo form app
        PropertyBag bag = new PropertyBag();
        bag["abc"] = "def";
        bag["ghi"] = "jkl";
        bag["mno"] = "pqr";
        Application.EnableVisualStyles();
        Application.Run(
            new Form {
                Controls = { new PropertyGrid {
                    Dock = DockStyle.Fill,
                    SelectedObject = bag
                }}
            });
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top