首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带接口的TypeConverter

带接口的TypeConverter
EN

Stack Overflow用户
提问于 2015-05-18 04:37:22
回答 1查看 1.5K关注 0票数 0

我在将C#中的属性实现为设计器类型时遇到了一些小问题。基本上,我有多个实现接口的具体类型。我想在设计时选择哪种混凝土类型。

代码语言:javascript
复制
using System;
using System.ComponentModel;
using System.Drawing;

/// <summary>
/// implement a summarizable type
/// </summary>
public interface ISummarizable
{
    /// <summary>
    /// Summarize along rows
    /// </summary>
    /// <param name="rawdata"></param>
    /// <returns></returns>
    double[] Summarize(double[,] rawdata);
}

/// <summary>
/// calculate by mean value
/// </summary>
public class MeanSummary : ISummarizable
{
    public double[] Summarize(double[,] rawdata)
    {
        double[] result = new double[rawdata.GetLength(0)];
        for (int n = 0; n < result.Length; n++)
        {
            double value = 0;
            for (int j = 0; n < rawdata.GetLength(1); j++)
                value += rawdata[n, j] / rawdata.GetLength(1);
            result[n] = value;
        }
        return result;
    }
}

/// <summary>
/// Calculate by max value
/// </summary>
public class MaxSummary : ISummarizable
{
    public double[] Summarize(double[,] rawdata)
    {
        double[] result = new double[rawdata.GetLength(0)];
        for (int n = 0; n < result.Length; n++)
        {
            double value = double.MinValue;     // guaranteed to change at least once
            for (int j = 0; n < rawdata.GetLength(1); j++)
                value = Math.Max(value, rawdata[n, j]);
            result[n] = value;
        }
        return result;
    }
}

/// <summary>
/// type converter
/// </summary>
public class SummaryTypeConverter : System.ComponentModel.TypeListConverter
{
    public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;
        return base.CanConvertFrom(context, sourceType);
    }

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

    public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value is string)
            return value.ToString();
        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            if(value is ISummarizable)
            {
                return value.GetType().Name;
            }
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }


    /// <summary>
    /// constructor w/ initializer
    /// </summary>
    public SummaryTypeConverter() :
        base(new Type[] { typeof(MeanSummary), typeof(MaxSummary) })
    {
    }
}

public class MyControl : System.Windows.Forms.Control
{
    protected ISummarizable _summary = new MaxSummary();

    [TypeConverter(typeof(SummaryTypeConverter))]
    public ISummarizable Summary { get { return _summary; } set { _summary = value; } }


}

在属性查看器中,我为“汇总”属性提供了一个不错的下拉列表,其中包含了我的两种具体类型。但是在选择一个时,错误是

代码语言:javascript
复制
Object of type System.String cannot be converted to type Example.ISummarizable

我尝试在ConvertFrom和ConvertTo中实现ConvertFrom和ConvertTo,但没有成功,并尝试使用第二个Visual实例对其进行调试,但我甚至无法捕获此错误。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-18 05:55:05

在您的ConvertFrom实现中,在这种情况下,您会收到一个有意义的字符串(例如。),您需要返回实际类型的实例,而不是调用基方法(正如Carsten所说)。

您可以尝试使用反射(查找具有该名称的类型,检查它是否为ISummarizable,调用构造函数)或使用开关语句。

通过反射,您的代码将是:

代码语言:javascript
复制
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
    if (value is string)
    {
        var source = (string)value;
        var sourceType = Type.GetType(source);

        if (sourceType != null && typeof(ISummarizable).IsAssignableFrom(sourceType))
        {
            var constructor = sourceType.GetConstructor(Type.EmptyTypes);
            var sourceInstance = constructor.Invoke(new object[0]);

            return sourceInstance;
        }
    }

    return base.ConvertFrom(context, culture, value);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30295193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档