2013-02-18 30 views
6

Tôi muốn chuyển một số tham số đến lớp bắt nguồn từ TypeConverter. Hãy cho tôi biết, làm thế nào tôi có thể làm điều đó? Ví dụ, tôi có lớp học này:Cách chuyển tham số sang lớp dẫn xuất TypeConverter

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

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

Tôi muốn vượt qua giá trị FirstParam như thế này:

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

là nó có thể?

Dường như, nhiệm vụ này không có quyết định. Tôi sẽ cố gắng giải quyết vấn đề. Tôi có một lớp bắt nguồn từ TypeConverter và tôi áp dụng nó cho các thuộc tính khác nhau để hiển thị danh sách thả xuống các giá trị khác nhau. Làm thế nào tôi có thể xác định các thuộc tính nào từ ExClassConverter : TypeConverter để điền vào danh sách thả xuống với các giá trị thích hợp?

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

Không, không thể. Giải pháp 'tốt nhất' là tạo ra các lớp dẫn xuất từ ​​'ExClassConverter' với các giá trị mặc định được đặt trong hàm tạo. – leppie

Trả lời

7

Tôi đã giải quyết được tác vụ. Tôi đã từng ITypeDescriptorContext context xét các phương pháp ConvertTo và 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()); 
} 
4

Cũ câu hỏi, nhưng ...

Đối với lợi ích của sự hoàn chỉnh, một giải pháp cho những gì dường như là vấn đề OP là: chỉ cần kiểm tra các tên thuộc tính các loại chuyển đổi đang được gọi là chống lại. Ví dụ:

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

Giải pháp này tránh cần thêm thuộc tính OP được áp dụng cho sản phẩm.

Các vấn đề liên quan