C# 如何将参数传递给TypeConverter派生类
我想将一些参数传递给从TypeConverter派生的类。请告诉我,我怎么做? 例如,我有一门课:C# 如何将参数传递给TypeConverter派生类,c#,.net,typeconverter,C#,.net,Typeconverter,我想将一些参数传递给从TypeConverter派生的类。请告诉我,我怎么做? 例如,我有一门课: public class DDlExample { [TypeConverter(typeof(ExClassConverter))] public int Bounds { get; set; } } class ExClassConverter : TypeConverter { public int FirstParam{get;set;} ... }
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派生的类,我将它应用于不同的属性以显示不同的值下拉列表。我如何从ExportsConverter: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;
}
}
[AttributeUsage(AttributeTargets.Property,AllowMultiple=true,Inherited=true)]
公共类ParamDesc:Attribute
{
公共参数描述(int-PD){dictd=PD;}
公共整数{get;set;}
}
公共类示例
{
[附件(1)]
[TypeConverter(typeof(感叹转换))]
公共整数边界{get;set;}
[附件(2)]
[TypeConverter(typeof(感叹转换))]
公共整数舍入{get;set;}
}
类转换程序:类型转换程序
{
私有列表LSValues1=新列表(新字符串[]{“第一”、“第二”、“第三”});
私有列表LSValues2=新列表(新字符串[]{“苹果”、“瓜”、“葡萄”});
公共覆盖布尔CanConvertFrom(ITypeScriptorContext上下文,类型sourceType)
{
if(sourceType==typeof(string))
返回true;
返回base.CanConvertFrom(上下文,sourceType);
}
公共覆盖布尔CanConvertTo(ITypeScriptorContext上下文,类型sourceType)
{
if(sourceType==typeof(int))
返回(sourceType==typeof(int)?真:假);
返回base.CanConvertTo(context,sourceType);
}
公共重写对象转换为(ITypeDescriptorContext上下文,
CultureInfo区域性,对象值,类型(类型)
{
if(值为int)
{
返回LSValues1[(int)值];
}
返回base.ConvertTo(上下文、区域性、值、类型);
}
公共重写对象转换自(ITypeDescriptorContext上下文,
文化信息(文化,对象值)
{
if(值为字符串)
{
返回LSValues1.IndexOf(value.ToString());
}
返回base.ConvertFrom(上下文、区域性、值);
}
公共覆盖布尔GetStandardValuesSupported(ITypeDescriptorContext上下文)
{
返回true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptor上下文)
{
返回true;
}
public override TypeConverter.StandardValues集合GetStandardValues(ITypeDescriptor上下文)
{
StandardValuesCollection svc=新的StandardValuesCollection(LSValues1);
返回svc;
}
}
我已经解决了任务。我在ConvertTo和ConvertFrom方法中使用了ITypeScriptorContext上下文
:
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());
}
老问题,但是
为了完整性起见,似乎是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的额外属性应用于属性的需要。不,不可能。最好的“解决方案”是使用构造函数中设置的默认值从
ExportsConverter
创建派生类。对于那些阅读此答案的人,我可以看出有遗漏的信息。显然,“ParamDesc”是一个属性,OP必须使用dictd成员实现该属性,以指示他想要该属性的标准值集。属性(也有TypeConverter属性)随后被这个属性修饰,以按照本文所示的方式将信息传递给类型转换器。GetStandardValues检查此属性,如图所示,并根据该属性决定返回什么。