0

I have the following code taken from the thread: TypeConverter reuse in PropertyGrid .NET winforms

The code sample is from the reply to the above thread by Simon Mourier

public class Animals
{
    // both properties have the same TypeConverter, but they use different custom attributes
    [TypeConverter(typeof(ListTypeConverter))]
    [ListTypeConverter(new [] { "cat", "dog" })]
    public string Pets { get; set; }

    [TypeConverter(typeof(ListTypeConverter))]
    [ListTypeConverter(new [] { "cow", "sheep" })]
    public string Others { get; set; }
}

// this is your custom attribute
// Note attribute can only use constants (as they are added at compile time), so you can't add a List object here
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ListTypeConverterAttribute : Attribute
{
    public ListTypeConverterAttribute(string[] list)
    {
        List = list;
    }

    public string[] List { get; set; }
}

// this is the type converter that knows how to use the ListTypeConverterAttribute attribute
public class ListTypeConverter : TypeConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        var list = new List<string>();

        // the context contains the property descriptor
        // the property descriptor has all custom attributes defined on the property
        // just get it (with proper null handling)
        var choices = context.PropertyDescriptor.Attributes.OfType<ListTypeConverterAttribute>().FirstOrDefault()?.List;
        if (choices != null)
        {
            list.AddRange(choices);
        }
        return new StandardValuesCollection(list);
    }
}

This approach seems to be possible as I have also seen it referenced in the following two threads: http://geekswithblogs.net/abhijeetp/archive/2009/01/10/dynamic-attributes-in-c.aspx StringConverter GetStandardValueCollection

I can find the properties with ListTypeConverter custom attribute in the Animal class, and iterate through them

        var props = context.Instance.GetType().GetProperties().Where(
        prop => Attribute.IsDefined(prop, typeof(ListTypeConverterAttribute))).ToList();

The issue I am having is that the code provided by Simon and the others requires the context.PropertyDescriptor, and in my case

context.PropertyDescriptor == null

How can I find the Animal class property that invoked the ListTypeConverter so that I can return the proper custom attribute list in the

return new StandardValuesCollection(list);
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
PBrenek
  • 561
  • 1
  • 8
  • 24
  • Is your code running in the Winforms standard property grid? PropertyDescriptor shouldn't be null. What is the full type name of `context`? – Simon Mourier Mar 27 '19 at 08:13
  • The Animal class is a nested class: RTS > Animals The RTS class derives from ICustomTypeDescritor which is used to hide/show properties at runtime by modyfying the PropertyDescriptorCollection. – PBrenek Mar 27 '19 at 14:51
  • (context == null).ToString() is False / context.ToString() is System.Windows.Controls.WpfPropertyGrid.PropertyItem+ComponentContext / context.GetType().FullName is System.Windows.Controls.WpfPropertyGrid.PropertyItem+ComponentContext / context.GetType().Attributes.ToString() is AutoLayout, AnsiClass, Class, NestedPrivate, BeforeFieldInit – PBrenek Mar 27 '19 at 14:52
  • context.GetType().CustomAttributes.ToString() is System.Collections.ObjectModel.ReadOnlyCollection`1[System.Reflection.CustomAttributeData] / context.GetType().GetProperties().ToString() is System.Reflection.PropertyInfo[] / (context.PropertyDescriptor == null).ToString() is True – PBrenek Mar 27 '19 at 14:53
  • `WpfPropertyGrid` is **not** the Windows form property grid. I don't know about that thing but it's probably not fuly compatible the the Winforms one. – Simon Mourier Mar 27 '19 at 18:30
  • I am aware that it is not the same thing. I did not think though that any incompatibility issue might arise at something as basic as the PropertyDescriptor. As I am able to retrieve the properties, is there any other way to flag the actual property invoking the TypeDescriptor? Also, is there any way to add the WpfPropertyGrid tag to this question; I don't want to repost it. – PBrenek Mar 27 '19 at 19:06
  • When you're aware of something important, it's good to put it in the question. – Simon Mourier Mar 27 '19 at 19:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190785/discussion-between-pbrenek-and-simon-mourier). – PBrenek Mar 27 '19 at 19:22
  • I believe the question was tagged with Wpf and PropertyGrid but I agree; it should have been stated in the question itself for more clarity. – PBrenek Mar 27 '19 at 19:46

0 Answers0