c# - 使用 TypeConverter 进行依赖注入(inject)

标签 c# .net dependency-injection typeconverter

我有一个正在设计的应用程序,它引用了一个我也在设计的库。具体来说,应用程序需要创建我的下层库中定义的 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 是我的高级应用程序中的一个类,因为那是定义护套列表的地方。通常这个问题可以通过依赖注入(inject)来解决,但是因为这涉及类型转换器,所以我不确定是否可以使用依赖注入(inject)。我不确定如何解决这个问题

最佳答案

context 参数是预期的注入(inject)点。

像这样创建一个类:

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

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

关于c# - 使用 TypeConverter 进行依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31301522/

相关文章:

.net - .Net 上有 URL 验证器吗?

dependency-injection - 带有构造函数参数的 Dagger 模块?

c# - 具有适当缓存和 dbContext 使用的 DI 服务

c# - 无法为此文件显示设计器,因为无法设计其中的任何类

c# - Umbraco Lucene 或搜索多个日期范围

c# - 如何在数组C#中保存数据表的第一列

c# - 使用共享 IP 地址从 IIS 站点发出出站 Web 请求时失败

c# - 分配第二个数据网格的数据源时,DataGridView 不更新

c# - 未定义的 CLR 命名空间 - 未找到解决方案

javascript - 我应该将包依赖项传递给模块函数还是只在模块文件中需要它们?