【问题标题】:Dependency Injection with TypeConverters使用 TypeConverters 进行依赖注入
【发布时间】:2015-09-26 21:16:10
【问题描述】:

我正在设计一个应用程序,它引用了我也在设计的一个库。具体来说,应用程序需要创建在我的较低级别库中定义的 Sheathing 类的实例。

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

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

我的应用程序在属性网格中列出了不同的护套选项。因为它在下拉菜单中列出了它们,所以我不得不将 ExpandableObjectConverter 扩展两次。第一级是我的 SheathingObjectConverter,它正确显示了单个 Sheathing 对象

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

第二级向下扩展了 SheathingObjectConverter 以在属性网格中将 Sheathing 对象列表显示为下拉菜单

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

这就是问题所在;上面的所有代码都在我的较低级别的库中,但 SettingsController 是我的较高级别应用程序中的一个类,因为那是定义护套列表的地方。通常这个问题可以通过依赖注入来解决,但是因为这涉及类型转换器,所以我不确定是否可以使用依赖注入。我不知道如何解决这个问题

【问题讨论】:

    标签: c# .net dependency-injection typeconverter


    【解决方案1】:

    context 参数是预期的注入点。

    像这样创建一个类:

    class SheathingContext : ITypeDescriptorContext
    {
        public List<Sheathing> AvailableSheathings 
        {
            get { return SettingsController.AvailableSheathings; }
        }
    }
    

    然后将其作为context 传递给类型转换器。 在类型转换器中,您可以使用((SheathingContext)context).AvailableSheathings 作为您的列表。

    【讨论】:

    猜你喜欢
    • 2014-01-19
    • 2014-03-25
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多