2015-07-08 33 views
8

Ayrıca tasarladığım bir kütüphaneye referans veren bir uygulama var. Özellikle, uygulama benim alt düzey kitaplığımda tanımlanan benim Yönetim Kurulu sınıf örnekleri yapmak gerekir.TypeConverters ile Bağımlılık Enjeksiyonu

[TypeConverter(typeof(SheathingOptionsConverter))] 
public class Sheathing : Lumber 
{ 
    public string Description { get; set; } 

    public Sheathing(string passedDescription) 
    { 
     Description = passedDescription; 
    } 
} 

Uygulamam, özellikler kılavuzunda farklı kaplama seçenekleri listeler. Onları bir açılır menüde listelediğinden, iki kez ExpandableObjectConverter genişletmek zorunda kaldım. İlk seviye aşağı düzgün bir özellik ızgarasında bir açılır menü olarak kaplaması nesnelerin listesini görüntülemek için SheathingObjectConverter tek Mantolama nesnesi

public class SheathingObjectConverter : ExpandableObjectConverter 
{ 
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     if (destinationType == typeof(Sheathing)) 
     { 
      return true; 
     } 
     return base.CanConvertTo(context, destinationType); 
    } 

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     if (sourceType == typeof(string)) 
     { 
      return true; 
     } 
     return base.CanConvertFrom(context, sourceType); 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) 
    { 
     if (destinationType == typeof(System.String) && value is Sheathing) 
     { 
      Sheathing s = (Sheathing)value; 
      return "Description: " + s.Description; 
     } 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    { 
     if (value is string) 
     { 
      try 
      { 
       string description = (string)value; 
       Sheathing s = new Sheathing(description); 
       return s; 
      } 
      catch 
      { 
       throw new ArgumentException("Can not convert '" + (string)value + "' to type Sheathing"); 
      } 
     } 
     return base.ConvertFrom(context, culture, value); 
    } 
} 

Ve ikinci seviyesi aşağı görüntüler uzanır benim SheathingObjectConverter olduğunu

public class SheathingOptionsConverter : SheathingObjectConverter 
{ 
    /// <summary> 
    /// Override the GetStandardValuesSupported method and return true to indicate that this object supports a standard set of values that can be picked from a list 
    /// </summary> 
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context) 
    { 
     return true; 
    } 

    /// <summary> 
    /// Override the GetStandardValues method and return a StandardValuesCollection filled with your standard values 
    /// </summary> 
    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
    { 
     List<Sheathing> sheathingDescriptions = SettingsController.AvailableSheathings; //ToDo: Fix needing a list from the application (higher level) 
     return new StandardValuesCollection(sheathingDescriptions.ToArray()); 
    } 
} 

İşte burada yatıyor; Yukarıdaki tüm kod benim alt düzey kitaplığımdayken, ancak SettingsController, yüksek düzeydeki uygulamamdaki bir sınıftır, çünkü kılıf listesinin tanımlandığı yer budur. Normalde bu sorun bağımlılık enjeksiyonu ile çözülebilirdi, ancak bu tip konvertörlerle ilgilenir çünkü bağımlılık enjeksiyonunun kullanılıp kullanılamayacağından emin değilim. Bu sorunun nasıl düzeltileceğinden emin değilim

cevap

0

context argümanı, hedeflenen enjeksiyon noktasıdır.

Böyle bir sınıf oluşturun:
class SheathingContext : ITypeDescriptorContext 
{ 
    public List<Sheathing> AvailableSheathings 
    { 
     get { return SettingsController.AvailableSheathings; } 
    } 
} 

Ardından tip dönüştürücü context olarak geçmektedir. Tip dönüştürücüde, listeniz için ((SheathingContext)context).AvailableSheathings kullanabilirsiniz.

+1

Arabirimin geri kalanını uygulamak zorunda değil misiniz? –

+1

@Jake Smith Evet, ancak bu alanlar sadece boş değerlere dönebilir. Https://msdn.microsoft.com/en-us/library/system.componentmodel.itypedescriptorcontext(v=vs.110).aspx adresindeki nota bakın. – dss539

İlgili konular