【问题标题】:How to pass parameter to TypeConverter derived class如何将参数传递给 TypeConverter 派生类
【发布时间】:2013-02-02 11:49:41
【问题描述】:

我想将一些参数传递给从 TypeConverter 派生的类。请告诉我,我该怎么做? 比如我有这个类:

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

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

我想像这样传递值 FirstParam:

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

有可能吗?

看来这个任务没有解决办法。我会尝试重申这个问题。 我有一个派生自 TypeConverter 的类,并将其应用于不同的属性以显示不同的值下拉列表。如何定义 ExClassConverter : TypeConverter 中的哪些属性以使用适当的值填充下拉列表?

[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;
}
}

【问题讨论】:

  • 不,不可能。最好的“解决方案”是从 ExClassConverter 创建派生类,并在构造函数中设置这些默认值。

标签: c# .net typeconverter


【解决方案1】:

我已经完成了任务。我在 ConvertTo 和 ConvertFrom 方法中使用了ITypeDescriptorContext context

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());
}

【讨论】:

  • 对于那些阅读这个答案的人,我可以看出有遗漏的信息。显然,“ParamDesc”是一个属性,它必须是 OP 使用 DictID 成员实现的属性,以指示他想要该属性的哪一组标准值。然后使用此属性修饰属性(也具有 TypeConverter 属性),以按照本文中所示的方式将信息传递给类型转换器。 GetStandardValues 如图所示检查此属性并根据该属性决定返回什么。
【解决方案2】:

老问题,但是...

为了完整起见,似乎是 OP 问题的解决方案是:只需检查调用类型转换器的属性名称。例如,

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;
}

此解决方案避免了将 OP 的额外属性应用于属性的需要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 2014-04-11
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    相关资源
    最近更新 更多