TypeConverters的依赖注入

Nic*_*ert 10 .net c# dependency-injection typeconverter

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

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

    public Sheathing(string passedDescription)
    {
        Description = passedDescription;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序在属性网格中列出了不同的护套选项.因为它在下拉菜单中列出它们所以我必须将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);
    }
}
Run Code Online (Sandbox Code Playgroud)

第二级向下扩展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());
    }
}
Run Code Online (Sandbox Code Playgroud)

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

dss*_*539 1

参数context是预期的注入点。

创建一个像这样的类:

class SheathingContext : ITypeDescriptorContext
{
    public List<Sheathing> AvailableSheathings 
    {
        get { return SettingsController.AvailableSheathings; }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 您不需要实现接口的其余部分吗? (2认同)
  • @Jake Smith 是的,但这些字段只能返回 null。请参阅 https://msdn.microsoft.com/en-us/library/system.componentmodel.itypedescriptorcontext(v=vs.110).aspx 上的注释 (2认同)