احصل على الخصوصية الافتراضية
-
09-09-2019 - |
سؤال
أنا تخصيص كيف يتم عرض نوع الكائن في PropertyGrid
عن طريق التنفيذ ICustomTypeDescriptor
. وبعد أنا أسمح للمستخدم بإنشاء خصائص مخصصة خاصة بهم المخزنة في قاموس واحد للمفاتيح والقيم. أنا قادر على إنشاء كل PropertyDescriptors
لهذه القيم وعرضها في شبكة الملكية. ومع ذلك، أريد أيضا إظهار كل الخصائص الافتراضية التي كان من شأنها أن تظهر إذا PropertyGrid
تم ملؤها من خلال انعكاس بدلا من التجاوز ICustomTypeDescriptor.GetProperties
طريقة.
الآن أعرف كيفية الحصول على نوع الكائن، ثم GetProperties()
, ، ولكن هذا إرجاع مجموعة من PropertyInfo
ليس ProperyDescriptor
. وبعد فكيف يمكنني تحويل PropertyInfo
كائن من النوع PropertyDescriptor
الكائنات لتشمل في مجموعتي مع العرف PropertyDescriptors
?
//gets the local intrinsic properties of the object
Type thisType = this.GetType();
PropertyInfo[] thisProps = thisType.GetProperties();
//this line obviously doesn't work because the propertydescriptor
//collection needs an array of PropertyDescriptors not PropertyInfo
PropertyDescriptorCollection propCOl =
new PropertyDescriptorCollection(thisProps);
المحلول
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(thisType);
جانبا: هذا لن يشمل الخاص بك ICustomTypeDescriptor
توضيحات، لكنها إرادة تشمل أي تجميلات مصنوعة من خلال TypeDescriptionProvider
.
(تحرير) كثاني جانبا - يمكنك أيضا القرص PropertyGrid
من خلال توفير أ TypeConverter
- أبسط بكثير من أي ICustomTypeDescriptor
أو TypeDescriptionProvider
- علي سبيل المثال:
[TypeConverter(typeof(FooConverter))]
class Foo { }
class FooConverter : ExpandableObjectConverter
{
public override PropertyDescriptorCollection GetProperties(
ITypeDescriptorContext context, object value, Attribute[] attributes)
{
// your code here, perhaps using base.GetPoperties(
// context, value, attributes);
}
}