Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter to TypeConverter derived class

I would like to pass some parameter to class derived from TypeConverter. Tell me please, how can I do it? For example, I have this class:

public class DDlExample
{
        [TypeConverter(typeof(ExClassConverter))]
        public int Bounds { get; set; }
}

class ExClassConverter : TypeConverter
{
  public int FirstParam{get;set;}
...
}

I would like to pass value FirstParam like this:

public class DDlExample
{
        [TypeConverter(typeof(ExClassConverter), ***FirstParam=2***)]
        public int Bounds { get; set; }
}

Is it possible?

It seems this task has no solution. I'll try to restate the problem. I have one class derived from TypeConverter and I apply it to different properties to show different values drop down list. How can I define which properties from ExClassConverter : TypeConverter to fill drop down list with appropriate values?

[AttributeUsage(AttributeTargets.Property,AllowMultiple=true,Inherited=true)]   
public class ParamDesc:Attribute
{
    public ParamDesc(int PD) { DictID = PD; }
    public int DictID { get; set; }
}

public class DDlExample
{
    [ParamDesc(1)]
    [TypeConverter(typeof(ExClassConverter))]
    public int Bounds { get; set; }

    [ParamDes(2)]
    [TypeConverter(typeof(ExClassConverter))]
    public int Rounds { get; set; }
}

class ExClassConverter : TypeConverter
{

private List<string> LSValues1 = new List<string>(new string[] {"first","second","third"});
private List<string> LSValues2 = new List<string>(new string[] {"apple","melon","grapes"});

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
   if (sourceType == typeof(string))
      return true;
    return base.CanConvertFrom(context, sourceType);

}

public override bool CanConvertTo(ITypeDescriptorContext context, Type sourceType)
{            
   if (sourceType == typeof(int))
           return (sourceType == typeof(int)?true:false);
    return base.CanConvertTo(context, sourceType);
}

public override object ConvertTo(ITypeDescriptorContext context,
    CultureInfo culture, object value, Type destType)
{
        if (value is int)
        {          
            return LSValues1[(int)value];
        }
        return base.ConvertTo(context, culture, value, destType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
    if (value is string)
    {
        return LSValues1.IndexOf(value.ToString());
    }
    return base.ConvertFrom(context, culture, value);

}

public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
    return true;
}

public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
    return true;
}

public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
    StandardValuesCollection svc = new StandardValuesCollection(LSValues1);
    return svc;
}
}
like image 734
Serg Okunev Avatar asked Feb 18 '13 04:02

Serg Okunev


2 Answers

I have solved the task. I used ITypeDescriptorContext context at methods ConvertTo and ConvertFrom:

if (context != null)
{
    AttributeCollection ua = context.PropertyDescriptor.Attributes;                    
    ParamDesc cca = (ParamDesc)ua[typeof(ParamDesc)];    
    if (cca != null)
        System.Console.WriteLine("Attribute value is " + cca.DictID.ToString());
}
like image 57
Serg Okunev Avatar answered Oct 07 '22 21:10

Serg Okunev


Old question, but ...

For sake of completeness, a solution for what appears to be OP's problem is: just check the property name the type converter is being called against. For example,

public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
    StandardValuesCollection svc;
    if (context.PropertyDescriptor.Name == "Bounds")
        svc = new StandardValuesCollection(LSValues1);
    else if (context.PropertyDescriptor.Name == "Rounds")
        svc = new StandardValuesCollection(LSValues2);
    return svc;
}

This solution avoids need for OP's extra attribute applied to the property.

like image 32
dathompson Avatar answered Oct 07 '22 21:10

dathompson