Chciałbym przekazać niektóre parametry do klasy pochodzących z TypeConverter. Powiedz mi proszę, jak mogę to zrobić? Na przykład, mam tej klasy:Jak przekazać parametr do klasy pochodnej TypeConverter
public class DDlExample
{
[TypeConverter(typeof(ExClassConverter))]
public int Bounds { get; set; }
}
class ExClassConverter : TypeConverter
{
public int FirstParam{get;set;}
...
}
Chciałabym przekazać wartość FirstParam takiego:
public class DDlExample
{
[TypeConverter(typeof(ExClassConverter), ***FirstParam=2***)]
public int Bounds { get; set; }
}
Czy to możliwe?
Wygląda na to, że zadanie to nie ma żadnej decyzji. Spróbuję naprawić problem. Mam jedną klasę pochodną TypeConverter i stosuję ją dla różnych właściwości, aby wyświetlić listę rozwijaną różnych wartości. Jak mogę zdefiniować, które właściwości z listy rozwijanej należy uzupełnić o odpowiednie wartości?
[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;
}
}
Nie, niemożliwe. Najlepszym "rozwiązaniem" jest tworzenie klas pochodnych z 'ExClassConverter' z tymi wartościami domyślnymi ustawionymi w konstruktorze. – leppie