c# – 如何将参数传递给TypeConverter派生类

前端之家收集整理的这篇文章主要介绍了c# – 如何将参数传递给TypeConverter派生类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将一些参数传递给从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,object value)
{
    if (value is string)
    {
        return LSValues1.IndexOf(value.ToString());
    }
    return base.ConvertFrom(context,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;
}
}

解决方法

我已经解决了这个任务.我在方法ConvertTo和ConvertFrom中使用了ITypeDescriptorContext上下文:
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());
}
原文链接:https://www.f2er.com/csharp/243276.html

猜你在找的C#相关文章